]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/useradmin/UserAdminUtils.java
Continue finalising security. Fix issues with login in web.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / util / useradmin / UserAdminUtils.java
1 package org.argeo.cms.util.useradmin;
2
3 import java.util.List;
4 import java.util.Set;
5
6 import javax.naming.InvalidNameException;
7 import javax.naming.ldap.LdapName;
8 import javax.naming.ldap.Rdn;
9
10 import org.argeo.cms.CmsException;
11 import org.argeo.cms.auth.CurrentUser;
12 import org.argeo.eclipse.ui.EclipseUiUtils;
13 import org.argeo.jcr.JcrUtils;
14 import org.argeo.naming.LdapAttrs;
15 import org.argeo.node.NodeConstants;
16 import org.osgi.service.useradmin.Group;
17 import org.osgi.service.useradmin.Role;
18 import org.osgi.service.useradmin.User;
19 import org.osgi.service.useradmin.UserAdmin;
20
21 /** Centralise common patterns to manage roles with a user admin */
22 public class UserAdminUtils {
23
24 /** Retrieves a {@link Role} given a LDAP name */
25 public final static Role getRole(UserAdmin userAdmin, LdapName dn) {
26 Role role = userAdmin.getRole(dn.toString());
27 return role;
28 }
29
30 /** Retrieves the unique local username given a {@link User}. */
31 public final static String getUsername(User user) {
32 String username = null;
33 if (user instanceof Group)
34 username = getProperty(user, LdapAttrs.cn.name());
35 else
36 username = getProperty(user, LdapAttrs.uid.name());
37 return username;
38 }
39
40 /**
41 * Easily retrieves one of the {@link Role}'s property or an empty String if
42 * the requested property is not defined
43 */
44 public final static String getProperty(Role role, String key) {
45 Object obj = role.getProperties().get(key);
46 if (obj != null)
47 return (String) obj;
48 else
49 return "";
50 }
51
52 // CENTRALIZE SOME METHODS UNTIL API IS STABLE
53 /** Simply checks if current user is registered */
54 public static boolean isRegistered() {
55 return !CurrentUser.isAnonymous();
56 }
57
58 /** Simply checks if current user as a home */
59 public static boolean hasHome() {
60 return isRegistered();
61 }
62
63 // SELF HELPERS
64 /** Simply retrieves the current logged-in user display name. */
65 public static User getCurrentUser(UserAdmin userAdmin) {
66 return (User) getRole(userAdmin, getCurrentUserLdapName());
67 }
68
69 /** Simply retrieves the current logged-in user display name. */
70 public static String getCurrentUserDisplayName(UserAdmin userAdmin) {
71 String username = CurrentUser.getUsername();
72 return getUserDisplayName(userAdmin, username);
73 }
74
75 /** Simply retrieves the current logged-in user display name. */
76 public static String getCurrentUserMail(UserAdmin userAdmin) {
77 String username = CurrentUser.getUsername();
78 return getUserMail(userAdmin, username);
79 }
80
81 /** Returns the local name of the current connected user */
82 public final static String getUsername(UserAdmin userAdmin) {
83 LdapName dn = getCurrentUserLdapName();
84 return getUsername((User) getRole(userAdmin, dn));
85 }
86
87 /** Returns true if the current user is in the specified role */
88 public static boolean isUserInRole(String role) {
89 Set<String> roles = CurrentUser.roles();
90 return roles.contains(role);
91 }
92
93 /** Simply checks if current user is the same as the passed one */
94 public static boolean isCurrentUser(User user) {
95 String userName = getProperty(user, LdapAttrs.DN);
96 try {
97 LdapName selfUserName = getCurrentUserLdapName();
98 LdapName userLdapName = new LdapName(userName);
99 if (userLdapName.equals(selfUserName))
100 return true;
101 else
102 return false;
103 } catch (InvalidNameException e) {
104 throw new CmsException("User " + user + " has an unvalid dn: " + userName, e);
105 }
106 }
107
108 public final static LdapName getCurrentUserLdapName() {
109 String name = CurrentUser.getUsername();
110 return getLdapName(name);
111 }
112
113 /**
114 * Simply retrieves username for current user, generally a LDAP dn
115 *
116 * @deprecated Use {@link CurrentUser#getUsername()}
117 */
118 @Deprecated
119 public static String getCurrentUsername() {
120 return CurrentUser.getUsername();
121 }
122
123 // /**
124 // * Fork of the {@link CurrentUser#currentSubject} method that is private.
125 // * TODO Enhance and factorize
126 // */
127 // private static Subject currentSubject() {
128 // CmsView cmsView = CmsUtils.getCmsView();
129 // if (cmsView != null)
130 // return cmsView.getSubject();
131 // Subject subject = Subject.getSubject(AccessController.getContext());
132 // if (subject != null)
133 // return subject;
134 // throw new RuntimeException("Cannot find related subject");
135 // }
136
137 // HOME MANAGEMENT
138 /**
139 * Simply retrieves the *relative* path to the current user home node from
140 * the base home node
141 */
142 public static String getCurrentUserHomeRelPath() {
143 return getHomeRelPath(CurrentUser.getUsername());
144 }
145
146 /**
147 * Simply retrieves the *relative* path to the home node of a user given its
148 * userName
149 */
150 public static String getHomeRelPath(String userName) {
151 String id = getUserUid(userName);
152 String currHomePath = JcrUtils.firstCharsToPath(id, 2) + "/" + id;
153 return currHomePath;
154 }
155
156 // HELPERS TO RETRIEVE REMARKABLE PROPERTIES
157 /** Simply retrieves the user uid from his dn with no useradmin */
158 public static String getUserUid(String dn) {
159 LdapName ldapName = getLdapName(dn);
160 Rdn last = ldapName.getRdn(ldapName.size() - 1);
161 if (last.getType().toLowerCase().equals(LdapAttrs.uid.name())
162 || last.getType().toLowerCase().equals(LdapAttrs.cn.name()))
163 return (String) last.getValue();
164 else
165 throw new CmsException("Cannot retrieve user uid, " + "non valid dn: " + dn);
166 }
167
168 /**
169 * Returns the local username if no user with this dn is found or if the
170 * found user has no defined display name
171 */
172 public static String getUserDisplayName(UserAdmin userAdmin, String dn) {
173 Role user = getRole(userAdmin, getLdapName(dn));
174 if (user == null)
175 return getUserUid(dn);
176 String displayName = getProperty(user, LdapAttrs.displayName.name());
177 if (EclipseUiUtils.isEmpty(displayName))
178 displayName = getProperty(user, LdapAttrs.cn.name());
179 if (EclipseUiUtils.isEmpty(displayName))
180 return getUserUid(dn);
181 else
182 return displayName;
183 }
184
185 /**
186 * Returns null if no user with this dn is found or if the found user has no
187 * defined mail
188 */
189 public static String getUserMail(UserAdmin userAdmin, String dn) {
190 Role user = getRole(userAdmin, getLdapName(dn));
191 if (user == null)
192 return null;
193 else
194 return getProperty(user, LdapAttrs.mail.name());
195 }
196
197 // VARIOUS UI HELPERS
198 public final static String buildDefaultCn(String firstName, String lastName) {
199 return (firstName.trim() + " " + lastName.trim() + " ").trim();
200 }
201
202 /** Simply retrieves a display name of the relevant domain */
203 public final static String getDomainName(User user) {
204 String dn = user.getName();
205 if (dn.endsWith(NodeConstants.ROLES_BASEDN))
206 return "System roles";
207 try {
208 LdapName name = new LdapName(dn);
209 List<Rdn> rdns = name.getRdns();
210 String dname = null;
211 int i = 0;
212 loop: while (i < rdns.size()) {
213 Rdn currrRdn = rdns.get(i);
214 if (!LdapAttrs.dc.name().equals(currrRdn.getType()))
215 break loop;
216 else {
217 String currVal = (String) currrRdn.getValue();
218 dname = dname == null ? currVal : currVal + "." + dname;
219 }
220 i++;
221 }
222 return dname;
223 } catch (InvalidNameException e) {
224 throw new CmsException("Unable to get domain name for " + dn, e);
225 }
226 }
227
228 // Local Helpers
229 /** Simply retrieves a LDAP name from a dn with no exception */
230 public static LdapName getLdapName(String dn) {
231 try {
232 return new LdapName(dn);
233 } catch (InvalidNameException e) {
234 throw new CmsException("Cannot parse LDAP name " + dn, e);
235 }
236 }
237 }