]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/CmsSession.java
Store UI context data in CMS View.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CmsSession.java
1 package org.argeo.cms.auth;
2
3 import java.time.ZonedDateTime;
4 import java.util.Collection;
5 import java.util.Locale;
6 import java.util.UUID;
7
8 import javax.naming.ldap.LdapName;
9 import javax.security.auth.Subject;
10
11 import org.argeo.naming.LdapAttrs;
12 import org.osgi.framework.BundleContext;
13 import org.osgi.framework.InvalidSyntaxException;
14 import org.osgi.framework.ServiceReference;
15 import org.osgi.service.useradmin.Authorization;
16
17 /** An authenticated user session. */
18 public interface CmsSession {
19 final static String USER_DN = LdapAttrs.DN;
20 final static String SESSION_UUID = LdapAttrs.entryUUID.name();
21 final static String SESSION_LOCAL_ID = LdapAttrs.uniqueIdentifier.name();
22
23 UUID getUuid();
24
25 String getUserRole();
26
27 LdapName getUserDn();
28
29 String getLocalId();
30
31 Authorization getAuthorization();
32
33 boolean isAnonymous();
34
35 ZonedDateTime getCreationTime();
36
37 ZonedDateTime getEnd();
38
39 Locale getLocale();
40
41 boolean isValid();
42
43 void registerView(String uid, Object view);
44
45 /** @return The {@link CmsSession} for this {@link Subject} or null. */
46 static CmsSession getCmsSession(BundleContext bc, Subject subject) {
47 if (subject.getPrivateCredentials(CmsSessionId.class).isEmpty())
48 return null;
49 CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
50 String uuid = cmsSessionId.getUuid().toString();
51 Collection<ServiceReference<CmsSession>> sr;
52 try {
53 sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_UUID + "=" + uuid + ")");
54 } catch (InvalidSyntaxException e) {
55 throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid, e);
56 }
57 ServiceReference<CmsSession> cmsSessionRef;
58 if (sr.size() == 1) {
59 cmsSessionRef = sr.iterator().next();
60 return bc.getService(cmsSessionRef);
61 } else if (sr.size() == 0) {
62 return null;
63 } else
64 throw new IllegalStateException(sr.size() + " CMS sessions registered for " + uuid);
65 }
66 }