Clean userAdmin utils methods
authorBruno Sinou <bsinou@argeo.org>
Tue, 25 Oct 2016 14:25:26 +0000 (14:25 +0000)
committerBruno Sinou <bsinou@argeo.org>
Tue, 25 Oct 2016 14:25:26 +0000 (14:25 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@9281 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.cms.ui/src/org/argeo/cms/util/useradmin/UserAdminUtils.java

index 34b528e1b17886a82c051bcf81cbceac13d690b4..1c7ba0a6aab339db83f009ff2a47a02335a65eec 100644 (file)
@@ -10,66 +10,38 @@ import javax.naming.ldap.Rdn;
 import org.argeo.cms.CmsException;
 import org.argeo.cms.auth.CurrentUser;
 import org.argeo.eclipse.ui.EclipseUiUtils;
-import org.argeo.jcr.JcrUtils;
 import org.argeo.naming.LdapAttrs;
 import org.argeo.node.NodeConstants;
-import org.osgi.service.useradmin.Group;
 import org.osgi.service.useradmin.Role;
 import org.osgi.service.useradmin.User;
 import org.osgi.service.useradmin.UserAdmin;
 
-/** Centralise common patterns to manage roles with a user admin */
-@Deprecated
+/** Centralise common patterns to manage users with a {@link UserAdmin} */
 public class UserAdminUtils {
 
-       /** 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}. */
-       public final static String getUsername(User user) {
-               String username = null;
-               if (user instanceof Group)
-                       username = getProperty(user, LdapAttrs.cn.name());
-               else
-                       username = getProperty(user, LdapAttrs.uid.name());
-               return username;
-       }
-
-       /**
-        * 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);
-               if (obj != null)
-                       return (String) obj;
-               else
-                       return "";
-       }
-
-       // CENTRALIZE SOME METHODS UNTIL API IS STABLE
+       // SELF HELPERS
        /** Simply checks if current user is registered */
        public static boolean isRegistered() {
                return !CurrentUser.isAnonymous();
        }
 
-       /** Simply checks if current user as a home */
-       public static boolean hasHome() {
-               return isRegistered();
+       /** Simply checks if current user is the same as the passed one */
+       public static boolean isCurrentUser(User user) {
+               String userUsername = getProperty(user, LdapAttrs.DN);
+               LdapName userLdapName = getLdapName(userUsername);
+               LdapName selfUserName = getCurrentUserLdapName();
+               return userLdapName.equals(selfUserName);
        }
 
-       /** Simply retrieves the current logged-in user display name. */
-       public static String getCurrentUserDisplayName() {
-               return CurrentUser.getDisplayName();
+       /** Simply retrieves the current logged-in {@link User} */
+       public static User getCurrentUser(UserAdmin userAdmin) {
+               return (User) userAdmin.getRole(CurrentUser.getUsername());
        }
 
-       // SELF HELPERS
-       /** Simply retrieves the current logged-in user display name. */
-       public static User getCurrentUser(UserAdmin userAdmin) {
-               return (User) getRole(userAdmin, getCurrentUserLdapName());
+       /** Simply retrieves the current logged-in user {@link LdapName} */
+       public final static LdapName getCurrentUserLdapName() {
+               String name = CurrentUser.getUsername();
+               return getLdapName(name);
        }
 
        /** Simply retrieves the current logged-in user display name. */
@@ -78,68 +50,25 @@ public class UserAdminUtils {
                return getUserMail(userAdmin, username);
        }
 
-       /** Returns the local name of the current connected user */
-       public final static String getUsername() {
-               return CurrentUser.getUsername();
-       }
-
        /** Returns true if the current user is in the specified role */
        public static boolean isUserInRole(String role) {
                Set<String> roles = CurrentUser.roles();
                return roles.contains(role);
        }
 
-       /** Simply checks if current user is the same as the passed one */
-       public static boolean isCurrentUser(User user) {
-               String userName = getProperty(user, LdapAttrs.DN);
-               try {
-                       LdapName selfUserName = getCurrentUserLdapName();
-                       LdapName userLdapName = new LdapName(userName);
-                       if (userLdapName.equals(selfUserName))
-                               return true;
-                       else
-                               return false;
-               } catch (InvalidNameException e) {
-                       throw new CmsException("User " + user + " has an unvalid dn: "
-                                       + userName, e);
-               }
-       }
-
-       public final static LdapName getCurrentUserLdapName() {
-               String name = CurrentUser.getUsername();
-               return getLdapName(name);
-       }
-
-       // HOME MANAGEMENT
-       /**
-        * Simply retrieves the *relative* path to the current user home node from
-        * the base home node
-        */
-       public static String getCurrentUserHomeRelPath() {
-               return getHomeRelPath(CurrentUser.getUsername());
-       }
-
+       // OTHER USERS HELPERS
        /**
-        * Simply retrieves the *relative* path to the home node of a user given its
-        * userName
+        * Simply retrieves the local id of a user or group, that is respectively
+        * the uid or cn of the passed dn with no {@link UserAdmin}
         */
-       public static String getHomeRelPath(String userName) {
-               String id = getUserUid(userName);
-               String currHomePath = JcrUtils.firstCharsToPath(id, 2) + "/" + id;
-               return currHomePath;
-       }
-
-       // HELPERS TO RETRIEVE REMARKABLE PROPERTIES
-       /** Simply retrieves the user uid from his dn with no useradmin */
-       public static String getUserUid(String dn) {
+       public static String getUserLocalId(String dn) {
                LdapName ldapName = getLdapName(dn);
                Rdn last = ldapName.getRdn(ldapName.size() - 1);
                if (last.getType().toLowerCase().equals(LdapAttrs.uid.name())
                                || last.getType().toLowerCase().equals(LdapAttrs.cn.name()))
                        return (String) last.getValue();
                else
-                       throw new CmsException("Cannot retrieve user uid, "
-                                       + "non valid dn: " + dn);
+                       throw new CmsException("Cannot retrieve user local id, non valid dn: " + dn);
        }
 
        /**
@@ -147,16 +76,18 @@ public class UserAdminUtils {
         * found user has no defined display name
         */
        public static String getUserDisplayName(UserAdmin userAdmin, String dn) {
-               Role user = getRole(userAdmin, getLdapName(dn));
+               Role user = userAdmin.getRole(dn);
+               String dName;
                if (user == null)
-                       return getUserUid(dn);
-               String displayName = getProperty(user, LdapAttrs.displayName.name());
-               if (EclipseUiUtils.isEmpty(displayName))
-                       displayName = getProperty(user, LdapAttrs.cn.name());
-               if (EclipseUiUtils.isEmpty(displayName))
-                       return getUserUid(dn);
-               else
-                       return displayName;
+                       dName = getUserLocalId(dn);
+               else {
+                       dName = getProperty(user, LdapAttrs.displayName.name());
+                       if (EclipseUiUtils.isEmpty(dName))
+                               dName = getProperty(user, LdapAttrs.cn.name());
+                       if (EclipseUiUtils.isEmpty(dName))
+                               dName = getUserLocalId(dn);
+               }
+               return dName;
        }
 
        /**
@@ -164,16 +95,36 @@ public class UserAdminUtils {
         * defined mail
         */
        public static String getUserMail(UserAdmin userAdmin, String dn) {
-               Role user = getRole(userAdmin, getLdapName(dn));
+               Role user = userAdmin.getRole(dn);
                if (user == null)
                        return null;
                else
                        return getProperty(user, LdapAttrs.mail.name());
        }
 
-       // VARIOUS UI HELPERS
-       public final static String buildDefaultCn(String firstName, String lastName) {
-               return (firstName.trim() + " " + lastName.trim() + " ").trim();
+       // LDAP NAMES HELPERS
+       /**
+        * 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);
+               if (obj != null)
+                       return (String) obj;
+               else
+                       return "";
+       }
+
+       /**
+        * Simply retrieves a LDAP name from a {@link LdapAttrs.DN} with no
+        * exception
+        */
+       public static LdapName getLdapName(String dn) {
+               try {
+                       return new LdapName(dn);
+               } catch (InvalidNameException e) {
+                       throw new CmsException("Cannot parse LDAP name " + dn, e);
+               }
        }
 
        /** Simply retrieves a display name of the relevant domain */
@@ -202,13 +153,8 @@ public class UserAdminUtils {
                }
        }
 
-       // Local Helpers
-       /** Simply retrieves a LDAP name from a dn with no exception */
-       public static LdapName getLdapName(String dn) {
-               try {
-                       return new LdapName(dn);
-               } catch (InvalidNameException e) {
-                       throw new CmsException("Cannot parse LDAP name " + dn, e);
-               }
+       // VARIOUS HELPERS
+       public final static String buildDefaultCn(String firstName, String lastName) {
+               return (firstName.trim() + " " + lastName.trim() + " ").trim();
        }
 }