X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fauth%2FCurrentUser.java;h=faf5555d0a61ef31ee06c7a79412e516ad7d67dd;hb=e168383bac50637131fef8c41e119db7eb2284a7;hp=afb6360b476450401ad9fa06f638f01063f51558;hpb=b71546ddc74d6ca49d252806aafd491c75dfe1fb;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 afb6360b4..faf5555d0 100644 --- a/org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java +++ b/org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java @@ -6,19 +6,22 @@ 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; +import org.argeo.api.cms.CmsConstants; import org.argeo.api.cms.CmsSession; import org.argeo.api.cms.CmsSessionId; -import org.argeo.api.cms.CmsConstants; 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; /** @@ -107,7 +110,7 @@ public final class CurrentUser { public final static Locale locale(Subject subject) { Set 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(); @@ -121,10 +124,15 @@ public final class CurrentUser { 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()); + Iterator 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,8 +159,8 @@ 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 if (cmsSession != 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 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()); + } + } + private CurrentUser() { } }