X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.app.core%2Fsrc%2Forg%2Fargeo%2Fapp%2Fcore%2FSuiteUtils.java;h=7b614a74ea0939a5d494e0b3b7f90227442db2e6;hb=d6c9d33b61e475914d3f8d7534374ed30eca8150;hp=0a31b1424e640df0d1aae1b4eaabbc011ba05659;hpb=942fd932e1f6ed3319575c4c1d944912dfca9fa5;p=gpl%2Fargeo-suite.git diff --git a/org.argeo.app.core/src/org/argeo/app/core/SuiteUtils.java b/org.argeo.app.core/src/org/argeo/app/core/SuiteUtils.java index 0a31b14..7b614a7 100644 --- a/org.argeo.app.core/src/org/argeo/app/core/SuiteUtils.java +++ b/org.argeo.app.core/src/org/argeo/app/core/SuiteUtils.java @@ -3,103 +3,25 @@ package org.argeo.app.core; import java.util.HashSet; import java.util.Set; -import javax.jcr.Node; -import javax.jcr.RepositoryException; -import javax.jcr.Session; -import javax.jcr.nodetype.NodeType; -import javax.jcr.security.Privilege; -import javax.naming.ldap.LdapName; -import javax.security.auth.x500.X500Principal; +import javax.xml.namespace.QName; -import org.argeo.api.cms.CmsConstants; -import org.argeo.api.cms.CmsSession; +import org.argeo.api.acr.Content; +import org.argeo.api.acr.ldap.LdapAttr; +import org.argeo.api.acr.ldap.LdapObj; import org.argeo.app.api.EntityType; -import org.argeo.app.api.SuiteRole; -import org.argeo.jackrabbit.security.JackrabbitSecurityUtils; -import org.argeo.jcr.JcrException; -import org.argeo.jcr.JcrUtils; -import org.argeo.util.naming.LdapAttrs; +import org.argeo.cms.RoleNameUtils; /** Utilities around the Argeo Suite APIs. */ public class SuiteUtils { + public final static String USER_STATE_NODE_NAME = "state"; + public final static String USER_DEVICES_NODE_NAME = "devices"; + public final static String USER_SESSIONS_NODE_NAME = "sessions"; - public static String getUserNodePath(LdapName userDn) { - String uid = userDn.getRdn(userDn.size() - 1).getValue().toString(); + public static String getUserNodePath(String userDn) { + String uid = RoleNameUtils.getLastRdnValue(userDn); return EntityType.user.basePath() + '/' + uid; } - public static Node getOrCreateUserNode(Session adminSession, LdapName userDn) { - try { - Node usersBase = adminSession.getNode(EntityType.user.basePath()); - String uid = userDn.getRdn(userDn.size() - 1).getValue().toString(); - Node userNode; - if (!usersBase.hasNode(uid)) { - userNode = usersBase.addNode(uid, NodeType.NT_UNSTRUCTURED); - userNode.addMixin(EntityType.user.get()); - userNode.addMixin(NodeType.MIX_CREATED); - userNode.setProperty(LdapAttrs.distinguishedName.property(), userDn.toString()); - userNode.setProperty(LdapAttrs.uid.property(), uid); - adminSession.save(); - JackrabbitSecurityUtils.denyPrivilege(adminSession, userNode.getPath(), SuiteRole.coworker.dn(), - Privilege.JCR_READ); - JcrUtils.addPrivilege(adminSession, userNode.getPath(), new X500Principal(userDn.toString()).getName(), - Privilege.JCR_READ); - JcrUtils.addPrivilege(adminSession, userNode.getPath(), CmsConstants.ROLE_USER_ADMIN, - Privilege.JCR_ALL); - } else { - userNode = usersBase.getNode(uid); - } - return userNode; - } catch (RepositoryException e) { - throw new JcrException("Cannot create user node for " + userDn, e); - } - } - - public static Node getCmsSessionNode(Session session, CmsSession cmsSession) { - try { - return session.getNode(getUserNodePath(cmsSession.getUserDn()) + '/' + cmsSession.getUuid().toString()); - } catch (RepositoryException e) { - throw new JcrException("Cannot get session dir for " + cmsSession, e); - } - } - - public static Node getOrCreateCmsSessionNode(Session adminSession, CmsSession cmsSession) { - try { - LdapName userDn = cmsSession.getUserDn(); -// String uid = userDn.get(userDn.size() - 1); - Node userNode = getOrCreateUserNode(adminSession, userDn); -// if (!usersBase.hasNode(uid)) { -// userNode = usersBase.addNode(uid, NodeType.NT_UNSTRUCTURED); -// userNode.addMixin(EntityType.user.get()); -// userNode.addMixin(NodeType.MIX_CREATED); -// usersBase.setProperty(LdapAttrs.uid.property(), uid); -// usersBase.setProperty(LdapAttrs.distinguishedName.property(), userDn.toString()); -// adminSession.save(); -// } else { -// userNode = usersBase.getNode(uid); -// } - String cmsSessionUuid = cmsSession.getUuid().toString(); - Node cmsSessionNode; - if (!userNode.hasNode(cmsSessionUuid)) { - cmsSessionNode = userNode.addNode(cmsSessionUuid, NodeType.NT_UNSTRUCTURED); - cmsSessionNode.addMixin(NodeType.MIX_CREATED); - adminSession.save(); - JcrUtils.addPrivilege(adminSession, cmsSessionNode.getPath(), cmsSession.getUserRole(), - Privilege.JCR_ALL); - } else { - cmsSessionNode = userNode.getNode(cmsSessionUuid); - } - return cmsSessionNode; - } catch (RepositoryException e) { - throw new JcrException("Cannot create session dir for " + cmsSession, e); - } - } - - /** Singleton. */ - private SuiteUtils() { - - } - public static Set extractRoles(String[] semiColArr) { Set res = new HashSet<>(); // TODO factorize and make it more robust @@ -120,4 +42,33 @@ public class SuiteUtils { return res; } + synchronized static public long findNextId(Content hierarchyUnit, QName cclass) { + if (!hierarchyUnit.hasContentClass(LdapObj.posixGroup.qName())) + throw new IllegalArgumentException(hierarchyUnit + " is not a POSIX group"); + + long min = hierarchyUnit.get(LdapAttr.gidNumber.qName(), Long.class).orElseThrow(); + long currentMax = 0l; + for (Content childHu : hierarchyUnit) { + if (!childHu.hasContentClass(LdapObj.organizationalUnit.qName())) + continue; + // FIXME filter out functional hierarchy unit + for (Content role : childHu) { + if (role.hasContentClass(cclass)) { + + if (LdapObj.posixAccount.qName().equals(cclass)) { + Long id = role.get(LdapAttr.uidNumber.qName(), Long.class).orElseThrow(); + if (id > currentMax) + currentMax = id; + } + } + } + } + if (currentMax == 0l) + return min; + return currentMax + 1; + } + + /** Singleton. */ + private SuiteUtils() { + } }