]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/UserAdminUtils.java
Store UI context data in CMS View.
[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.NodeConstants;
10 import org.argeo.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 String dName;
71 if (user == null)
72 dName = getUserLocalId(dn);
73 else {
74 dName = getProperty(user, LdapAttrs.displayName.name());
75 if (isEmpty(dName))
76 dName = getProperty(user, LdapAttrs.cn.name());
77 if (isEmpty(dName))
78 dName = getUserLocalId(dn);
79 }
80 return dName;
81 }
82
83 /**
84 * Returns null if no user with this dn is found or if the found user has no
85 * defined mail
86 */
87 public static String getUserMail(UserAdmin userAdmin, String dn) {
88 Role user = userAdmin.getRole(dn);
89 if (user == null)
90 return null;
91 else
92 return getProperty(user, LdapAttrs.mail.name());
93 }
94
95 // LDAP NAMES HELPERS
96 /**
97 * Easily retrieves one of the {@link Role}'s property or an empty String if the
98 * requested property is not defined
99 */
100 public final static String getProperty(Role role, String key) {
101 Object obj = role.getProperties().get(key);
102 if (obj != null)
103 return (String) obj;
104 else
105 return "";
106 }
107
108 public final static String getProperty(Role role, Enum<?> key) {
109 Object obj = role.getProperties().get(key.name());
110 if (obj != null)
111 return (String) obj;
112 else
113 return "";
114 }
115
116 public final static void setProperty(Role role, String key, String value) {
117 role.getProperties().put(key, value);
118 }
119
120 public final static void setProperty(Role role, Enum<?> key, String value) {
121 setProperty(role, key.name(), value);
122 }
123
124 /**
125 * Simply retrieves a LDAP name from a {@link LdapAttrs.DN} with no exception
126 */
127 private static LdapName getLdapName(String dn) {
128 try {
129 return new LdapName(dn);
130 } catch (InvalidNameException e) {
131 throw new IllegalArgumentException("Cannot parse LDAP name " + dn, e);
132 }
133 }
134
135 /** Simply retrieves a display name of the relevant domain */
136 public final static String getDomainName(User user) {
137 String dn = user.getName();
138 if (dn.endsWith(NodeConstants.ROLES_BASEDN))
139 return "System roles";
140 if (dn.endsWith(NodeConstants.TOKENS_BASEDN))
141 return "Tokens";
142 try {
143 // FIXME deal with non-DC
144 LdapName name = new LdapName(dn);
145 List<Rdn> rdns = name.getRdns();
146 String dname = null;
147 int i = 0;
148 loop: while (i < rdns.size()) {
149 Rdn currrRdn = rdns.get(i);
150 if (!LdapAttrs.dc.name().equals(currrRdn.getType()))
151 break loop;
152 else {
153 String currVal = (String) currrRdn.getValue();
154 dname = dname == null ? currVal : currVal + "." + dname;
155 }
156 i++;
157 }
158 return dname;
159 } catch (InvalidNameException e) {
160 throw new IllegalArgumentException("Unable to get domain name for " + dn, e);
161 }
162 }
163
164 // VARIOUS HELPERS
165 public final static String buildDefaultCn(String firstName, String lastName) {
166 return (firstName.trim() + " " + lastName.trim() + " ").trim();
167 }
168
169 /** Simply checks if a string is null or empty */
170 private static boolean isEmpty(String stringToTest) {
171 return stringToTest == null || "".equals(stringToTest.trim());
172 }
173
174 }