From: Mathieu Baudier Date: Wed, 29 Jun 2022 06:28:11 +0000 (+0200) Subject: Introduce CurrentSubject X-Git-Tag: v2.3.10~151 X-Git-Url: https://git.argeo.org/?a=commitdiff_plain;h=1079048d9b6dc6003169327ea77eb64029283ecb;p=lgpl%2Fargeo-commons.git Introduce CurrentSubject --- diff --git a/org.argeo.cms.ux/src/org/argeo/cms/ux/CmsUxUtils.java b/org.argeo.cms.ux/src/org/argeo/cms/ux/CmsUxUtils.java index 015f35db1..d6fd221a4 100644 --- a/org.argeo.cms.ux/src/org/argeo/cms/ux/CmsUxUtils.java +++ b/org.argeo.cms.ux/src/org/argeo/cms/ux/CmsUxUtils.java @@ -4,11 +4,11 @@ import org.argeo.api.acr.Content; import org.argeo.api.acr.ContentRepository; import org.argeo.api.acr.ContentSession; import org.argeo.api.cms.ux.CmsView; -import org.argeo.cms.auth.CurrentUser; +import org.argeo.util.CurrentSubject; public class CmsUxUtils { public static ContentSession getContentSession(ContentRepository contentRepository, CmsView cmsView) { - return CurrentUser.callAs(cmsView.getCmsSession().getSubject(), () -> contentRepository.get()); + return CurrentSubject.callAs(cmsView.getCmsSession().getSubject(), () -> contentRepository.get()); } public static String getTitle(Content content) { 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..13b54a549 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; /** @@ -144,14 +142,7 @@ public final class CurrentUser { * 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()); + return CurrentSubject.current(); } private static Authorization getAuthorization(Subject subject) { @@ -175,29 +166,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() { } } diff --git a/org.argeo.util/src/org/argeo/util/CurrentSubject.java b/org.argeo.util/src/org/argeo/util/CurrentSubject.java new file mode 100644 index 000000000..d7627629f --- /dev/null +++ b/org.argeo.util/src/org/argeo/util/CurrentSubject.java @@ -0,0 +1,44 @@ +package org.argeo.util; + +import java.security.AccessController; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.concurrent.Callable; +import java.util.concurrent.CompletionException; + +import javax.security.auth.Subject; + +/** + * Prepare evotion of Java APIs introduced in JDK 18, as these static methods + * will be added to {@link Subject}. + */ +@SuppressWarnings("removal") +public class CurrentSubject { + + /** Singleton. */ + private CurrentSubject() { + + } + + public static Subject current() { + Subject subject = Subject.getSubject(AccessController.getContext()); + if (subject == null) + throw new IllegalStateException("Cannot find related subject"); + return subject; + } + + 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()); + } + } +}