]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java
2f6325d279ccf687f82370771c8b5237746e3e19
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CurrentUser.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.cms.auth;
17
18 import java.security.AccessController;
19 import java.security.Principal;
20 import java.security.PrivilegedAction;
21 import java.security.PrivilegedActionException;
22 import java.security.PrivilegedExceptionAction;
23 import java.security.acl.Group;
24 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.UUID;
27
28 import javax.security.auth.Subject;
29 import javax.security.auth.x500.X500Principal;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.argeo.cms.CmsException;
34 import org.argeo.cms.internal.auth.CmsSessionImpl;
35 import org.argeo.eclipse.ui.specific.UiContext;
36 import org.argeo.node.NodeConstants;
37 import org.osgi.service.useradmin.Authorization;
38
39 /**
40 * Programmatic access to the currently authenticated user, within a CMS
41 * context.
42 */
43 public final class CurrentUser {
44 private final static Log log = LogFactory.getLog(CurrentUser.class);
45 // private final static BundleContext bc = FrameworkUtil.getBundle(CurrentUser.class).getBundleContext();
46 /*
47 * CURRENT USER API
48 */
49
50 /**
51 * Technical username of the currently authenticated user.
52 *
53 * @return the authenticated username or null if not authenticated /
54 * anonymous
55 */
56 public static String getUsername() {
57 return getUsername(currentSubject());
58 }
59
60 /**
61 * Human readable name of the currently authenticated user (typically first
62 * name and last name).
63 */
64 public static String getDisplayName() {
65 return getDisplayName(currentSubject());
66 }
67
68 /** Whether a user is currently authenticated. */
69 public static boolean isAnonymous() {
70 return isAnonymous(currentSubject());
71 }
72
73 /** Roles of the currently logged-in user */
74 public final static Set<String> roles() {
75 return roles(currentSubject());
76 }
77
78 /** Returns true if the current user is in the specified role */
79 public static boolean isInRole(String role) {
80 Set<String> roles = roles();
81 return roles.contains(role);
82 }
83
84 /** Executes as the current user */
85 public final static <T> T doAs(PrivilegedAction<T> action) {
86 return Subject.doAs(currentSubject(), action);
87 }
88
89 /** Executes as the current user */
90 public final static <T> T tryAs(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
91 return Subject.doAs(currentSubject(), action);
92 }
93
94 /*
95 * WRAPPERS
96 */
97
98 public final static String getUsername(Subject subject) {
99 if (subject == null)
100 throw new CmsException("Subject cannot be null");
101 if (subject.getPrincipals(X500Principal.class).size() != 1)
102 return NodeConstants.ROLE_ANONYMOUS;
103 Principal principal = subject.getPrincipals(X500Principal.class).iterator().next();
104 return principal.getName();
105 }
106
107 public final static String getDisplayName(Subject subject) {
108 return getAuthorization(subject).toString();
109 }
110
111 public final static Set<String> roles(Subject subject) {
112 Set<String> roles = new HashSet<String>();
113 roles.add(getUsername(subject));
114 for (Principal group : subject.getPrincipals(Group.class)) {
115 roles.add(group.getName());
116 }
117 return roles;
118 }
119
120 /** Whether this user is currently authenticated. */
121 public static boolean isAnonymous(Subject subject) {
122 if (subject == null)
123 return true;
124 String username = getUsername(subject);
125 return username == null || username.equalsIgnoreCase(NodeConstants.ROLE_ANONYMOUS);
126 }
127 /*
128 * HELPERS
129 */
130
131 private static Subject currentSubject() {
132 CmsAuthenticated cmsView = getNodeAuthenticated();
133 if (cmsView != null)
134 return cmsView.getSubject();
135 Subject subject = Subject.getSubject(AccessController.getContext());
136 if (subject != null)
137 return subject;
138 throw new CmsException("Cannot find related subject");
139 }
140
141 /**
142 * The node authenticated component (typically a CMS view) related to this
143 * display, or null if none is available from this call. <b>Not API: Only
144 * for low-level access.</b>
145 */
146 private static CmsAuthenticated getNodeAuthenticated() {
147 return UiContext.getData(CmsAuthenticated.KEY);
148 }
149
150 private static Authorization getAuthorization(Subject subject) {
151 return subject.getPrivateCredentials(Authorization.class).iterator().next();
152 }
153
154 public static boolean logoutCmsSession(Subject subject) {
155 UUID nodeSessionId;
156 if (subject.getPrivateCredentials(CmsSessionId.class).size() == 1)
157 nodeSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next().getUuid();
158 else
159 return false;
160 CmsSessionImpl cmsSession = (CmsSessionImpl) CmsSessionImpl.getByUuid(nodeSessionId.toString());
161 cmsSession.close();
162 // Collection<ServiceReference<CmsSession>> srs;
163 // try {
164 // srs = bc.getServiceReferences(CmsSession.class, "(" +
165 // CmsSession.SESSION_UUID + "=" + nodeSessionId + ")");
166 // } catch (InvalidSyntaxException e) {
167 // throw new CmsException("Cannot retrieve CMS session #" +
168 // nodeSessionId, e);
169 // }
170 //
171 // if (srs.size() == 0) {
172 // // if (log.isTraceEnabled())
173 // // log.warn("No CMS web session found for http session " +
174 // // nodeSessionId);
175 // return false;
176 // } else if (srs.size() > 1)
177 // throw new CmsException(srs.size() + " CMS web sessions found for http
178 // session " + nodeSessionId);
179 //
180 // WebCmsSessionImpl cmsSession = (WebCmsSessionImpl)
181 // bc.getService(srs.iterator().next());
182 // cmsSession.cleanUp();
183 // subject.getPrivateCredentials().removeAll(subject.getPrivateCredentials(CmsSessionId.class));
184 if (log.isDebugEnabled())
185 log.debug("Logged out CMS session " + cmsSession.getUuid());
186 return true;
187 }
188
189 private CurrentUser() {
190 }
191 }