]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/UserAdminUtils.java
2aa23b2d0023af4c25a4d687ca1830bd808bee95
[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 display name. */
42 public static String getCurrentUserMail(UserAdmin userAdmin) {
43 String username = CurrentUser.getUsername();
44 return getUserMail(userAdmin, username);
45 }
46
47 // OTHER USERS HELPERS
48 /**
49 * Retrieves the local id of a user or group, that is respectively the uid
50 * or cn of the passed dn with no {@link UserAdmin}
51 */
52 public static String getUserLocalId(String dn) {
53 LdapName ldapName = getLdapName(dn);
54 Rdn last = ldapName.getRdn(ldapName.size() - 1);
55 if (last.getType().toLowerCase().equals(LdapAttrs.uid.name())
56 || last.getType().toLowerCase().equals(LdapAttrs.cn.name()))
57 return (String) last.getValue();
58 else
59 throw new CmsException("Cannot retrieve user local id, non valid dn: " + dn);
60 }
61
62 /**
63 * Returns the local username if no user with this dn is found or if the
64 * found user has no defined display name
65 */
66 public static String getUserDisplayName(UserAdmin userAdmin, String dn) {
67 Role user = userAdmin.getRole(dn);
68 String dName;
69 if (user == null)
70 dName = getUserLocalId(dn);
71 else {
72 dName = getProperty(user, LdapAttrs.displayName.name());
73 if (EclipseUiUtils.isEmpty(dName))
74 dName = getProperty(user, LdapAttrs.cn.name());
75 if (EclipseUiUtils.isEmpty(dName))
76 dName = getUserLocalId(dn);
77 }
78 return dName;
79 }
80
81 /**
82 * Returns null if no user with this dn is found or if the found user has no
83 * defined mail
84 */
85 public static String getUserMail(UserAdmin userAdmin, String dn) {
86 Role user = userAdmin.getRole(dn);
87 if (user == null)
88 return null;
89 else
90 return getProperty(user, LdapAttrs.mail.name());
91 }
92
93 // LDAP NAMES HELPERS
94 /**
95 * Easily retrieves one of the {@link Role}'s property or an empty String if
96 * the requested property is not defined
97 */
98 public final static String getProperty(Role role, String key) {
99 Object obj = role.getProperties().get(key);
100 if (obj != null)
101 return (String) obj;
102 else
103 return "";
104 }
105
106 /**
107 * Simply retrieves a LDAP name from a {@link LdapAttrs.DN} with no
108 * exception
109 */
110 private static LdapName getLdapName(String dn) {
111 try {
112 return new LdapName(dn);
113 } catch (InvalidNameException e) {
114 throw new CmsException("Cannot parse LDAP name " + dn, e);
115 }
116 }
117
118 /** Simply retrieves a display name of the relevant domain */
119 public final static String getDomainName(User user) {
120 String dn = user.getName();
121 if (dn.endsWith(NodeConstants.ROLES_BASEDN))
122 return "System roles";
123 try {
124 LdapName name = new LdapName(dn);
125 List<Rdn> rdns = name.getRdns();
126 String dname = null;
127 int i = 0;
128 loop: while (i < rdns.size()) {
129 Rdn currrRdn = rdns.get(i);
130 if (!LdapAttrs.dc.name().equals(currrRdn.getType()))
131 break loop;
132 else {
133 String currVal = (String) currrRdn.getValue();
134 dname = dname == null ? currVal : currVal + "." + dname;
135 }
136 i++;
137 }
138 return dname;
139 } catch (InvalidNameException e) {
140 throw new CmsException("Unable to get domain name for " + dn, e);
141 }
142 }
143
144 // VARIOUS HELPERS
145 public final static String buildDefaultCn(String firstName, String lastName) {
146 return (firstName.trim() + " " + lastName.trim() + " ").trim();
147 }
148 }