Search in sources :

Example 1 with CreateUser

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

the class CreateUserServlet method createUser.

/* (non-Javadoc)
     * @see org.apache.sling.jackrabbit.usermanager.CreateUser#createUser(javax.jcr.Session, java.lang.String, java.lang.String, java.lang.String, java.util.Map, java.util.List)
     */
public User createUser(Session jcrSession, String name, String password, String passwordConfirm, Map<String, ?> properties, List<Modification> changes) throws RepositoryException {
    if (jcrSession == null) {
        throw new RepositoryException("JCR Session not found");
    }
    // check for an administrator
    boolean administrator = false;
    try {
        UserManager um = AccessControlUtil.getUserManager(jcrSession);
        User currentUser = (User) um.getAuthorizable(jcrSession.getUserID());
        administrator = currentUser.isAdmin();
        if (!administrator) {
            //check if the user is a member of the 'User administrator' group
            Authorizable userAdmin = um.getAuthorizable(this.userAdminGroupName);
            if (userAdmin instanceof Group) {
                boolean isMember = ((Group) userAdmin).isMember(currentUser);
                if (isMember) {
                    administrator = true;
                }
            }
        }
    } catch (Exception ex) {
        log.warn("Failed to determine if the user is an admin, assuming not. Cause: " + ex.getMessage());
        administrator = false;
    }
    // make sure user self-registration is enabled
    if (!administrator && !selfRegistrationEnabled) {
        throw new RepositoryException("Sorry, registration of new users is not currently enabled.  Please try again later.");
    }
    // check that the submitted parameter values have valid values.
    if (name == null || name.length() == 0) {
        throw new RepositoryException("User name was not submitted");
    }
    if (password == null) {
        throw new RepositoryException("Password was not submitted");
    }
    if (!password.equals(passwordConfirm)) {
        throw new RepositoryException("Password value does not match the confirmation password");
    }
    User user = null;
    Session selfRegSession = jcrSession;
    boolean useAdminSession = !administrator && selfRegistrationEnabled;
    try {
        if (useAdminSession) {
            //the current user doesn't have permission to create the user,
            // but self-registration is enabled, so use an admin session
            // to do the work.
            selfRegSession = getSession();
        }
        UserManager userManager = AccessControlUtil.getUserManager(selfRegSession);
        Authorizable authorizable = userManager.getAuthorizable(name);
        if (authorizable != null) {
            // user already exists!
            throw new RepositoryException("A principal already exists with the requested name: " + name);
        } else {
            user = userManager.createUser(name, password);
            String userPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PREFIX + user.getID();
            Collection<RequestProperty> reqProperties = collectContent(properties);
            changes.add(Modification.onCreated(userPath));
            // write content from form
            writeContent(selfRegSession, user, reqProperties, changes);
            if (selfRegSession.hasPendingChanges()) {
                selfRegSession.save();
            }
            if (useAdminSession) {
                //lookup the user from the user session so we can return a live object
                UserManager userManager2 = AccessControlUtil.getUserManager(jcrSession);
                Authorizable authorizable2 = userManager2.getAuthorizable(user.getID());
                if (authorizable2 instanceof User) {
                    user = (User) authorizable2;
                } else {
                    user = null;
                }
            }
        }
    } finally {
        if (useAdminSession) {
            //done with the self-reg admin session, so clean it up
            ungetSession(selfRegSession);
        }
    }
    return user;
}
Also used : Group(org.apache.jackrabbit.api.security.user.Group) User(org.apache.jackrabbit.api.security.user.User) CreateUser(org.apache.sling.jackrabbit.usermanager.CreateUser) RequestProperty(org.apache.sling.servlets.post.impl.helper.RequestProperty) UserManager(org.apache.jackrabbit.api.security.user.UserManager) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) RepositoryException(javax.jcr.RepositoryException) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 2 with CreateUser

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

the class CreateUserServlet 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);
    User user = createUser(session, principalName, request.getParameter("pwd"), request.getParameter("pwdConfirm"), request.getRequestParameterMap(), changes);
    String userPath = null;
    if (user == null) {
        if (changes.size() > 0) {
            Modification modification = changes.get(0);
            if (modification.getType() == ModificationType.CREATE) {
                userPath = modification.getSource();
            }
        }
    } else {
        userPath = AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PREFIX + user.getID();
    }
    if (userPath != null) {
        response.setPath(userPath);
        response.setLocation(externalizePath(request, userPath));
    }
    response.setParentLocation(externalizePath(request, AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PATH));
}
Also used : Modification(org.apache.sling.servlets.post.Modification) User(org.apache.jackrabbit.api.security.user.User) CreateUser(org.apache.sling.jackrabbit.usermanager.CreateUser) Session(javax.jcr.Session)

Aggregations

Session (javax.jcr.Session)2 User (org.apache.jackrabbit.api.security.user.User)2 CreateUser (org.apache.sling.jackrabbit.usermanager.CreateUser)2 RepositoryException (javax.jcr.RepositoryException)1 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)1 Group (org.apache.jackrabbit.api.security.user.Group)1 UserManager (org.apache.jackrabbit.api.security.user.UserManager)1 Modification (org.apache.sling.servlets.post.Modification)1 RequestProperty (org.apache.sling.servlets.post.impl.helper.RequestProperty)1