]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/auth/CurrentUser.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / auth / CurrentUser.java
1 package org.argeo.cms.auth;
2
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;
10 import java.util.Set;
11 import java.util.UUID;
12
13 import javax.security.auth.Subject;
14 import javax.security.auth.x500.X500Principal;
15
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;
26
27 /**
28 * Programmatic access to the currently authenticated user, within a CMS
29 * context.
30 */
31 public final class CurrentUser {
32 /*
33 * CURRENT USER API
34 */
35
36 /**
37 * Technical username of the currently authenticated user.
38 *
39 * @return the authenticated username or null if not authenticated / anonymous
40 */
41 public static String getUsername() {
42 return getUsername(currentSubject());
43 }
44
45 /**
46 * Human readable name of the currently authenticated user (typically first name
47 * and last name).
48 */
49 public static String getDisplayName() {
50 return getDisplayName(currentSubject());
51 }
52
53 /** Whether a user is currently authenticated. */
54 public static boolean isAnonymous() {
55 return isAnonymous(currentSubject());
56 }
57
58 /** Locale of the current user */
59 public final static Locale locale() {
60 return locale(currentSubject());
61 }
62
63 /** Roles of the currently logged-in user */
64 public final static Set<String> roles() {
65 return roles(currentSubject());
66 }
67
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);
72 }
73
74 /** Implies this {@link SystemRole} in this context. */
75 public final static boolean implies(SystemRole role, String context) {
76 return role.implied(currentSubject(), context);
77 }
78
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);
82 }
83
84 /** Executes as the current user */
85 public final static <T> T doAs(PrivilegedAction<T> action) {
86 return Subject.doAs(currentSubject(), action);
87 }
88
89 /** Executes as the current user */
90 public final static <T> T tryAs(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
91 return Subject.doAs(currentSubject(), action);
92 }
93
94 /*
95 * WRAPPERS
96 */
97
98 public final static String getUsername(Subject subject) {
99 if (subject == null)
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();
105 }
106
107 public final static String getDisplayName(Subject subject) {
108 return getAuthorization(subject).toString();
109 }
110
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());
116 }
117 return roles;
118 }
119
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;
125 } else
126 return locales.iterator().next();
127 }
128
129 /** Whether this user is currently authenticated. */
130 public static boolean isAnonymous(Subject subject) {
131 if (subject == null)
132 return true;
133 String username = getUsername(subject);
134 return username == null || username.equalsIgnoreCase(CmsConstants.ROLE_ANONYMOUS);
135 }
136
137 public static CmsSession getCmsSession() {
138 Subject subject = currentSubject();
139 Iterator<CmsSessionId> it = subject.getPrivateCredentials(CmsSessionId.class).iterator();
140 if (!it.hasNext())
141 throw new IllegalStateException("No CMS session id available for " + subject);
142 CmsSessionId cmsSessionId = it.next();
143 if (it.hasNext())
144 throw new IllegalStateException("More than one CMS session id available for " + subject);
145 return CmsContextImpl.getCmsContext().getCmsSessionByUuid(cmsSessionId.getUuid());
146 }
147
148 public static boolean isAvailable() {
149 return CurrentSubject.current() != null;
150 }
151
152 /*
153 * HELPERS
154 */
155 private static Subject currentSubject() {
156 Subject subject = CurrentSubject.current();
157 if (subject == null)
158 throw new IllegalStateException("Cannot find related subject");
159 return subject;
160 }
161
162 private static Authorization getAuthorization(Subject subject) {
163 return subject.getPrivateCredentials(Authorization.class).iterator().next();
164 }
165
166 public static boolean logoutCmsSession(Subject subject) {
167 UUID nodeSessionId;
168 if (subject.getPrivateCredentials(CmsSessionId.class).size() == 1)
169 nodeSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next().getUuid();
170 else
171 return false;
172 CmsSessionImpl cmsSession = CmsContextImpl.getCmsContext().getCmsSessionByUuid(nodeSessionId);
173
174 // FIXME logout all views
175 // TODO check why it is sometimes null
176 if (cmsSession != null)
177 cmsSession.close();
178 // if (log.isDebugEnabled())
179 // log.debug("Logged out CMS session " + cmsSession.getUuid());
180 return true;
181 }
182
183 /** singleton */
184 private CurrentUser() {
185 }
186 }