Author: asanso
Date: Fri Jun  6 06:51:46 2014
New Revision: 1600814

URL: http://svn.apache.org/r1600814
Log:
SLING-3633 - Sling uses wrong value for Principal object

Modified:
    
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java
    
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java

Modified: 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java?rev=1600814&r1=1600813&r2=1600814&view=diff
==============================================================================
--- 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java
 (original)
+++ 
sling/trunk/bundles/engine/src/main/java/org/apache/sling/engine/impl/SlingHttpServletRequestImpl.java
 Fri Jun  6 06:51:46 2014
@@ -280,6 +280,11 @@ public class SlingHttpServletRequestImpl
      */
     @Override
     public Principal getUserPrincipal() {
+        Principal principal = getResourceResolver().adaptTo(Principal.class);
+        if (principal != null) {
+            return principal;
+        }
+        //fallback to the userid
         String remoteUser = getRemoteUser();
         return (remoteUser != null) ? new UserPrincipal(remoteUser) : null;
     }

Modified: 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java?rev=1600814&r1=1600813&r2=1600814&view=diff
==============================================================================
--- 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
 (original)
+++ 
sling/trunk/bundles/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrResourceProvider.java
 Fri Jun  6 06:51:46 2014
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.jcr.resource.internal.helper.jcr;
 
+import java.security.Principal;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -37,6 +38,9 @@ import javax.jcr.query.Row;
 import javax.jcr.query.RowIterator;
 import javax.servlet.http.HttpServletRequest;
 
+import org.apache.jackrabbit.api.JackrabbitSession;
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.UserManager;
 import org.apache.sling.api.SlingException;
 import org.apache.sling.api.adapter.SlingAdaptable;
 import org.apache.sling.api.resource.AttributableResourceProvider;
@@ -386,6 +390,22 @@ public class JcrResourceProvider
     public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
         if (type == Session.class) {
             return (AdapterType) session;
+        } else if (type == Principal.class) {       
+            try {
+                if (this.session instanceof JackrabbitSession){
+                    JackrabbitSession s =((JackrabbitSession) this.session);
+                    final UserManager um = s.getUserManager();
+                    if (um != null) {
+                        final Authorizable auth = 
um.getAuthorizable(s.getUserID());
+                        if (auth != null) {
+                            return (AdapterType) auth.getPrincipal();
+                        }
+                    }
+                }
+                log.debug("not able to adapto Resource to Principal, let the 
base class try to adapt");
+            } catch (RepositoryException e) {
+                log.warn("error while adapting Resource to Principal, let the 
base class try to adapt", e);
+            }
         }
         return super.adaptTo(type);
     }


Reply via email to