Remove node data model, home areas based on workspaces instead.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CmsSession.java
index 14d6d71f655b54c9a23bc9fd55e1bf0761cbb885..b9798006b4ca040fa3ecb3d543cf27f639dcc967 100644 (file)
@@ -1,34 +1,62 @@
 package org.argeo.cms.auth;
 
+import java.time.ZonedDateTime;
+import java.util.Collection;
+import java.util.Locale;
 import java.util.UUID;
 
-import javax.jcr.Repository;
-import javax.jcr.Session;
 import javax.naming.ldap.LdapName;
+import javax.security.auth.Subject;
 
 import org.argeo.naming.LdapAttrs;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.useradmin.Authorization;
 
+/** An authenticated user session. */
 public interface CmsSession {
-       public final static String USER_DN = LdapAttrs.DN;
-       public final static String SESSION_UUID = LdapAttrs.entryUUID.name();
-       public final static String SESSION_LOCAL_ID = LdapAttrs.uniqueIdentifier.name();
+       final static String USER_DN = LdapAttrs.DN;
+       final static String SESSION_UUID = LdapAttrs.entryUUID.name();
+       final static String SESSION_LOCAL_ID = LdapAttrs.uniqueIdentifier.name();
 
-       // public String getId();
+       UUID getUuid();
 
-       public UUID getUuid();
+       LdapName getUserDn();
 
-       public LdapName getUserDn();
+       String getLocalId();
 
-       public String getLocalId();
+       Authorization getAuthorization();
 
-       public Authorization getAuthorization();
+       boolean isAnonymous();
 
-       public Session getDataSession(String cn, String workspace, Repository repository);
+       ZonedDateTime getCreationTime();
 
-       public void releaseDataSession(String cn, Session session);
+       ZonedDateTime getEnd();
 
-       // public void addHttpSession(HttpServletRequest request);
+       Locale getLocale();
 
-       // public void cleanUp();
+       boolean isValid();
+
+       /** @return The {@link CmsSession} for this {@link Subject} or null. */
+       static CmsSession getCmsSession(BundleContext bc, Subject subject) {
+               if (subject.getPrivateCredentials(CmsSessionId.class).isEmpty())
+                       return null;
+               CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
+               String uuid = cmsSessionId.getUuid().toString();
+               Collection<ServiceReference<CmsSession>> sr;
+               try {
+                       sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_UUID + "=" + uuid + ")");
+               } catch (InvalidSyntaxException e) {
+                       throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid, e);
+               }
+               ServiceReference<CmsSession> cmsSessionRef;
+               if (sr.size() == 1) {
+                       cmsSessionRef = sr.iterator().next();
+                       return bc.getService(cmsSessionRef);
+               } else if (sr.size() == 0) {
+                       return null;
+               } else
+                       throw new IllegalStateException(sr.size() + " CMS sessions registered for " + uuid);
+       }
 }