]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java
Introduce system roles
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CurrentUser.java
index b43bf98b5f707744591b26006535ca784d8252b7..16ac638c107a943422e84a059b0a79414f68e54f 100644 (file)
@@ -6,6 +6,7 @@ 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;
@@ -70,6 +71,11 @@ public final class CurrentUser {
                return roles.contains(role);
        }
 
+       /** Implies this {@link SystemRole} in this context. */
+       public final static boolean implies(SystemRole role, String context) {
+               return role.implied(currentSubject(), context);
+       }
+
        /** Executes as the current user */
        public final static <T> T doAs(PrivilegedAction<T> action) {
                return Subject.doAs(currentSubject(), action);
@@ -125,7 +131,12 @@ public final class CurrentUser {
 
        public static CmsSession getCmsSession() {
                Subject subject = currentSubject();
-               CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
+               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());
        }
 
@@ -165,8 +176,8 @@ public final class CurrentUser {
        }
 
        /*
-        * PREPARE EVOLUTION OF JAVA APIs INTRODUCED IN JDK 18
-        * The following static methods will be added to Subject 
+        * PREPARE EVOLUTION OF JAVA APIs INTRODUCED IN JDK 18 The following static
+        * methods will be added to Subject
         */
        public Subject current() {
                return currentSubject();