]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.cms/src/org/argeo/cms/util/useradmin/UserAdminUtils.java
Merge security.ui bundle in the cms.ui.workbench bundle
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / useradmin / UserAdminUtils.java
index 8a8a2187da72cc578aa4db4ca1beb23b99ef2a50..9ccc3057f3a355d66d369cdd06f3bd5633f2567f 100644 (file)
@@ -10,7 +10,6 @@ import javax.naming.ldap.Rdn;
 import javax.security.auth.Subject;
 import javax.security.auth.x500.X500Principal;
 
-import org.argeo.ArgeoException;
 import org.argeo.cms.CmsException;
 import org.argeo.cms.CmsView;
 import org.argeo.cms.auth.AuthConstants;
@@ -27,10 +26,10 @@ import org.osgi.service.useradmin.UserAdmin;
 /** Centralise common patterns to manage roles with a user admin */
 public class UserAdminUtils {
 
-       /** Retrieves a {@link User} given a LDAP name */
-       public final static User getUser(UserAdmin userAdmin, LdapName dn) {
-               User user = userAdmin.getUser(LdifName.dn.name(), dn.toString());
-               return user;
+       /** Retrieves a {@link Role} given a LDAP name */
+       public final static Role getRole(UserAdmin userAdmin, LdapName dn) {
+               Role role = userAdmin.getRole(dn.toString());
+               return role;
        }
 
        /** Retrieves the unique local username given a {@link User}. */
@@ -44,8 +43,8 @@ public class UserAdminUtils {
        }
 
        /**
-        * Easily retrieves one of the Role property or an empty String if the
-        * requested property is not defined for the passed user
+        * Easily retrieves one of the {@link Role}'s property or an empty String if
+        * the requested property is not defined
         */
        public final static String getProperty(Role role, String key) {
                Object obj = role.getProperties().get(key);
@@ -69,7 +68,7 @@ public class UserAdminUtils {
        // SELF HELPERS
        /** Simply retrieves the current logged-in user display name. */
        public static User getCurrentUser(UserAdmin userAdmin) {
-               return getUser(userAdmin, getCurrentUserLdapName());
+               return (User) getRole(userAdmin, getCurrentUserLdapName());
        }
 
        /** Simply retrieves the current logged-in user display name. */
@@ -87,7 +86,7 @@ public class UserAdminUtils {
        /** Returns the local name of the current connected user */
        public final static String getUsername(UserAdmin userAdmin) {
                LdapName dn = getCurrentUserLdapName();
-               return getUsername(getUser(userAdmin, dn));
+               return getUsername((User) getRole(userAdmin, dn));
        }
 
        /** Returns true if the current user is in the specified role */
@@ -107,7 +106,7 @@ public class UserAdminUtils {
                        else
                                return false;
                } catch (InvalidNameException e) {
-                       throw new ArgeoException("User " + user + " has an unvalid dn: "
+                       throw new CmsException("User " + user + " has an unvalid dn: "
                                        + userName, e);
                }
        }
@@ -136,7 +135,7 @@ public class UserAdminUtils {
                Subject subject = Subject.getSubject(AccessController.getContext());
                if (subject != null)
                        return subject;
-               throw new CmsException("Cannot find related subject");
+               throw new RuntimeException("Cannot find related subject");
        }
 
        // HOME MANAGEMENT
@@ -161,17 +160,14 @@ public class UserAdminUtils {
        // HELPERS TO RETRIEVE REMARKABLE PROPERTIES
        /** Simply retrieves the user uid from his dn with no useradmin */
        public static String getUserUid(String dn) {
-               try {
-                       LdapName ldapName = new LdapName(dn);
-                       Rdn last = ldapName.getRdn(ldapName.size() - 1);
-                       if (last.getType().equals(LdifName.uid.name()))
-                               return (String) last.getValue();
-                       else
-                               throw new ArgeoException("Cannot retrieve user uid, "
-                                               + "non valid dn: " + dn);
-               } catch (InvalidNameException e) {
-                       throw new ArgeoException("Cannot parse LDAP name " + dn, e);
-               }
+               LdapName ldapName = getLdapName(dn);
+               Rdn last = ldapName.getRdn(ldapName.size() - 1);
+               if (last.getType().toLowerCase().equals(LdifName.uid.name())
+                               || last.getType().toLowerCase().equals(LdifName.cn.name()))
+                       return (String) last.getValue();
+               else
+                       throw new CmsException("Cannot retrieve user uid, "
+                                       + "non valid dn: " + dn);
        }
 
        /**
@@ -179,7 +175,7 @@ public class UserAdminUtils {
         * found user has no defined display name
         */
        public static String getUserDisplayName(UserAdmin userAdmin, String dn) {
-               User user = getUser(userAdmin, getLdapName(dn));
+               Role user = getRole(userAdmin, getLdapName(dn));
                if (user == null)
                        return getUserUid(dn);
                String displayName = getProperty(user, LdifName.displayName.name());
@@ -196,7 +192,7 @@ public class UserAdminUtils {
         * defined mail
         */
        public static String getUserMail(UserAdmin userAdmin, String dn) {
-               User user = getUser(userAdmin, getLdapName(dn));
+               Role user = getRole(userAdmin, getLdapName(dn));
                if (user == null)
                        return null;
                else
@@ -204,7 +200,7 @@ public class UserAdminUtils {
        }
 
        // VARIOUS UI HELPERS
-       public final static String getDefaultCn(String firstName, String lastName) {
+       public final static String buildDefaultCn(String firstName, String lastName) {
                return (firstName.trim() + " " + lastName.trim() + " ").trim();
        }
 
@@ -230,7 +226,7 @@ public class UserAdminUtils {
                        }
                        return dname;
                } catch (InvalidNameException e) {
-                       throw new ArgeoException("Unable to get domain name for " + dn, e);
+                       throw new CmsException("Unable to get domain name for " + dn, e);
                }
        }
 
@@ -240,8 +236,7 @@ public class UserAdminUtils {
                try {
                        return new LdapName(dn);
                } catch (InvalidNameException e) {
-                       throw new ArgeoException("Cannot parse LDAP name " + dn, e);
+                       throw new CmsException("Cannot parse LDAP name " + dn, e);
                }
        }
-
 }
\ No newline at end of file