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
.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
.argeo
.util
.CurrentSubject
;
23 import org
.osgi
.service
.useradmin
.Authorization
;
26 * Programmatic access to the currently authenticated user, within a CMS
29 public final class CurrentUser
{
35 * Technical username of the currently authenticated user.
37 * @return the authenticated username or null if not authenticated / anonymous
39 public static String
getUsername() {
40 return getUsername(currentSubject());
44 * Human readable name of the currently authenticated user (typically first name
47 public static String
getDisplayName() {
48 return getDisplayName(currentSubject());
51 /** Whether a user is currently authenticated. */
52 public static boolean isAnonymous() {
53 return isAnonymous(currentSubject());
56 /** Locale of the current user */
57 public final static Locale
locale() {
58 return locale(currentSubject());
61 /** Roles of the currently logged-in user */
62 public final static Set
<String
> roles() {
63 return roles(currentSubject());
66 /** Returns true if the current user is in the specified role */
67 public static boolean isInRole(String role
) {
68 Set
<String
> roles
= roles();
69 return roles
.contains(role
);
72 /** Implies this {@link SystemRole} in this context. */
73 public final static boolean implies(SystemRole role
, String context
) {
74 return role
.implied(currentSubject(), context
);
77 /** Executes as the current user */
78 public final static <T
> T
doAs(PrivilegedAction
<T
> action
) {
79 return Subject
.doAs(currentSubject(), action
);
82 /** Executes as the current user */
83 public final static <T
> T
tryAs(PrivilegedExceptionAction
<T
> action
) throws PrivilegedActionException
{
84 return Subject
.doAs(currentSubject(), action
);
91 public final static String
getUsername(Subject subject
) {
93 throw new IllegalArgumentException("Subject cannot be null");
94 if (subject
.getPrincipals(X500Principal
.class).size() != 1)
95 return CmsConstants
.ROLE_ANONYMOUS
;
96 Principal principal
= subject
.getPrincipals(X500Principal
.class).iterator().next();
97 return principal
.getName();
100 public final static String
getDisplayName(Subject subject
) {
101 return getAuthorization(subject
).toString();
104 public final static Set
<String
> roles(Subject subject
) {
105 Set
<String
> roles
= new HashSet
<String
>();
106 roles
.add(getUsername(subject
));
107 for (Principal group
: subject
.getPrincipals(ImpliedByPrincipal
.class)) {
108 roles
.add(group
.getName());
113 public final static Locale
locale(Subject subject
) {
114 Set
<Locale
> locales
= subject
.getPublicCredentials(Locale
.class);
115 if (locales
.isEmpty()) {
116 Locale defaultLocale
= CmsContextImpl
.getCmsContext().getDefaultLocale();
117 return defaultLocale
;
119 return locales
.iterator().next();
122 /** Whether this user is currently authenticated. */
123 public static boolean isAnonymous(Subject subject
) {
126 String username
= getUsername(subject
);
127 return username
== null || username
.equalsIgnoreCase(CmsConstants
.ROLE_ANONYMOUS
);
130 public static CmsSession
getCmsSession() {
131 Subject subject
= currentSubject();
132 Iterator
<CmsSessionId
> it
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator();
134 throw new IllegalStateException("No CMS session id available for " + subject
);
135 CmsSessionId cmsSessionId
= it
.next();
137 throw new IllegalStateException("More than one CMS session id available for " + subject
);
138 return CmsContextImpl
.getCmsContext().getCmsSessionByUuid(cmsSessionId
.getUuid());
144 private static Subject
currentSubject() {
145 return CurrentSubject
.current();
148 private static Authorization
getAuthorization(Subject subject
) {
149 return subject
.getPrivateCredentials(Authorization
.class).iterator().next();
152 public static boolean logoutCmsSession(Subject subject
) {
154 if (subject
.getPrivateCredentials(CmsSessionId
.class).size() == 1)
155 nodeSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next().getUuid();
158 CmsSessionImpl cmsSession
= CmsContextImpl
.getCmsContext().getCmsSessionByUuid(nodeSessionId
);
160 // FIXME logout all views
161 // TODO check why it is sometimes null
162 if (cmsSession
!= null)
164 // if (log.isDebugEnabled())
165 // log.debug("Logged out CMS session " + cmsSession.getUuid());
170 private CurrentUser() {