1 package org
.argeo
.cms
.auth
;
3 import java
.security
.AccessController
;
4 import java
.security
.Principal
;
5 import java
.security
.PrivilegedAction
;
6 import java
.security
.PrivilegedActionException
;
7 import java
.security
.PrivilegedExceptionAction
;
8 import java
.util
.HashSet
;
9 import java
.util
.Locale
;
11 import java
.util
.UUID
;
13 import javax
.security
.auth
.Subject
;
14 import javax
.security
.auth
.x500
.X500Principal
;
16 import org
.argeo
.api
.cms
.CmsConstants
;
17 import org
.argeo
.api
.cms
.CmsSession
;
18 import org
.argeo
.api
.cms
.CmsSessionId
;
19 import org
.argeo
.cms
.internal
.auth
.CmsSessionImpl
;
20 import org
.argeo
.cms
.internal
.auth
.ImpliedByPrincipal
;
21 import org
.argeo
.cms
.internal
.runtime
.CmsContextImpl
;
22 import org
.osgi
.service
.useradmin
.Authorization
;
25 * Programmatic access to the currently authenticated user, within a CMS
28 public final class CurrentUser
{
34 * Technical username of the currently authenticated user.
36 * @return the authenticated username or null if not authenticated / anonymous
38 public static String
getUsername() {
39 return getUsername(currentSubject());
43 * Human readable name of the currently authenticated user (typically first name
46 public static String
getDisplayName() {
47 return getDisplayName(currentSubject());
50 /** Whether a user is currently authenticated. */
51 public static boolean isAnonymous() {
52 return isAnonymous(currentSubject());
55 /** Locale of the current user */
56 public final static Locale
locale() {
57 return locale(currentSubject());
60 /** Roles of the currently logged-in user */
61 public final static Set
<String
> roles() {
62 return roles(currentSubject());
65 /** Returns true if the current user is in the specified role */
66 public static boolean isInRole(String role
) {
67 Set
<String
> roles
= roles();
68 return roles
.contains(role
);
71 /** Executes as the current user */
72 public final static <T
> T
doAs(PrivilegedAction
<T
> action
) {
73 return Subject
.doAs(currentSubject(), action
);
76 /** Executes as the current user */
77 public final static <T
> T
tryAs(PrivilegedExceptionAction
<T
> action
) throws PrivilegedActionException
{
78 return Subject
.doAs(currentSubject(), action
);
85 public final static String
getUsername(Subject subject
) {
87 throw new IllegalArgumentException("Subject cannot be null");
88 if (subject
.getPrincipals(X500Principal
.class).size() != 1)
89 return CmsConstants
.ROLE_ANONYMOUS
;
90 Principal principal
= subject
.getPrincipals(X500Principal
.class).iterator().next();
91 return principal
.getName();
94 public final static String
getDisplayName(Subject subject
) {
95 return getAuthorization(subject
).toString();
98 public final static Set
<String
> roles(Subject subject
) {
99 Set
<String
> roles
= new HashSet
<String
>();
100 roles
.add(getUsername(subject
));
101 for (Principal group
: subject
.getPrincipals(ImpliedByPrincipal
.class)) {
102 roles
.add(group
.getName());
107 public final static Locale
locale(Subject subject
) {
108 Set
<Locale
> locales
= subject
.getPublicCredentials(Locale
.class);
109 if (locales
.isEmpty()) {
110 Locale defaultLocale
= CmsContextImpl
.getCmsContext().getDefaultLocale();
111 return defaultLocale
;
113 return locales
.iterator().next();
116 /** Whether this user is currently authenticated. */
117 public static boolean isAnonymous(Subject subject
) {
120 String username
= getUsername(subject
);
121 return username
== null || username
.equalsIgnoreCase(CmsConstants
.ROLE_ANONYMOUS
);
124 public CmsSession
getCmsSession() {
125 Subject subject
= currentSubject();
126 CmsSessionId cmsSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next();
127 return CmsSessionImpl
.getByUuid(cmsSessionId
.getUuid());
133 private static Subject
currentSubject() {
134 Subject subject
= getAccessControllerSubject();
137 throw new IllegalStateException("Cannot find related subject");
140 private static Subject
getAccessControllerSubject() {
141 return Subject
.getSubject(AccessController
.getContext());
144 private static Authorization
getAuthorization(Subject subject
) {
145 return subject
.getPrivateCredentials(Authorization
.class).iterator().next();
148 public static boolean logoutCmsSession(Subject subject
) {
150 if (subject
.getPrivateCredentials(CmsSessionId
.class).size() == 1)
151 nodeSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next().getUuid();
154 CmsSessionImpl cmsSession
= CmsSessionImpl
.getByUuid(nodeSessionId
.toString());
156 // FIXME logout all views
157 // TODO check why it is sometimes null
158 if (cmsSession
!= null)
160 // if (log.isDebugEnabled())
161 // log.debug("Logged out CMS session " + cmsSession.getUuid());
165 private CurrentUser() {