]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/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
.acr
.ldap
.LdapAttr
;
10 import org
.argeo
.api
.cms
.CmsConstants
;
11 import org
.argeo
.cms
.CurrentUser
;
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
, LdapAttr
.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
, LdapAttr
.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(LdapAttr
.uid
.name())
59 || last
.getType().toLowerCase().equals(LdapAttr
.cn
.name()))
60 return (String
) last
.getValue();
62 throw new IllegalArgumentException("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
);
72 return getUserLocalId(dn
);
73 return getUserDisplayName(user
);
76 public static String
getUserDisplayName(Role user
) {
77 String dName
= getProperty(user
, LdapAttr
.displayName
.name());
79 dName
= getProperty(user
, LdapAttr
.cn
.name());
81 dName
= getProperty(user
, LdapAttr
.uid
.name());
83 dName
= getUserLocalId(user
.getName());
88 * Returns null if no user with this dn is found or if the found user has no
91 public static String
getUserMail(UserAdmin userAdmin
, String dn
) {
92 Role user
= userAdmin
.getRole(dn
);
96 return getProperty(user
, LdapAttr
.mail
.name());
101 * Easily retrieves one of the {@link Role}'s property or an empty String if the
102 * requested property is not defined
104 public final static String
getProperty(Role role
, String key
) {
105 Object obj
= role
.getProperties().get(key
);
112 public final static String
getProperty(Role role
, Enum
<?
> key
) {
113 Object obj
= role
.getProperties().get(key
.name());
120 public final static void setProperty(Role role
, String key
, String value
) {
121 role
.getProperties().put(key
, value
);
124 public final static void setProperty(Role role
, Enum
<?
> key
, String value
) {
125 setProperty(role
, key
.name(), value
);
129 * Simply retrieves a LDAP name from a {@link LdapAttr.DN} with no exception
131 private static LdapName
getLdapName(String dn
) {
133 return new LdapName(dn
);
134 } catch (InvalidNameException e
) {
135 throw new IllegalArgumentException("Cannot parse LDAP name " + dn
, e
);
139 /** Simply retrieves a display name of the relevant domain */
140 public final static String
getDomainName(User user
) {
141 String dn
= user
.getName();
142 if (dn
.endsWith(CmsConstants
.SYSTEM_ROLES_BASEDN
))
143 return "System roles";
144 if (dn
.endsWith(CmsConstants
.TOKENS_BASEDN
))
147 // FIXME deal with non-DC
148 LdapName name
= new LdapName(dn
);
149 List
<Rdn
> rdns
= name
.getRdns();
152 loop
: while (i
< rdns
.size()) {
153 Rdn currrRdn
= rdns
.get(i
);
154 if (LdapAttr
.uid
.name().equals(currrRdn
.getType()) || LdapAttr
.cn
.name().equals(currrRdn
.getType())
155 || LdapAttr
.ou
.name().equals(currrRdn
.getType()))
158 String currVal
= (String
) currrRdn
.getValue();
159 dname
= dname
== null ? currVal
: currVal
+ "." + dname
;
164 } catch (InvalidNameException e
) {
165 throw new IllegalArgumentException("Unable to get domain name for " + dn
, e
);
170 public final static String
buildDefaultCn(String firstName
, String lastName
) {
171 return (firstName
.trim() + " " + lastName
.trim() + " ").trim();
174 /** Simply checks if a string is null or empty */
175 private static boolean isEmpty(String stringToTest
) {
176 return stringToTest
== null || "".equals(stringToTest
.trim());