]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.core/src/org/argeo/app/core/SuiteUtils.java
Releasing
[gpl/argeo-suite.git] / org.argeo.app.core / src / org / argeo / app / core / SuiteUtils.java
1 package org.argeo.app.core;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import javax.xml.namespace.QName;
7
8 import org.argeo.api.acr.Content;
9 import org.argeo.api.acr.ldap.LdapAttr;
10 import org.argeo.api.acr.ldap.LdapObj;
11 import org.argeo.app.api.EntityType;
12 import org.argeo.cms.RoleNameUtils;
13
14 /** Utilities around the Argeo Suite APIs. */
15 public class SuiteUtils {
16 public final static String USER_STATE_NODE_NAME = "state";
17 public final static String USER_DEVICES_NODE_NAME = "devices";
18 public final static String USER_SESSIONS_NODE_NAME = "sessions";
19
20 public static String getUserNodePath(String userDn) {
21 String uid = RoleNameUtils.getLastRdnValue(userDn);
22 return EntityType.user.basePath() + '/' + uid;
23 }
24
25 public static Set<String> extractRoles(String[] semiColArr) {
26 Set<String> res = new HashSet<>();
27 // TODO factorize and make it more robust
28 final String rolesPrefix = "roles:=\"";
29 // first one is layer id
30 for (int i = 1; i < semiColArr.length; i++) {
31 if (semiColArr[i].startsWith(rolesPrefix)) {
32 String rolesStr = semiColArr[i].substring(rolesPrefix.length());
33 // remove last "
34 rolesStr = rolesStr.substring(0, rolesStr.lastIndexOf('\"'));
35 // TODO support AND (&) as well
36 String[] roles = rolesStr.split("\\|");// OR (|)
37 for (String role : roles) {
38 res.add(role.trim());
39 }
40 }
41 }
42 return res;
43 }
44
45 synchronized static public long findNextId(Content hierarchyUnit, QName cclass) {
46 if (!hierarchyUnit.hasContentClass(LdapObj.posixGroup.qName()))
47 throw new IllegalArgumentException(hierarchyUnit + " is not a POSIX group");
48
49 long min = hierarchyUnit.get(LdapAttr.gidNumber.qName(), Long.class).orElseThrow();
50 long currentMax = 0l;
51 for (Content childHu : hierarchyUnit) {
52 if (!childHu.hasContentClass(LdapObj.organizationalUnit.qName()))
53 continue;
54 // FIXME filter out functional hierarchy unit
55 for (Content role : childHu) {
56 if (role.hasContentClass(cclass)) {
57
58 if (LdapObj.posixAccount.qName().equals(cclass)) {
59 Long id = role.get(LdapAttr.uidNumber.qName(), Long.class).orElseThrow();
60 if (id > currentMax)
61 currentMax = id;
62 }
63 }
64 }
65 }
66 if (currentMax == 0l)
67 return min;
68 return currentMax + 1;
69 }
70
71 /** Singleton. */
72 private SuiteUtils() {
73 }
74 }