]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/UserAdminUtils.java
Add JGit to client.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / util / UserAdminUtils.java
1 package org.argeo.cms.util;
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.cms.CmsException;
10 import org.argeo.cms.auth.CurrentUser;
11 import org.argeo.eclipse.ui.EclipseUiUtils;
12 import org.argeo.naming.LdapAttrs;
13 import org.argeo.node.NodeConstants;
14 import org.osgi.service.useradmin.Role;
15 import org.osgi.service.useradmin.User;
16 import org.osgi.service.useradmin.UserAdmin;
17
18 /** Centralise common patterns to manage users with a {@link UserAdmin} */
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, LdapAttrs.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, LdapAttrs.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(LdapAttrs.uid.name())
61 || last.getType().toLowerCase().equals(LdapAttrs.cn.name()))
62 return (String) last.getValue();
63 else
64 throw new CmsException("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 String dName;
74 if (user == null)
75 dName = getUserLocalId(dn);
76 else {
77 dName = getProperty(user, LdapAttrs.displayName.name());
78 if (EclipseUiUtils.isEmpty(dName))
79 dName = getProperty(user, LdapAttrs.cn.name());
80 if (EclipseUiUtils.isEmpty(dName))
81 dName = getUserLocalId(dn);
82 }
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 @SuppressWarnings("unchecked")
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 LdapAttrs.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 CmsException("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(NodeConstants.ROLES_BASEDN))
143 return "System roles";
144 if (dn.endsWith(NodeConstants.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 (!LdapAttrs.dc.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 CmsException("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 }