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
.NodeConstants
;
17 import org
.argeo
.cms
.internal
.auth
.CmsSessionImpl
;
18 import org
.argeo
.cms
.internal
.auth
.ImpliedByPrincipal
;
19 import org
.argeo
.cms
.internal
.kernel
.Activator
;
20 import org
.osgi
.service
.useradmin
.Authorization
;
23 * Programmatic access to the currently authenticated user, within a CMS
26 public final class CurrentUser
{
32 * Technical username of the currently authenticated user.
34 * @return the authenticated username or null if not authenticated / anonymous
36 public static String
getUsername() {
37 return getUsername(currentSubject());
41 * Human readable name of the currently authenticated user (typically first name
44 public static String
getDisplayName() {
45 return getDisplayName(currentSubject());
48 /** Whether a user is currently authenticated. */
49 public static boolean isAnonymous() {
50 return isAnonymous(currentSubject());
53 /** Locale of the current user */
54 public final static Locale
locale() {
55 return locale(currentSubject());
58 /** Roles of the currently logged-in user */
59 public final static Set
<String
> roles() {
60 return roles(currentSubject());
63 /** Returns true if the current user is in the specified role */
64 public static boolean isInRole(String role
) {
65 Set
<String
> roles
= roles();
66 return roles
.contains(role
);
69 /** Executes as the current user */
70 public final static <T
> T
doAs(PrivilegedAction
<T
> action
) {
71 return Subject
.doAs(currentSubject(), action
);
74 /** Executes as the current user */
75 public final static <T
> T
tryAs(PrivilegedExceptionAction
<T
> action
) throws PrivilegedActionException
{
76 return Subject
.doAs(currentSubject(), action
);
83 public final static String
getUsername(Subject subject
) {
85 throw new IllegalArgumentException("Subject cannot be null");
86 if (subject
.getPrincipals(X500Principal
.class).size() != 1)
87 return NodeConstants
.ROLE_ANONYMOUS
;
88 Principal principal
= subject
.getPrincipals(X500Principal
.class).iterator().next();
89 return principal
.getName();
92 public final static String
getDisplayName(Subject subject
) {
93 return getAuthorization(subject
).toString();
96 public final static Set
<String
> roles(Subject subject
) {
97 Set
<String
> roles
= new HashSet
<String
>();
98 roles
.add(getUsername(subject
));
99 for (Principal group
: subject
.getPrincipals(ImpliedByPrincipal
.class)) {
100 roles
.add(group
.getName());
105 public final static Locale
locale(Subject subject
) {
106 Set
<Locale
> locales
= subject
.getPublicCredentials(Locale
.class);
107 if (locales
.isEmpty()) {
108 Locale defaultLocale
= Activator
.getNodeState().getDefaultLocale();
109 return defaultLocale
;
111 return locales
.iterator().next();
114 /** Whether this user is currently authenticated. */
115 public static boolean isAnonymous(Subject subject
) {
118 String username
= getUsername(subject
);
119 return username
== null || username
.equalsIgnoreCase(NodeConstants
.ROLE_ANONYMOUS
);
122 public CmsSession
getCmsSession() {
123 Subject subject
= currentSubject();
124 CmsSessionId cmsSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next();
125 return CmsSessionImpl
.getByUuid(cmsSessionId
.getUuid());
131 private static Subject
currentSubject() {
132 Subject subject
= getAccessControllerSubject();
135 throw new IllegalStateException("Cannot find related subject");
138 private static Subject
getAccessControllerSubject() {
139 return Subject
.getSubject(AccessController
.getContext());
142 private static Authorization
getAuthorization(Subject subject
) {
143 return subject
.getPrivateCredentials(Authorization
.class).iterator().next();
146 public static boolean logoutCmsSession(Subject subject
) {
148 if (subject
.getPrivateCredentials(CmsSessionId
.class).size() == 1)
149 nodeSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next().getUuid();
152 CmsSessionImpl cmsSession
= CmsSessionImpl
.getByUuid(nodeSessionId
.toString());
154 // FIXME logout all views
155 // TODO check why it is sometimes null
156 if (cmsSession
!= null)
158 // if (log.isDebugEnabled())
159 // log.debug("Logged out CMS session " + cmsSession.getUuid());
163 private CurrentUser() {