]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/UserAdminUtils.java
Use runtime namespace context as default.
[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 = getUserLocalId(user.getName());
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 IllegalArgumentException("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(CmsConstants.ROLES_BASEDN))
140 return "System roles";
141 if (dn.endsWith(CmsConstants.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.uid.name().equals(currrRdn.getType()) || LdapAttrs.cn.name().equals(currrRdn.getType())
152 || LdapAttrs.ou.name().equals(currrRdn.getType()))
153 break loop;
154 else {
155 String currVal = (String) currrRdn.getValue();
156 dname = dname == null ? currVal : currVal + "." + dname;
157 }
158 i++;
159 }
160 return dname;
161 } catch (InvalidNameException e) {
162 throw new IllegalArgumentException("Unable to get domain name for " + dn, e);
163 }
164 }
165
166 // VARIOUS HELPERS
167 public final static String buildDefaultCn(String firstName, String lastName) {
168 return (firstName.trim() + " " + lastName.trim() + " ").trim();
169 }
170
171 /** Simply checks if a string is null or empty */
172 private static boolean isEmpty(String stringToTest) {
173 return stringToTest == null || "".equals(stringToTest.trim());
174 }
175
176 }