]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - src/org/argeo/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
.cms
.CmsConstants
;
10 import org
.argeo
.util
.naming
.LdapAttrs
;
11 import org
.osgi
.service
.useradmin
.Role
;
12 import org
.osgi
.service
.useradmin
.User
;
13 import org
.osgi
.service
.useradmin
.UserAdmin
;
15 /** Centralise common patterns to manage users with a {@link UserAdmin} */
16 public class UserAdminUtils
{
18 // CURRENTUSER HELPERS
19 /** Checks if current user is the same as the passed one */
20 public static boolean isCurrentUser(User user
) {
21 String userUsername
= getProperty(user
, LdapAttrs
.DN
);
22 LdapName userLdapName
= getLdapName(userUsername
);
23 LdapName selfUserName
= getCurrentUserLdapName();
24 return userLdapName
.equals(selfUserName
);
27 /** Retrieves the current logged-in {@link User} */
28 public static User
getCurrentUser(UserAdmin userAdmin
) {
29 return (User
) userAdmin
.getRole(CurrentUser
.getUsername());
32 /** Retrieves the current logged-in user {@link LdapName} */
33 public final static LdapName
getCurrentUserLdapName() {
34 String name
= CurrentUser
.getUsername();
35 return getLdapName(name
);
38 /** Retrieves the current logged-in user mail */
39 public static String
getCurrentUserMail(UserAdmin userAdmin
) {
40 String username
= CurrentUser
.getUsername();
41 return getUserMail(userAdmin
, username
);
44 /** Retrieves the current logged-in user common name */
45 public final static String
getCommonName(User user
) {
46 return getProperty(user
, LdapAttrs
.cn
.name());
49 // OTHER USERS HELPERS
51 * Retrieves the local id of a user or group, that is respectively the uid or cn
52 * of the passed dn with no {@link UserAdmin}
54 public static String
getUserLocalId(String dn
) {
55 LdapName ldapName
= getLdapName(dn
);
56 Rdn last
= ldapName
.getRdn(ldapName
.size() - 1);
57 if (last
.getType().toLowerCase().equals(LdapAttrs
.uid
.name())
58 || last
.getType().toLowerCase().equals(LdapAttrs
.cn
.name()))
59 return (String
) last
.getValue();
61 throw new IllegalArgumentException("Cannot retrieve user local id, non valid dn: " + dn
);
65 * Returns the local username if no user with this dn is found or if the found
66 * user has no defined display name
68 public static String
getUserDisplayName(UserAdmin userAdmin
, String dn
) {
69 Role user
= userAdmin
.getRole(dn
);
72 dName
= getUserLocalId(dn
);
74 dName
= getProperty(user
, LdapAttrs
.displayName
.name());
76 dName
= getProperty(user
, LdapAttrs
.cn
.name());
78 dName
= getUserLocalId(dn
);
84 * Returns null if no user with this dn is found or if the found user has no
87 public static String
getUserMail(UserAdmin userAdmin
, String dn
) {
88 Role user
= userAdmin
.getRole(dn
);
92 return getProperty(user
, LdapAttrs
.mail
.name());
97 * Easily retrieves one of the {@link Role}'s property or an empty String if the
98 * requested property is not defined
100 public final static String
getProperty(Role role
, String key
) {
101 Object obj
= role
.getProperties().get(key
);
108 public final static String
getProperty(Role role
, Enum
<?
> key
) {
109 Object obj
= role
.getProperties().get(key
.name());
116 public final static void setProperty(Role role
, String key
, String value
) {
117 role
.getProperties().put(key
, value
);
120 public final static void setProperty(Role role
, Enum
<?
> key
, String value
) {
121 setProperty(role
, key
.name(), value
);
125 * Simply retrieves a LDAP name from a {@link LdapAttrs.DN} with no exception
127 private static LdapName
getLdapName(String dn
) {
129 return new LdapName(dn
);
130 } catch (InvalidNameException e
) {
131 throw new IllegalArgumentException("Cannot parse LDAP name " + dn
, e
);
135 /** Simply retrieves a display name of the relevant domain */
136 public final static String
getDomainName(User user
) {
137 String dn
= user
.getName();
138 if (dn
.endsWith(CmsConstants
.ROLES_BASEDN
))
139 return "System roles";
140 if (dn
.endsWith(CmsConstants
.TOKENS_BASEDN
))
143 // FIXME deal with non-DC
144 LdapName name
= new LdapName(dn
);
145 List
<Rdn
> rdns
= name
.getRdns();
148 loop
: while (i
< rdns
.size()) {
149 Rdn currrRdn
= rdns
.get(i
);
150 if (LdapAttrs
.uid
.name().equals(currrRdn
.getType()) || LdapAttrs
.cn
.name().equals(currrRdn
.getType())
151 || LdapAttrs
.ou
.name().equals(currrRdn
.getType()))
154 String currVal
= (String
) currrRdn
.getValue();
155 dname
= dname
== null ? currVal
: currVal
+ "." + dname
;
160 } catch (InvalidNameException e
) {
161 throw new IllegalArgumentException("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());