I am compiling an interface to the user manager. I made some nodes and
corresponding scripts to gather the data, that gets written out in
custom json to be consumed by a dojo store. The code below works fine,
still I'd like to know if it shows the /sling/ way of doing this, or if
I missed some shortcut. (I removed all error-checking, hiding of "jcr:"
properties etc. and kept the repetitive setup of basic structures.)
# to get a list of users and some of their properties:
var jcrSession =
request.getResourceResolver().adaptTo(Packages.javax.jcr.Session);
var userManager =
Packages.org.apache.sling.jcr.base.util.AccessControlUtil.getUserManager(jcrSession);
var principalManager =
Packages.org.apache.sling.jcr.base.util.AccessControlUtil.getPrincipalManager(jcrSession);
var principals =
principalManager.getPrincipals(Packages.org.apache.jackrabbit.api.security.principal.PrincipalManager.SEARCH_TYPE_NOT_GROUP);
while (principals.hasNext()) {
var principal = principals.nextPrincipal();
var authorizable = userManager.getAuthorizable(principal.name);
var uid = authorizable.ID;
var property1 = authorizable.getProperty("Property1")[0].getString();
}
# to get a list of groups and some of their properties:
var jcrSession =
request.getResourceResolver().adaptTo(Packages.javax.jcr.Session);
var userManager =
Packages.org.apache.sling.jcr.base.util.AccessControlUtil.getUserManager(jcrSession);
var principalManager =
Packages.org.apache.sling.jcr.base.util.AccessControlUtil.getPrincipalManager(jcrSession);
var principals =
principalManager.getPrincipals(Packages.org.apache.jackrabbit.api.security.principal.PrincipalManager.SEARCH_TYPE_GROUP);
while (principals.hasNext()) {
var principal = principals.nextPrincipal();
var authorizable = userManager.getAuthorizable(principal.name);
var gid = authorizable.ID;
var property1 = authorizable.getProperty("Property1")[0].getString();
}
# to get all the properties of user "admin":
var principalID = "admin";
var jcrSession =
request.getResourceResolver().adaptTo(Packages.javax.jcr.Session);
var userManager =
Packages.org.apache.sling.jcr.base.util.AccessControlUtil.getUserManager(jcrSession);
var authorizable = userManager.getAuthorizable(principalID);
var names = authorizable.getPropertyNames();
while (names.hasNext()) {
var name = names.next();
var value = authorizable.getProperty(name)[0].getString();
}
# to get all the properties of group "administrators":
var principalID = "administrators";
var jcrSession =
request.getResourceResolver().adaptTo(Packages.javax.jcr.Session);
var userManager =
Packages.org.apache.sling.jcr.base.util.AccessControlUtil.getUserManager(jcrSession);
var authorizable = userManager.getAuthorizable(principalID);
var names = authorizable.getPropertyNames();
while (names.hasNext()) {
var name = names.next();
var value = authorizable.getProperty(name)[0].getString();
}
# to get a list of groups for user "admin":
var principalID = "admin";
var jcrSession =
request.getResourceResolver().adaptTo(Packages.javax.jcr.Session);
var userManager =
Packages.org.apache.sling.jcr.base.util.AccessControlUtil.getUserManager(jcrSession);
var authorizable = userManager.getAuthorizable(principalID);
var names = authorizable.memberOf();
while (names.hasNext()) {
var group = names.next();
var id = group.ID;
}
# to get a list of members for group "administrators":
var principalID = "administrators";
var jcrSession =
request.getResourceResolver().adaptTo(Packages.javax.jcr.Session);
var userManager =
Packages.org.apache.sling.jcr.base.util.AccessControlUtil.getUserManager(jcrSession);
var authorizable = userManager.getAuthorizable(principalID);
var names = authorizable.getMembers();
while (names.hasNext()) {
var user = names.next();
var id = user.ID;
}
--
peter