1 package org
.argeo
.cms
.auth
;
3 import java
.security
.Principal
;
4 import java
.security
.PrivilegedAction
;
5 import java
.security
.PrivilegedActionException
;
6 import java
.security
.PrivilegedExceptionAction
;
7 import java
.util
.HashSet
;
8 import java
.util
.Iterator
;
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
.acr
.NamespaceUtils
;
17 import org
.argeo
.api
.cms
.CmsConstants
;
18 import org
.argeo
.api
.cms
.CmsSession
;
19 import org
.argeo
.api
.cms
.CmsSessionId
;
20 import org
.argeo
.cms
.SystemRole
;
21 import org
.argeo
.cms
.internal
.auth
.CmsSessionImpl
;
22 import org
.argeo
.cms
.internal
.auth
.ImpliedByPrincipal
;
23 import org
.argeo
.cms
.internal
.runtime
.CmsContextImpl
;
24 import org
.argeo
.cms
.util
.CurrentSubject
;
25 import org
.osgi
.service
.useradmin
.Authorization
;
28 * Programmatic access to the currently authenticated user, within a CMS
31 public final class CurrentUser
{
37 * Technical username of the currently authenticated user.
39 * @return the authenticated username or null if not authenticated / anonymous
41 public static String
getUsername() {
42 return getUsername(currentSubject());
46 * Human readable name of the currently authenticated user (typically first name
49 public static String
getDisplayName() {
50 return getDisplayName(currentSubject());
53 /** Whether a user is currently authenticated. */
54 public static boolean isAnonymous() {
55 return isAnonymous(currentSubject());
58 /** Locale of the current user */
59 public final static Locale
locale() {
60 return locale(currentSubject());
63 /** Roles of the currently logged-in user */
64 public final static Set
<String
> roles() {
65 return roles(currentSubject());
68 /** Returns true if the current user is in the specified role */
69 public static boolean isInRole(String role
) {
70 Set
<String
> roles
= roles();
71 return roles
.contains(role
);
74 /** Implies this {@link SystemRole} in this context. */
75 public final static boolean implies(SystemRole role
, String context
) {
76 return role
.implied(currentSubject(), context
);
79 /** Implies this role name, also independently of the context. */
80 public final static boolean implies(String role
, String context
) {
81 return SystemRole
.implied(NamespaceUtils
.parsePrefixedName(role
), currentSubject(), context
);
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 IllegalArgumentException("Subject cannot be null");
101 if (subject
.getPrincipals(X500Principal
.class).size() != 1)
102 return CmsConstants
.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(ImpliedByPrincipal
.class)) {
115 roles
.add(group
.getName());
120 public final static Locale
locale(Subject subject
) {
121 Set
<Locale
> locales
= subject
.getPublicCredentials(Locale
.class);
122 if (locales
.isEmpty()) {
123 Locale defaultLocale
= CmsContextImpl
.getCmsContext().getDefaultLocale();
124 return defaultLocale
;
126 return locales
.iterator().next();
129 /** Whether this user is currently authenticated. */
130 public static boolean isAnonymous(Subject subject
) {
133 String username
= getUsername(subject
);
134 return username
== null || username
.equalsIgnoreCase(CmsConstants
.ROLE_ANONYMOUS
);
137 public static CmsSession
getCmsSession() {
138 Subject subject
= currentSubject();
139 Iterator
<CmsSessionId
> it
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator();
141 throw new IllegalStateException("No CMS session id available for " + subject
);
142 CmsSessionId cmsSessionId
= it
.next();
144 throw new IllegalStateException("More than one CMS session id available for " + subject
);
145 return CmsContextImpl
.getCmsContext().getCmsSessionByUuid(cmsSessionId
.getUuid());
148 public static boolean isAvailable() {
149 return CurrentSubject
.current() != null;
155 private static Subject
currentSubject() {
156 Subject subject
= CurrentSubject
.current();
158 throw new IllegalStateException("Cannot find related subject");
162 private static Authorization
getAuthorization(Subject subject
) {
163 return subject
.getPrivateCredentials(Authorization
.class).iterator().next();
166 public static boolean logoutCmsSession(Subject subject
) {
168 if (subject
.getPrivateCredentials(CmsSessionId
.class).size() == 1)
169 nodeSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next().getUuid();
172 CmsSessionImpl cmsSession
= CmsContextImpl
.getCmsContext().getCmsSessionByUuid(nodeSessionId
);
174 // FIXME logout all views
175 // TODO check why it is sometimes null
176 if (cmsSession
!= null)
178 // if (log.isDebugEnabled())
179 // log.debug("Logged out CMS session " + cmsSession.getUuid());
184 private CurrentUser() {