]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/internal/http/CmsSessionProvider.java
Improve and clean authentication. Reintroduce anonymous login context.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / http / CmsSessionProvider.java
index 375520171a213ea5cc9f6ab5e9267e78b6d37d13..37ba5cdb15332a440e1dac222ecaf7ed16d1db28 100644 (file)
@@ -13,6 +13,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.jackrabbit.server.SessionProvider;
 import org.argeo.cms.auth.CmsSession;
+import org.argeo.jcr.JcrUtils;
 
 /**
  * Implements an open session in view patter: a new JCR session is created for
@@ -35,17 +36,43 @@ class CmsSessionProvider implements SessionProvider, Serializable {
                        throws javax.jcr.LoginException, ServletException, RepositoryException {
 
                CmsSession cmsSession = WebCmsSessionImpl.getCmsSession(request);
+//             if (cmsSession == null)
+//                     return anonymousSession(request, rep, workspace);
+               if (log.isTraceEnabled()) {
+                       log.debug("Get JCR session from " + cmsSession);
+               }
                Session session = cmsSession.getDataSession(alias, workspace, rep);
                cmsSessions.put(session, cmsSession);
                return session;
        }
 
-       public void releaseSession(Session session) {
+//     private synchronized Session anonymousSession(HttpServletRequest request, Repository repository, String workspace) {
+//             // TODO rather log in here as anonymous?
+//             LoginContext lc = (LoginContext) request.getAttribute(NodeConstants.LOGIN_CONTEXT_ANONYMOUS);
+//             if (lc == null)
+//                     throw new CmsException("No login context available");
+//             // optimize
+//             Session session;
+//             try {
+//                     session = Subject.doAs(lc.getSubject(), new PrivilegedExceptionAction<Session>() {
+//                             @Override
+//                             public Session run() throws Exception {
+//                                     return repository.login(workspace);
+//                             }
+//                     });
+//             } catch (Exception e) {
+//                     throw new CmsException("Cannot log in to JCR", e);
+//             }
+//             return session;
+//     }
+
+       public synchronized void releaseSession(Session session) {
                if (cmsSessions.containsKey(session)) {
                        CmsSession cmsSession = cmsSessions.get(session);
                        cmsSession.releaseDataSession(alias, session);
                } else {
-                       log.warn("No CMS session for JCR session " + session);
+                       log.warn("JCR session "+session+" not found in CMS session list. Logging it out...");
+                       JcrUtils.logoutQuietly(session);
                }
        }
 }