Make username and user role more consistent.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / servlet / ServletAuthUtils.java
index 13dfbe638782c3a4243f03d96d0af311e6b471d0..333fa1aa027f481090b4370e1d61ba8be16dcdf4 100644 (file)
@@ -8,11 +8,16 @@ import java.util.function.Supplier;
 import javax.security.auth.Subject;
 import javax.servlet.http.HttpServletRequest;
 
+import org.argeo.cms.auth.CmsSession;
 import org.argeo.cms.auth.CurrentUser;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
 import org.osgi.service.http.HttpContext;
 
 /** Authentications utilities when using servlets. */
 public class ServletAuthUtils {
+       private static BundleContext bundleContext = FrameworkUtil.getBundle(ServletAuthUtils.class).getBundleContext();
+
        /**
         * Execute this supplier, using the CMS class loader as context classloader.
         * Useful to log in to JCR.
@@ -50,4 +55,11 @@ public class ServletAuthUtils {
                req.setAttribute(HttpContext.REMOTE_USER, null);
                req.setAttribute(AccessControlContext.class.getName(), null);
        }
+
+       public static CmsSession getCmsSession(HttpServletRequest req) {
+               Subject subject = Subject
+                               .getSubject((AccessControlContext) req.getAttribute(AccessControlContext.class.getName()));
+               CmsSession cmsSession = CmsSession.getCmsSession(bundleContext, subject);
+               return cmsSession;
+       }
 }