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