]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsContentRepository.java
c2d6b21e406f61a5930e43f235de6fa8e7c2e42b
[lgpl/argeo-commons.git] / 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.cms.auth.CurrentUser;
16 import org.argeo.cms.internal.runtime.CmsContextImpl;
17
18 /**
19 * Multi-session {@link ProvidedRepository}, integrated with a CMS.
20 */
21 public class CmsContentRepository extends AbstractContentRepository {
22
23 private Map<CmsSession, CmsContentSession> userSessions = Collections.synchronizedMap(new HashMap<>());
24
25 /*
26 * REPOSITORY
27 */
28
29 @Override
30 public ContentSession get() {
31 return get(CmsContextImpl.getCmsContext().getDefaultLocale());
32 }
33
34 @Override
35 public ContentSession get(Locale locale) {
36 // Subject subject = Subject.getSubject(AccessController.getContext());
37 CmsSession cmsSession = CurrentUser.getCmsSession();
38 CmsContentSession contentSession = userSessions.get(cmsSession);
39 if (contentSession == null) {
40 final CmsContentSession newContentSession = new CmsContentSession(this, cmsSession.getSubject(), locale);
41 cmsSession.addOnCloseCallback((c) -> {
42 newContentSession.close();
43 userSessions.remove(cmsSession);
44 });
45 contentSession = newContentSession;
46 }
47 return contentSession;
48 }
49
50 @Override
51 protected CmsContentSession newSystemSession() {
52 LoginContext loginContext;
53 try {
54 loginContext = new LoginContext(CmsAuth.DATA_ADMIN.getLoginContextName());
55 loginContext.login();
56 } catch (LoginException e1) {
57 throw new RuntimeException("Could not login as data admin", e1);
58 } finally {
59 }
60 return new CmsContentSession(this, loginContext.getSubject(), Locale.getDefault());
61 }
62
63 }