]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/auth/UserAdminUtils.java
1 package org
.argeo
.cms
.auth
;
5 import javax
.naming
.InvalidNameException
;
6 import javax
.naming
.ldap
.LdapName
;
7 import javax
.naming
.ldap
.Rdn
;
9 import org
.argeo
.api
.NodeConstants
;
10 import org
.argeo
.cms
.CmsException
;
11 import org
.argeo
.naming
.LdapAttrs
;
12 import org
.osgi
.service
.useradmin
.Role
;
13 import org
.osgi
.service
.useradmin
.User
;
14 import org
.osgi
.service
.useradmin
.UserAdmin
;
16 /** Centralise common patterns to manage users with a {@link UserAdmin} */
17 public class UserAdminUtils
{
19 // CURRENTUSER HELPERS
20 /** Checks if current user is the same as the passed one */
21 public static boolean isCurrentUser(User user
) {
22 String userUsername
= getProperty(user
, LdapAttrs
.DN
);
23 LdapName userLdapName
= getLdapName(userUsername
);
24 LdapName selfUserName
= getCurrentUserLdapName();
25 return userLdapName
.equals(selfUserName
);
28 /** Retrieves the current logged-in {@link User} */
29 public static User
getCurrentUser(UserAdmin userAdmin
) {
30 return (User
) userAdmin
.getRole(CurrentUser
.getUsername());
33 /** Retrieves the current logged-in user {@link LdapName} */
34 public final static LdapName
getCurrentUserLdapName() {
35 String name
= CurrentUser
.getUsername();
36 return getLdapName(name
);
39 /** Retrieves the current logged-in user mail */
40 public static String
getCurrentUserMail(UserAdmin userAdmin
) {
41 String username
= CurrentUser
.getUsername();
42 return getUserMail(userAdmin
, username
);
45 /** Retrieves the current logged-in user common name */
46 public final static String
getCommonName(User user
) {
47 return getProperty(user
, LdapAttrs
.cn
.name());
50 // OTHER USERS HELPERS
52 * Retrieves the local id of a user or group, that is respectively the uid or cn
53 * of the passed dn with no {@link UserAdmin}
55 public static String
getUserLocalId(String dn
) {
56 LdapName ldapName
= getLdapName(dn
);
57 Rdn last
= ldapName
.getRdn(ldapName
.size() - 1);
58 if (last
.getType().toLowerCase().equals(LdapAttrs
.uid
.name())
59 || last
.getType().toLowerCase().equals(LdapAttrs
.cn
.name()))
60 return (String
) last
.getValue();
62 throw new CmsException("Cannot retrieve user local id, non valid dn: " + dn
);
66 * Returns the local username if no user with this dn is found or if the found
67 * user has no defined display name
69 public static String
getUserDisplayName(UserAdmin userAdmin
, String dn
) {
70 Role user
= userAdmin
.getRole(dn
);
73 dName
= getUserLocalId(dn
);
75 dName
= getProperty(user
, LdapAttrs
.displayName
.name());
77 dName
= getProperty(user
, LdapAttrs
.cn
.name());
79 dName
= getUserLocalId(dn
);
85 * Returns null if no user with this dn is found or if the found user has no
88 public static String
getUserMail(UserAdmin userAdmin
, String dn
) {
89 Role user
= userAdmin
.getRole(dn
);
93 return getProperty(user
, LdapAttrs
.mail
.name());
98 * Easily retrieves one of the {@link Role}'s property or an empty String if the
99 * requested property is not defined
101 public final static String
getProperty(Role role
, String key
) {
102 Object obj
= role
.getProperties().get(key
);
109 public final static String
getProperty(Role role
, Enum
<?
> key
) {
110 Object obj
= role
.getProperties().get(key
.name());
117 public final static void setProperty(Role role
, String key
, String value
) {
118 role
.getProperties().put(key
, value
);
121 public final static void setProperty(Role role
, Enum
<?
> key
, String value
) {
122 setProperty(role
, key
.name(), value
);
126 * Simply retrieves a LDAP name from a {@link LdapAttrs.DN} with no exception
128 private static LdapName
getLdapName(String dn
) {
130 return new LdapName(dn
);
131 } catch (InvalidNameException e
) {
132 throw new CmsException("Cannot parse LDAP name " + dn
, e
);
136 /** Simply retrieves a display name of the relevant domain */
137 public final static String
getDomainName(User user
) {
138 String dn
= user
.getName();
139 if (dn
.endsWith(NodeConstants
.ROLES_BASEDN
))
140 return "System roles";
141 if (dn
.endsWith(NodeConstants
.TOKENS_BASEDN
))
144 // FIXME deal with non-DC
145 LdapName name
= new LdapName(dn
);
146 List
<Rdn
> rdns
= name
.getRdns();
149 loop
: while (i
< rdns
.size()) {
150 Rdn currrRdn
= rdns
.get(i
);
151 if (!LdapAttrs
.dc
.name().equals(currrRdn
.getType()))
154 String currVal
= (String
) currrRdn
.getValue();
155 dname
= dname
== null ? currVal
: currVal
+ "." + dname
;
160 } catch (InvalidNameException e
) {
161 throw new CmsException("Unable to get domain name for " + dn
, e
);
166 public final static String
buildDefaultCn(String firstName
, String lastName
) {
167 return (firstName
.trim() + " " + lastName
.trim() + " ").trim();
170 /** Simply checks if a string is null or empty */
171 private static boolean isEmpty(String stringToTest
) {
172 return stringToTest
== null || "".equals(stringToTest
.trim());