]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/CmsContentRepository.java
Prepare next development cycle
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / CmsContentRepository.java
1 package org.argeo.cms.acr;
2
3 import java.util.Collections;
4 import java.util.HashMap;
5 import java.util.Locale;
6 import java.util.Map;
7
8 import javax.security.auth.login.LoginContext;
9 import javax.security.auth.login.LoginException;
10
11 import org.argeo.api.acr.ContentSession;
12 import org.argeo.api.acr.spi.ProvidedRepository;
13 import org.argeo.api.cms.CmsAuth;
14 import org.argeo.api.cms.CmsSession;
15 import org.argeo.api.cms.CmsState;
16 import org.argeo.api.cms.DataAdminPrincipal;
17 import org.argeo.api.uuid.UuidFactory;
18 import org.argeo.cms.CurrentUser;
19 import org.argeo.cms.internal.runtime.CmsContextImpl;
20 import org.argeo.cms.util.CurrentSubject;
21
22 /**
23 * Multi-session {@link ProvidedRepository}, integrated with a CMS.
24 */
25 public class CmsContentRepository extends AbstractContentRepository {
26 public final static String RUN_BASE = "/run";
27 public final static String DIRECTORY_BASE = "/directory";
28
29 private Map<CmsSession, CmsContentSession> userSessions = Collections.synchronizedMap(new HashMap<>());
30
31 private CmsState cmsState;
32 private UuidFactory uuidFactory;
33
34 /*
35 * REPOSITORY
36 */
37
38 @Override
39 public ContentSession get() {
40 return get(CmsContextImpl.getCmsContext().getDefaultLocale());
41 }
42
43 @Override
44 public ContentSession get(Locale locale) {
45 if (!CmsSession.hasCmsSession(CurrentSubject.current())) {
46 if (DataAdminPrincipal.isDataAdmin(CurrentSubject.current())) {
47 // TODO open multiple data admin sessions?
48 return getSystemSession();
49 }
50 throw new IllegalStateException("Caller must be authenticated");
51 }
52
53 CmsSession cmsSession = CurrentUser.getCmsSession();
54 CmsContentSession contentSession = userSessions.get(cmsSession);
55 if (contentSession == null) {
56 final CmsContentSession newContentSession = new CmsContentSession(this, cmsSession.getUuid(),
57 cmsSession.getSubject(), locale, uuidFactory);
58 cmsSession.addOnCloseCallback((c) -> {
59 newContentSession.close();
60 userSessions.remove(cmsSession);
61 });
62 contentSession = newContentSession;
63 }
64 return contentSession;
65 }
66
67 @Override
68 protected CmsContentSession newSystemSession() {
69 LoginContext loginContext;
70 try {
71 loginContext = new LoginContext(CmsAuth.DATA_ADMIN.getLoginContextName());
72 loginContext.login();
73 } catch (LoginException e1) {
74 throw new RuntimeException("Could not login as data admin", e1);
75 } finally {
76 }
77 return new CmsContentSession(this, getCmsState().getUuid(), loginContext.getSubject(), Locale.getDefault(),
78 uuidFactory);
79 }
80
81 protected CmsState getCmsState() {
82 return cmsState;
83 }
84
85 public void setCmsState(CmsState cmsState) {
86 this.cmsState = cmsState;
87 }
88
89 public void setUuidFactory(UuidFactory uuidFactory) {
90 this.uuidFactory = uuidFactory;
91 }
92
93 }