]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/useradmin/UserAdminUtils.java
1 package org
.argeo
.cms
.util
.useradmin
;
3 import java
.security
.AccessController
;
7 import javax
.naming
.InvalidNameException
;
8 import javax
.naming
.ldap
.LdapName
;
9 import javax
.naming
.ldap
.Rdn
;
10 import javax
.security
.auth
.Subject
;
11 import javax
.security
.auth
.x500
.X500Principal
;
13 import org
.argeo
.ArgeoException
;
14 import org
.argeo
.cms
.CmsException
;
15 import org
.argeo
.cms
.CmsView
;
16 import org
.argeo
.cms
.auth
.AuthConstants
;
17 import org
.argeo
.cms
.auth
.CurrentUser
;
18 import org
.argeo
.cms
.util
.CmsUtils
;
19 import org
.argeo
.eclipse
.ui
.EclipseUiUtils
;
20 import org
.argeo
.jcr
.JcrUtils
;
21 import org
.argeo
.osgi
.useradmin
.LdifName
;
22 import org
.osgi
.service
.useradmin
.Group
;
23 import org
.osgi
.service
.useradmin
.Role
;
24 import org
.osgi
.service
.useradmin
.User
;
25 import org
.osgi
.service
.useradmin
.UserAdmin
;
27 /** Centralise common patterns to manage roles with a user admin */
28 public class UserAdminUtils
{
30 /** Retrieves a {@link Role} given a LDAP name */
31 public final static Role
getRole(UserAdmin userAdmin
, LdapName dn
) {
32 Role role
= userAdmin
.getRole(dn
.toString());
36 /** Retrieves the unique local username given a {@link User}. */
37 public final static String
getUsername(User user
) {
38 String username
= null;
39 if (user
instanceof Group
)
40 username
= getProperty(user
, LdifName
.cn
.name());
42 username
= getProperty(user
, LdifName
.uid
.name());
47 * Easily retrieves one of the {@link Role}'s property or an empty String if
48 * the requested property is not defined
50 public final static String
getProperty(Role role
, String key
) {
51 Object obj
= role
.getProperties().get(key
);
58 // CENTRALIZE SOME METHODS UNTIL API IS STABLE
59 /** Simply checks if current user is registered */
60 public static boolean isRegistered() {
61 return !CurrentUser
.isAnonymous();
64 /** Simply checks if current user as a home */
65 public static boolean hasHome() {
66 return isRegistered();
70 /** Simply retrieves the current logged-in user display name. */
71 public static User
getCurrentUser(UserAdmin userAdmin
) {
72 return (User
) getRole(userAdmin
, getCurrentUserLdapName());
75 /** Simply retrieves the current logged-in user display name. */
76 public static String
getCurrentUserDisplayName(UserAdmin userAdmin
) {
77 String username
= getCurrentUsername();
78 return getUserDisplayName(userAdmin
, username
);
81 /** Simply retrieves the current logged-in user display name. */
82 public static String
getCurrentUserMail(UserAdmin userAdmin
) {
83 String username
= getCurrentUsername();
84 return getUserMail(userAdmin
, username
);
87 /** Returns the local name of the current connected user */
88 public final static String
getUsername(UserAdmin userAdmin
) {
89 LdapName dn
= getCurrentUserLdapName();
90 return getUsername((User
) getRole(userAdmin
, dn
));
93 /** Returns true if the current user is in the specified role */
94 public static boolean isUserInRole(String role
) {
95 Set
<String
> roles
= CurrentUser
.roles();
96 return roles
.contains(role
);
99 /** Simply checks if current user is the same as the passed one */
100 public static boolean isCurrentUser(User user
) {
101 String userName
= getProperty(user
, LdifName
.dn
.name());
103 LdapName selfUserName
= getCurrentUserLdapName();
104 LdapName userLdapName
= new LdapName(userName
);
105 if (userLdapName
.equals(selfUserName
))
109 } catch (InvalidNameException e
) {
110 throw new ArgeoException("User " + user
+ " has an unvalid dn: "
115 public final static LdapName
getCurrentUserLdapName() {
116 String name
= getCurrentUsername();
117 return getLdapName(name
);
120 /** Simply retrieves username for current user, generally a LDAP dn */
121 public static String
getCurrentUsername() {
122 Subject subject
= currentSubject();
123 String name
= subject
.getPrincipals(X500Principal
.class).iterator()
129 * Fork of the {@link CurrentUser#currentSubject} method that is private.
130 * TODO Enhance and factorize
132 private static Subject
currentSubject() {
133 CmsView cmsView
= CmsUtils
.getCmsView();
135 return cmsView
.getSubject();
136 Subject subject
= Subject
.getSubject(AccessController
.getContext());
139 throw new CmsException("Cannot find related subject");
144 * Simply retrieves the *relative* path to the current user home node from
147 public static String
getCurrentUserHomeRelPath() {
148 return getHomeRelPath(getCurrentUsername());
152 * Simply retrieves the *relative* path to the home node of a user given its
155 public static String
getHomeRelPath(String userName
) {
156 String id
= getUserUid(userName
);
157 String currHomePath
= JcrUtils
.firstCharsToPath(id
, 2) + "/" + id
;
161 // HELPERS TO RETRIEVE REMARKABLE PROPERTIES
162 /** Simply retrieves the user uid from his dn with no useradmin */
163 public static String
getUserUid(String dn
) {
164 LdapName ldapName
= getLdapName(dn
);
165 Rdn last
= ldapName
.getRdn(ldapName
.size() - 1);
166 if (last
.getType().toLowerCase().equals(LdifName
.uid
.name())
167 || last
.getType().toLowerCase().equals(LdifName
.cn
.name()))
168 return (String
) last
.getValue();
170 throw new ArgeoException("Cannot retrieve user uid, "
171 + "non valid dn: " + dn
);
175 * Returns the local username if no user with this dn is found or if the
176 * found user has no defined display name
178 public static String
getUserDisplayName(UserAdmin userAdmin
, String dn
) {
179 Role user
= getRole(userAdmin
, getLdapName(dn
));
181 return getUserUid(dn
);
182 String displayName
= getProperty(user
, LdifName
.displayName
.name());
183 if (EclipseUiUtils
.isEmpty(displayName
))
184 displayName
= getProperty(user
, LdifName
.cn
.name());
185 if (EclipseUiUtils
.isEmpty(displayName
))
186 return getUserUid(dn
);
192 * Returns null if no user with this dn is found or if the found user has no
195 public static String
getUserMail(UserAdmin userAdmin
, String dn
) {
196 Role user
= getRole(userAdmin
, getLdapName(dn
));
200 return getProperty(user
, LdifName
.mail
.name());
203 // VARIOUS UI HELPERS
204 public final static String
buildDefaultCn(String firstName
, String lastName
) {
205 return (firstName
.trim() + " " + lastName
.trim() + " ").trim();
208 /** Simply retrieves a display name of the relevant domain */
209 public final static String
getDomainName(User user
) {
210 String dn
= user
.getName();
211 if (dn
.endsWith(AuthConstants
.ROLES_BASEDN
))
212 return "System roles";
214 LdapName name
= new LdapName(dn
);
215 List
<Rdn
> rdns
= name
.getRdns();
218 loop
: while (i
< rdns
.size()) {
219 Rdn currrRdn
= rdns
.get(i
);
220 if (!LdifName
.dc
.name().equals(currrRdn
.getType()))
223 String currVal
= (String
) currrRdn
.getValue();
224 dname
= dname
== null ? currVal
: currVal
+ "." + dname
;
229 } catch (InvalidNameException e
) {
230 throw new ArgeoException("Unable to get domain name for " + dn
, e
);
235 /** Simply retrieves a LDAP name from a dn with no exception */
236 public static LdapName
getLdapName(String dn
) {
238 return new LdapName(dn
);
239 } catch (InvalidNameException e
) {
240 throw new ArgeoException("Cannot parse LDAP name " + dn
, e
);