]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java
Improve ACR / JCR integration.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CurrentUser.java
index fa552323c4bfa3d9b65bef79ec34bce5cd7ebc8e..b43bf98b5f707744591b26006535ca784d8252b7 100644 (file)
@@ -9,14 +9,18 @@ import java.util.HashSet;
 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;
 
-import org.argeo.api.NodeConstants;
+import org.argeo.api.cms.CmsConstants;
+import org.argeo.api.cms.CmsSession;
+import org.argeo.api.cms.CmsSessionId;
 import org.argeo.cms.internal.auth.CmsSessionImpl;
 import org.argeo.cms.internal.auth.ImpliedByPrincipal;
-import org.argeo.cms.internal.kernel.Activator;
+import org.argeo.cms.internal.runtime.CmsContextImpl;
 import org.osgi.service.useradmin.Authorization;
 
 /**
@@ -84,7 +88,7 @@ public final class CurrentUser {
                if (subject == null)
                        throw new IllegalArgumentException("Subject cannot be null");
                if (subject.getPrincipals(X500Principal.class).size() != 1)
-                       return NodeConstants.ROLE_ANONYMOUS;
+                       return CmsConstants.ROLE_ANONYMOUS;
                Principal principal = subject.getPrincipals(X500Principal.class).iterator().next();
                return principal.getName();
        }
@@ -105,7 +109,7 @@ public final class CurrentUser {
        public final static Locale locale(Subject subject) {
                Set<Locale> locales = subject.getPublicCredentials(Locale.class);
                if (locales.isEmpty()) {
-                       Locale defaultLocale = Activator.getNodeState().getDefaultLocale();
+                       Locale defaultLocale = CmsContextImpl.getCmsContext().getDefaultLocale();
                        return defaultLocale;
                } else
                        return locales.iterator().next();
@@ -116,13 +120,13 @@ public final class CurrentUser {
                if (subject == null)
                        return true;
                String username = getUsername(subject);
-               return username == null || username.equalsIgnoreCase(NodeConstants.ROLE_ANONYMOUS);
+               return username == null || username.equalsIgnoreCase(CmsConstants.ROLE_ANONYMOUS);
        }
 
-       public CmsSession getCmsSession() {
+       public static CmsSession getCmsSession() {
                Subject subject = currentSubject();
                CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
-               return CmsSessionImpl.getByUuid(cmsSessionId.getUuid());
+               return CmsContextImpl.getCmsContext().getCmsSessionByUuid(cmsSessionId.getUuid());
        }
 
        /*
@@ -149,13 +153,40 @@ public final class CurrentUser {
                        nodeSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next().getUuid();
                else
                        return false;
-               CmsSessionImpl cmsSession = CmsSessionImpl.getByUuid(nodeSessionId.toString());
-               cmsSession.close();
+               CmsSessionImpl cmsSession = CmsContextImpl.getCmsContext().getCmsSessionByUuid(nodeSessionId);
+
+               // FIXME logout all views
+               // TODO check why it is sometimes null
+               if (cmsSession != null)
+                       cmsSession.close();
                // if (log.isDebugEnabled())
                // log.debug("Logged out CMS session " + cmsSession.getUuid());
                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() {
        }
 }