]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CurrentUser.java
b43bf98b5f707744591b26006535ca784d8252b7
[lgpl/argeo-commons.git] / CurrentUser.java
1 package org.argeo.cms.auth;
2
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;
10 import java.util.Set;
11 import java.util.UUID;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.CompletionException;
14
15 import javax.security.auth.Subject;
16 import javax.security.auth.x500.X500Principal;
17
18 import org.argeo.api.cms.CmsConstants;
19 import org.argeo.api.cms.CmsSession;
20 import org.argeo.api.cms.CmsSessionId;
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.osgi.service.useradmin.Authorization;
25
26 /**
27 * Programmatic access to the currently authenticated user, within a CMS
28 * context.
29 */
30 public final class CurrentUser {
31 /*
32 * CURRENT USER API
33 */
34
35 /**
36 * Technical username of the currently authenticated user.
37 *
38 * @return the authenticated username or null if not authenticated / anonymous
39 */
40 public static String getUsername() {
41 return getUsername(currentSubject());
42 }
43
44 /**
45 * Human readable name of the currently authenticated user (typically first name
46 * and last name).
47 */
48 public static String getDisplayName() {
49 return getDisplayName(currentSubject());
50 }
51
52 /** Whether a user is currently authenticated. */
53 public static boolean isAnonymous() {
54 return isAnonymous(currentSubject());
55 }
56
57 /** Locale of the current user */
58 public final static Locale locale() {
59 return locale(currentSubject());
60 }
61
62 /** Roles of the currently logged-in user */
63 public final static Set<String> roles() {
64 return roles(currentSubject());
65 }
66
67 /** Returns true if the current user is in the specified role */
68 public static boolean isInRole(String role) {
69 Set<String> roles = roles();
70 return roles.contains(role);
71 }
72
73 /** Executes as the current user */
74 public final static <T> T doAs(PrivilegedAction<T> action) {
75 return Subject.doAs(currentSubject(), action);
76 }
77
78 /** Executes as the current user */
79 public final static <T> T tryAs(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
80 return Subject.doAs(currentSubject(), action);
81 }
82
83 /*
84 * WRAPPERS
85 */
86
87 public final static String getUsername(Subject subject) {
88 if (subject == null)
89 throw new IllegalArgumentException("Subject cannot be null");
90 if (subject.getPrincipals(X500Principal.class).size() != 1)
91 return CmsConstants.ROLE_ANONYMOUS;
92 Principal principal = subject.getPrincipals(X500Principal.class).iterator().next();
93 return principal.getName();
94 }
95
96 public final static String getDisplayName(Subject subject) {
97 return getAuthorization(subject).toString();
98 }
99
100 public final static Set<String> roles(Subject subject) {
101 Set<String> roles = new HashSet<String>();
102 roles.add(getUsername(subject));
103 for (Principal group : subject.getPrincipals(ImpliedByPrincipal.class)) {
104 roles.add(group.getName());
105 }
106 return roles;
107 }
108
109 public final static Locale locale(Subject subject) {
110 Set<Locale> locales = subject.getPublicCredentials(Locale.class);
111 if (locales.isEmpty()) {
112 Locale defaultLocale = CmsContextImpl.getCmsContext().getDefaultLocale();
113 return defaultLocale;
114 } else
115 return locales.iterator().next();
116 }
117
118 /** Whether this user is currently authenticated. */
119 public static boolean isAnonymous(Subject subject) {
120 if (subject == null)
121 return true;
122 String username = getUsername(subject);
123 return username == null || username.equalsIgnoreCase(CmsConstants.ROLE_ANONYMOUS);
124 }
125
126 public static CmsSession getCmsSession() {
127 Subject subject = currentSubject();
128 CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
129 return CmsContextImpl.getCmsContext().getCmsSessionByUuid(cmsSessionId.getUuid());
130 }
131
132 /*
133 * HELPERS
134 */
135 private static Subject currentSubject() {
136 Subject subject = getAccessControllerSubject();
137 if (subject != null)
138 return subject;
139 throw new IllegalStateException("Cannot find related subject");
140 }
141
142 private static Subject getAccessControllerSubject() {
143 return Subject.getSubject(AccessController.getContext());
144 }
145
146 private static Authorization getAuthorization(Subject subject) {
147 return subject.getPrivateCredentials(Authorization.class).iterator().next();
148 }
149
150 public static boolean logoutCmsSession(Subject subject) {
151 UUID nodeSessionId;
152 if (subject.getPrivateCredentials(CmsSessionId.class).size() == 1)
153 nodeSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next().getUuid();
154 else
155 return false;
156 CmsSessionImpl cmsSession = CmsContextImpl.getCmsContext().getCmsSessionByUuid(nodeSessionId);
157
158 // FIXME logout all views
159 // TODO check why it is sometimes null
160 if (cmsSession != null)
161 cmsSession.close();
162 // if (log.isDebugEnabled())
163 // log.debug("Logged out CMS session " + cmsSession.getUuid());
164 return true;
165 }
166
167 /*
168 * PREPARE EVOLUTION OF JAVA APIs INTRODUCED IN JDK 18
169 * The following static methods will be added to Subject
170 */
171 public Subject current() {
172 return currentSubject();
173 }
174
175 public static <T> T callAs(Subject subject, Callable<T> action) {
176 try {
177 return Subject.doAs(subject, new PrivilegedExceptionAction<T>() {
178
179 @Override
180 public T run() throws Exception {
181 return action.call();
182 }
183
184 });
185 } catch (PrivilegedActionException e) {
186 throw new CompletionException("Failed to execute action for " + subject, e.getCause());
187 }
188 }
189
190 private CurrentUser() {
191 }
192 }