Search in sources :

Example 1 with CreateGroup

use of org.apache.sling.jackrabbit.usermanager.CreateGroup in project sling by apache.

the class CreateGroupServlet method handleOperation.

/*
     * (non-Javadoc)
     * @see
     * org.apache.sling.jackrabbit.usermanager.post.AbstractAuthorizablePostServlet
     * #handleOperation(org.apache.sling.api.SlingHttpServletRequest,
     * org.apache.sling.api.servlets.HtmlResponse, java.util.List)
     */
@Override
protected void handleOperation(SlingHttpServletRequest request, AbstractPostResponse response, List<Modification> changes) throws RepositoryException {
    Session session = request.getResourceResolver().adaptTo(Session.class);
    String principalName = request.getParameter(SlingPostConstants.RP_NODE_NAME);
    Group group = createGroup(session, principalName, request.getRequestParameterMap(), changes);
    String groupPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID();
    response.setPath(groupPath);
    response.setLocation(externalizePath(request, groupPath));
    response.setParentLocation(externalizePath(request, AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PATH));
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) CreateGroup(org.apache.sling.jackrabbit.usermanager.CreateGroup) Session(javax.jcr.Session)

Example 2 with CreateGroup

use of org.apache.sling.jackrabbit.usermanager.CreateGroup in project sling by apache.

the class CreateGroupServlet method createGroup.

/* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.CreateGroup#createGroup(javax.jcr.Session, java.lang.String, java.util.Map, java.util.List)
     */
public Group createGroup(Session jcrSession, final String name, Map<String, ?> properties, List<Modification> changes) throws RepositoryException {
    // check that the parameter values have valid values.
    if (jcrSession == null) {
        throw new IllegalArgumentException("JCR Session not found");
    }
    if (name == null || name.length() == 0) {
        throw new IllegalArgumentException("Group name was not supplied");
    }
    UserManager userManager = AccessControlUtil.getUserManager(jcrSession);
    Authorizable authorizable = userManager.getAuthorizable(name);
    Group group = null;
    if (authorizable != null) {
        // principal already exists!
        throw new RepositoryException("A group already exists with the requested name: " + name);
    } else {
        group = userManager.createGroup(new Principal() {

            public String getName() {
                return name;
            }
        });
        String groupPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PREFIX + group.getID();
        Collection<RequestProperty> reqProperties = collectContent(properties);
        changes.add(Modification.onCreated(groupPath));
        // write content from form
        writeContent(jcrSession, group, reqProperties, changes);
        // update the group memberships
        ResourceResolver resourceResolver = null;
        try {
            //create a resource resolver to resolve the relative paths used for group membership values
            final Map<String, Object> authInfo = new HashMap<String, Object>();
            authInfo.put(org.apache.sling.jcr.resource.api.JcrResourceConstants.AUTHENTICATION_INFO_SESSION, jcrSession);
            resourceResolver = resourceResolverFactory.getResourceResolver(authInfo);
            Resource baseResource = resourceResolver.getResource(AuthorizableResourceProvider.SYSTEM_USER_MANAGER_GROUP_PATH);
            updateGroupMembership(baseResource, properties, group, changes);
        } catch (LoginException e) {
            throw new RepositoryException(e);
        } finally {
            if (resourceResolver != null) {
                resourceResolver.close();
            }
        }
    }
    return group;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) CreateGroup(org.apache.sling.jackrabbit.usermanager.CreateGroup) HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) RepositoryException(javax.jcr.RepositoryException) RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) LoginException(org.apache.sling.api.resource.LoginException) Principal(java.security.Principal)

Aggregations

Group (org.apache.jackrabbit.api.security.user.Group)2 CreateGroup (org.apache.sling.jackrabbit.usermanager.CreateGroup)2 Principal (java.security.Principal)1 HashMap (java.util.HashMap)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)1 UserManager (org.apache.jackrabbit.api.security.user.UserManager)1 LoginException (org.apache.sling.api.resource.LoginException)1 Resource (org.apache.sling.api.resource.Resource)1 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)1 RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)1