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