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