X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fauth%2FCurrentUser.java;h=68ea5ff28243543112cbd586e5dbdd467b412acb;hb=dca2b13e0e3ca3e7a9469e089b980c48c880ad1a;hp=16ac638c107a943422e84a059b0a79414f68e54f;hpb=eb4324be6ac9cdff15828a21ee7d3f6ca2f19fb9;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java b/org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java index 16ac638c1..68ea5ff28 100644 --- a/org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java +++ b/org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java @@ -1,6 +1,5 @@ package org.argeo.cms.auth; -import java.security.AccessController; import java.security.Principal; import java.security.PrivilegedAction; import java.security.PrivilegedActionException; @@ -10,8 +9,6 @@ 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; @@ -22,6 +19,7 @@ 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.runtime.CmsContextImpl; +import org.argeo.util.CurrentSubject; import org.osgi.service.useradmin.Authorization; /** @@ -140,18 +138,18 @@ public final class CurrentUser { return CmsContextImpl.getCmsContext().getCmsSessionByUuid(cmsSessionId.getUuid()); } + public static boolean isAvailable() { + return CurrentSubject.current() != null; + } + /* * HELPERS */ private static Subject currentSubject() { - Subject subject = getAccessControllerSubject(); - if (subject != null) - return subject; - throw new IllegalStateException("Cannot find related subject"); - } - - private static Subject getAccessControllerSubject() { - return Subject.getSubject(AccessController.getContext()); + Subject subject = CurrentSubject.current(); + if (subject == null) + throw new IllegalStateException("Cannot find related subject"); + return subject; } private static Authorization getAuthorization(Subject subject) { @@ -175,29 +173,7 @@ 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 callAs(Subject subject, Callable action) { - try { - return Subject.doAs(subject, new PrivilegedExceptionAction() { - - @Override - public T run() throws Exception { - return action.call(); - } - - }); - } catch (PrivilegedActionException e) { - throw new CompletionException("Failed to execute action for " + subject, e.getCause()); - } - } - + /** singleton */ private CurrentUser() { } }