]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - security/runtime/org.argeo.security.core/src/main/java/org/argeo/security/jcr/JcrUserDetails.java
Workaround for proble with the creation of the home directory
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.core / src / main / java / org / argeo / security / jcr / JcrUserDetails.java
index a59eabc0a8dd862083c3bb027bce5524d91b6ce2..223c06de0d3dd44b4a108cc39c1b01a14fc495b3 100644 (file)
@@ -4,9 +4,12 @@ import java.util.ArrayList;
 import java.util.List;
 
 import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.RepositoryException;
+import javax.jcr.Session;
 
 import org.argeo.jcr.ArgeoNames;
+import org.argeo.jcr.JcrUtils;
 import org.springframework.security.BadCredentialsException;
 import org.springframework.security.DisabledException;
 import org.springframework.security.GrantedAuthority;
@@ -20,6 +23,9 @@ public class JcrUserDetails extends User implements ArgeoNames {
        private final String homePath;
        private final String securityWorkspace;
 
+       /** Human readable user name */
+       private String displayName;
+
        protected JcrUserDetails(String securityWorkspace, String homePath,
                        String username, String password, boolean enabled,
                        boolean accountNonExpired, boolean credentialsNonExpired,
@@ -41,11 +47,38 @@ public class JcrUserDetails extends User implements ArgeoNames {
                                userProfile.getProperty(ARGEO_CREDENTIALS_NON_EXPIRED)
                                                .getBoolean(), userProfile.getProperty(
                                                ARGEO_ACCOUNT_NON_LOCKED).getBoolean(), authorities);
+               // human readable name
+               if (userProfile.hasProperty(Property.JCR_TITLE)) {
+                       displayName = userProfile.getProperty(Property.JCR_TITLE)
+                                       .getString();
+                       if (displayName.trim().equals(""))
+                               displayName = null;
+               }
+               if (displayName == null)
+                       displayName = userProfile.getProperty(ARGEO_USER_ID).getString();
                // home is defined as the parent of the profile
                homePath = userProfile.getParent().getPath();
                securityWorkspace = userProfile.getSession().getWorkspace().getName();
        }
 
+       /**
+        * Convenience constructor
+        * 
+        * @param session
+        *            the security session
+        * @param username
+        *            the username
+        * @param password
+        *            the password, can be null
+        * @param authorities
+        *            the granted authorities
+        */
+       public JcrUserDetails(Session session, String username, String password,
+                       GrantedAuthority[] authorities) throws RepositoryException {
+               this(JcrUtils.getUserProfile(session, username),
+                               password != null ? password : "", authorities);
+       }
+
        /**
         * Check the account status in JCR, throwing the exceptions expected by
         * Spring security if needed.
@@ -89,4 +122,15 @@ public class JcrUserDetails extends User implements ArgeoNames {
        public String getSecurityWorkspace() {
                return securityWorkspace;
        }
+
+       /** The human readable name of this user */
+       public String getDisplayName() {
+               return displayName;
+       }
+
+       @Override
+       public String toString() {
+               return getDisplayName();
+       }
+
 }