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. */
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);
}
/**
* 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;
}
/**
* 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 */
}
}
- // 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();
}
}