2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.cms
.auth
;
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
;
26 import java
.util
.UUID
;
28 import javax
.security
.auth
.Subject
;
29 import javax
.security
.auth
.x500
.X500Principal
;
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
;
40 * Programmatic access to the currently authenticated user, within a CMS
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();
51 * Technical username of the currently authenticated user.
53 * @return the authenticated username or null if not authenticated /
56 public static String
getUsername() {
57 return getUsername(currentSubject());
61 * Human readable name of the currently authenticated user (typically first
62 * name and last name).
64 public static String
getDisplayName() {
65 return getDisplayName(currentSubject());
68 /** Whether a user is currently authenticated. */
69 public static boolean isAnonymous() {
70 return isAnonymous(currentSubject());
73 /** Roles of the currently logged-in user */
74 public final static Set
<String
> roles() {
75 return roles(currentSubject());
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
);
84 /** Executes as the current user */
85 public final static <T
> T
doAs(PrivilegedAction
<T
> action
) {
86 return Subject
.doAs(currentSubject(), action
);
89 /** Executes as the current user */
90 public final static <T
> T
tryAs(PrivilegedExceptionAction
<T
> action
) throws PrivilegedActionException
{
91 return Subject
.doAs(currentSubject(), action
);
98 public final static String
getUsername(Subject subject
) {
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();
107 public final static String
getDisplayName(Subject subject
) {
108 return getAuthorization(subject
).toString();
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());
120 /** Whether this user is currently authenticated. */
121 public static boolean isAnonymous(Subject subject
) {
124 String username
= getUsername(subject
);
125 return username
== null || username
.equalsIgnoreCase(NodeConstants
.ROLE_ANONYMOUS
);
131 private static Subject
currentSubject() {
132 CmsAuthenticated cmsView
= getNodeAuthenticated();
134 return cmsView
.getSubject();
135 Subject subject
= Subject
.getSubject(AccessController
.getContext());
138 throw new CmsException("Cannot find related subject");
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>
146 private static CmsAuthenticated
getNodeAuthenticated() {
147 return UiContext
.getData(CmsAuthenticated
.KEY
);
150 private static Authorization
getAuthorization(Subject subject
) {
151 return subject
.getPrivateCredentials(Authorization
.class).iterator().next();
154 public static boolean logoutCmsSession(Subject subject
) {
156 if (subject
.getPrivateCredentials(CmsSessionId
.class).size() == 1)
157 nodeSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next().getUuid();
160 CmsSessionImpl cmsSession
= (CmsSessionImpl
) CmsSessionImpl
.getByUuid(nodeSessionId
.toString());
162 // Collection<ServiceReference<CmsSession>> srs;
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);
171 // if (srs.size() == 0) {
172 // // if (log.isTraceEnabled())
173 // // log.warn("No CMS web session found for http session " +
174 // // nodeSessionId);
176 // } else if (srs.size() > 1)
177 // throw new CmsException(srs.size() + " CMS web sessions found for http
178 // session " + nodeSessionId);
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());
189 private CurrentUser() {