]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java
Introduce hierarchies in user management
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CurrentUser.java
index 86a748325ec0e0a553ab3e46d2be403a59fe5c73..faf5555d0a61ef31ee06c7a79412e516ad7d67dd 100644 (file)
@@ -6,9 +6,12 @@ import java.security.PrivilegedAction;
 import java.security.PrivilegedActionException;
 import java.security.PrivilegedExceptionAction;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Locale;
 import java.util.Set;
 import java.util.UUID;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CompletionException;
 
 import javax.security.auth.Subject;
 import javax.security.auth.x500.X500Principal;
@@ -123,8 +126,13 @@ public final class CurrentUser {
 
        public static CmsSession getCmsSession() {
                Subject subject = currentSubject();
-               CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
-               return CmsSessionImpl.getByUuid(cmsSessionId.getUuid());
+               Iterator<CmsSessionId> it = subject.getPrivateCredentials(CmsSessionId.class).iterator();
+               if (!it.hasNext())
+                       throw new IllegalStateException("No CMS session id available for " + subject);
+               CmsSessionId cmsSessionId = it.next();
+               if (it.hasNext())
+                       throw new IllegalStateException("More than one CMS session id available for " + subject);
+               return CmsContextImpl.getCmsContext().getCmsSessionByUuid(cmsSessionId.getUuid());
        }
 
        /*
@@ -151,7 +159,7 @@ public final class CurrentUser {
                        nodeSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next().getUuid();
                else
                        return false;
-               CmsSessionImpl cmsSession = CmsSessionImpl.getByUuid(nodeSessionId.toString());
+               CmsSessionImpl cmsSession = CmsContextImpl.getCmsContext().getCmsSessionByUuid(nodeSessionId);
 
                // FIXME logout all views
                // TODO check why it is sometimes null
@@ -162,6 +170,29 @@ public final class CurrentUser {
                return true;
        }
 
+       /*
+        * PREPARE EVOLUTION OF JAVA APIs INTRODUCED IN JDK 18 The following static
+        * methods will be added to Subject
+        */
+       public Subject current() {
+               return currentSubject();
+       }
+
+       public static <T> T callAs(Subject subject, Callable<T> action) {
+               try {
+                       return Subject.doAs(subject, new PrivilegedExceptionAction<T>() {
+
+                               @Override
+                               public T run() throws Exception {
+                                       return action.call();
+                               }
+
+                       });
+               } catch (PrivilegedActionException e) {
+                       throw new CompletionException("Failed to execute action for " + subject, e.getCause());
+               }
+       }
+
        private CurrentUser() {
        }
 }