View Javadoc
1   package org.argeo.cms.auth;
2   
3   import java.util.List;
4   
5   import javax.naming.InvalidNameException;
6   import javax.naming.ldap.LdapName;
7   import javax.naming.ldap.Rdn;
8   
9   import org.argeo.api.NodeConstants;
10  import org.argeo.cms.CmsException;
11  import org.argeo.naming.LdapAttrs;
12  import org.osgi.service.useradmin.Role;
13  import org.osgi.service.useradmin.User;
14  import org.osgi.service.useradmin.UserAdmin;
15  
16  /** Centralise common patterns to manage users with a {@link UserAdmin} */
17  public class UserAdminUtils {
18  
19  	// CURRENTUSER HELPERS
20  	/** Checks if current user is the same as the passed one */
21  	public static boolean isCurrentUser(User user) {
22  		String userUsername = getProperty(user, LdapAttrs.DN);
23  		LdapName userLdapName = getLdapName(userUsername);
24  		LdapName selfUserName = getCurrentUserLdapName();
25  		return userLdapName.equals(selfUserName);
26  	}
27  
28  	/** Retrieves the current logged-in {@link User} */
29  	public static User getCurrentUser(UserAdmin userAdmin) {
30  		return (User) userAdmin.getRole(CurrentUser.getUsername());
31  	}
32  
33  	/** Retrieves the current logged-in user {@link LdapName} */
34  	public final static LdapName getCurrentUserLdapName() {
35  		String name = CurrentUser.getUsername();
36  		return getLdapName(name);
37  	}
38  
39  	/** Retrieves the current logged-in user mail */
40  	public static String getCurrentUserMail(UserAdmin userAdmin) {
41  		String username = CurrentUser.getUsername();
42  		return getUserMail(userAdmin, username);
43  	}
44  
45  	/** Retrieves the current logged-in user common name */
46  	public final static String getCommonName(User user) {
47  		return getProperty(user, LdapAttrs.cn.name());
48  	}
49  
50  	// OTHER USERS HELPERS
51  	/**
52  	 * Retrieves the local id of a user or group, that is respectively the uid or cn
53  	 * of the passed dn with no {@link UserAdmin}
54  	 */
55  	public static String getUserLocalId(String dn) {
56  		LdapName ldapName = getLdapName(dn);
57  		Rdn last = ldapName.getRdn(ldapName.size() - 1);
58  		if (last.getType().toLowerCase().equals(LdapAttrs.uid.name())
59  				|| last.getType().toLowerCase().equals(LdapAttrs.cn.name()))
60  			return (String) last.getValue();
61  		else
62  			throw new CmsException("Cannot retrieve user local id, non valid dn: " + dn);
63  	}
64  
65  	/**
66  	 * Returns the local username if no user with this dn is found or if the found
67  	 * user has no defined display name
68  	 */
69  	public static String getUserDisplayName(UserAdmin userAdmin, String dn) {
70  		Role user = userAdmin.getRole(dn);
71  		String dName;
72  		if (user == null)
73  			dName = getUserLocalId(dn);
74  		else {
75  			dName = getProperty(user, LdapAttrs.displayName.name());
76  			if (isEmpty(dName))
77  				dName = getProperty(user, LdapAttrs.cn.name());
78  			if (isEmpty(dName))
79  				dName = getUserLocalId(dn);
80  		}
81  		return dName;
82  	}
83  
84  	/**
85  	 * Returns null if no user with this dn is found or if the found user has no
86  	 * defined mail
87  	 */
88  	public static String getUserMail(UserAdmin userAdmin, String dn) {
89  		Role user = userAdmin.getRole(dn);
90  		if (user == null)
91  			return null;
92  		else
93  			return getProperty(user, LdapAttrs.mail.name());
94  	}
95  
96  	// LDAP NAMES HELPERS
97  	/**
98  	 * Easily retrieves one of the {@link Role}'s property or an empty String if the
99  	 * requested property is not defined
100 	 */
101 	public final static String getProperty(Role role, String key) {
102 		Object obj = role.getProperties().get(key);
103 		if (obj != null)
104 			return (String) obj;
105 		else
106 			return "";
107 	}
108 
109 	public final static String getProperty(Role role, Enum<?> key) {
110 		Object obj = role.getProperties().get(key.name());
111 		if (obj != null)
112 			return (String) obj;
113 		else
114 			return "";
115 	}
116 
117 	public final static void setProperty(Role role, String key, String value) {
118 		role.getProperties().put(key, value);
119 	}
120 
121 	public final static void setProperty(Role role, Enum<?> key, String value) {
122 		setProperty(role, key.name(), value);
123 	}
124 
125 	/**
126 	 * Simply retrieves a LDAP name from a {@link LdapAttrs.DN} with no exception
127 	 */
128 	private static LdapName getLdapName(String dn) {
129 		try {
130 			return new LdapName(dn);
131 		} catch (InvalidNameException e) {
132 			throw new CmsException("Cannot parse LDAP name " + dn, e);
133 		}
134 	}
135 
136 	/** Simply retrieves a display name of the relevant domain */
137 	public final static String getDomainName(User user) {
138 		String dn = user.getName();
139 		if (dn.endsWith(NodeConstants.ROLES_BASEDN))
140 			return "System roles";
141 		if (dn.endsWith(NodeConstants.TOKENS_BASEDN))
142 			return "Tokens";
143 		try {
144 			// FIXME deal with non-DC
145 			LdapName name = new LdapName(dn);
146 			List<Rdn> rdns = name.getRdns();
147 			String dname = null;
148 			int i = 0;
149 			loop: while (i < rdns.size()) {
150 				Rdn currrRdn = rdns.get(i);
151 				if (!LdapAttrs.dc.name().equals(currrRdn.getType()))
152 					break loop;
153 				else {
154 					String currVal = (String) currrRdn.getValue();
155 					dname = dname == null ? currVal : currVal + "." + dname;
156 				}
157 				i++;
158 			}
159 			return dname;
160 		} catch (InvalidNameException e) {
161 			throw new CmsException("Unable to get domain name for " + dn, e);
162 		}
163 	}
164 
165 	// VARIOUS HELPERS
166 	public final static String buildDefaultCn(String firstName, String lastName) {
167 		return (firstName.trim() + " " + lastName.trim() + " ").trim();
168 	}
169 
170 	/** Simply checks if a string is null or empty */
171 	private static boolean isEmpty(String stringToTest) {
172 		return stringToTest == null || "".equals(stringToTest.trim());
173 	}
174 
175 }