]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/util/useradmin/UserAdminUtils.java
1 package org
.argeo
.cms
.util
.useradmin
;
6 import javax
.naming
.InvalidNameException
;
7 import javax
.naming
.ldap
.LdapName
;
8 import javax
.naming
.ldap
.Rdn
;
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
;
21 /** Centralise common patterns to manage roles with a user admin */
22 public class UserAdminUtils
{
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());
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());
36 username
= getProperty(user
, LdapAttrs
.uid
.name());
41 * Easily retrieves one of the {@link Role}'s property or an empty String if
42 * the requested property is not defined
44 public final static String
getProperty(Role role
, String key
) {
45 Object obj
= role
.getProperties().get(key
);
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();
58 /** Simply checks if current user as a home */
59 public static boolean hasHome() {
60 return isRegistered();
64 /** Simply retrieves the current logged-in user display name. */
65 public static User
getCurrentUser(UserAdmin userAdmin
) {
66 return (User
) getRole(userAdmin
, getCurrentUserLdapName());
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
);
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
);
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
));
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
);
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
);
97 LdapName selfUserName
= getCurrentUserLdapName();
98 LdapName userLdapName
= new LdapName(userName
);
99 if (userLdapName
.equals(selfUserName
))
103 } catch (InvalidNameException e
) {
104 throw new CmsException("User " + user
+ " has an unvalid dn: " + userName
, e
);
108 public final static LdapName
getCurrentUserLdapName() {
109 String name
= CurrentUser
.getUsername();
110 return getLdapName(name
);
114 * Simply retrieves username for current user, generally a LDAP dn
116 * @deprecated Use {@link CurrentUser#getUsername()}
119 public static String
getCurrentUsername() {
120 return CurrentUser
.getUsername();
124 // * Fork of the {@link CurrentUser#currentSubject} method that is private.
125 // * TODO Enhance and factorize
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)
134 // throw new RuntimeException("Cannot find related subject");
139 * Simply retrieves the *relative* path to the current user home node from
142 public static String
getCurrentUserHomeRelPath() {
143 return getHomeRelPath(CurrentUser
.getUsername());
147 * Simply retrieves the *relative* path to the home node of a user given its
150 public static String
getHomeRelPath(String userName
) {
151 String id
= getUserUid(userName
);
152 String currHomePath
= JcrUtils
.firstCharsToPath(id
, 2) + "/" + id
;
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();
165 throw new CmsException("Cannot retrieve user uid, " + "non valid dn: " + dn
);
169 * Returns the local username if no user with this dn is found or if the
170 * found user has no defined display name
172 public static String
getUserDisplayName(UserAdmin userAdmin
, String dn
) {
173 Role user
= getRole(userAdmin
, getLdapName(dn
));
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
);
186 * Returns null if no user with this dn is found or if the found user has no
189 public static String
getUserMail(UserAdmin userAdmin
, String dn
) {
190 Role user
= getRole(userAdmin
, getLdapName(dn
));
194 return getProperty(user
, LdapAttrs
.mail
.name());
197 // VARIOUS UI HELPERS
198 public final static String
buildDefaultCn(String firstName
, String lastName
) {
199 return (firstName
.trim() + " " + lastName
.trim() + " ").trim();
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";
208 LdapName name
= new LdapName(dn
);
209 List
<Rdn
> rdns
= name
.getRdns();
212 loop
: while (i
< rdns
.size()) {
213 Rdn currrRdn
= rdns
.get(i
);
214 if (!LdapAttrs
.dc
.name().equals(currrRdn
.getType()))
217 String currVal
= (String
) currrRdn
.getValue();
218 dname
= dname
== null ? currVal
: currVal
+ "." + dname
;
223 } catch (InvalidNameException e
) {
224 throw new CmsException("Unable to get domain name for " + dn
, e
);
229 /** Simply retrieves a LDAP name from a dn with no exception */
230 public static LdapName
getLdapName(String dn
) {
232 return new LdapName(dn
);
233 } catch (InvalidNameException e
) {
234 throw new CmsException("Cannot parse LDAP name " + dn
, e
);