private boolean createLogin(String user, String password) {
   try {
     return userManager.createUser(user, password) != null;
   } catch (AuthorizableExistsException e) {
     e.printStackTrace();
     return true;
   } catch (RepositoryException e) {
     e.printStackTrace();
   }
   return false;
 }
 public IPentahoUser createUser(
     Session session,
     final ITenant theTenant,
     final String userName,
     final String password,
     final String description,
     final String[] roles)
     throws AuthorizableExistsException, RepositoryException {
   ITenant tenant = theTenant;
   String user = userName;
   if (tenant == null) {
     tenant = JcrTenantUtils.getTenant(userName, true);
     user = JcrTenantUtils.getPrincipalName(userName, true);
   }
   if (tenant == null || tenant.getId() == null) {
     tenant = JcrTenantUtils.getCurrentTenant();
   }
   if (!TenantUtils.isAccessibleTenant(tenant)) {
     throw new NotFoundException(
         Messages.getInstance()
             .getString(
                 "AbstractJcrBackedUserRoleDao.ERROR_0006_TENANT_NOT_FOUND", theTenant.getId()));
   }
   String userId = tenantedUserNameUtils.getPrincipleId(tenant, user);
   UserManager tenantUserMgr = getUserManager(tenant, session);
   tenantUserMgr.createUser(userId, password, new PrincipalImpl(userId), ""); // $NON-NLS-1$
   session.save();
   /**
    * This call is absolutely necessary. setUserRolesForNewUser will never * inspect what roles
    * this user is a part of. Since this is a new user * it will not be a part of new roles
    */
   setUserRolesForNewUser(session, tenant, user, roles);
   setUserDescription(session, tenant, user, description);
   session.save();
   createUserHomeFolder(tenant, user, session);
   session.save();
   this.userDetailsCache.removeUserFromCache(userName);
   return getUser(session, tenant, userName);
 }
Пример #3
0
  /*
   * (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, HtmlResponse response, List<Modification> changes)
      throws RepositoryException {
    // make sure user self-registration is enabled
    if (!selfRegistrationEnabled) {
      throw new RepositoryException(
          "Sorry, registration of new users is not currently enabled.  Please try again later.");
    }

    Session session = request.getResourceResolver().adaptTo(Session.class);
    if (session == null) {
      throw new RepositoryException("JCR Session not found");
    }

    // check that the submitted parameter values have valid values.
    String principalName = request.getParameter(SlingPostConstants.RP_NODE_NAME);
    if (principalName == null) {
      throw new RepositoryException("User name was not submitted");
    }
    String pwd = request.getParameter("pwd");
    if (pwd == null) {
      throw new RepositoryException("Password was not submitted");
    }
    String pwdConfirm = request.getParameter("pwdConfirm");
    if (!pwd.equals(pwdConfirm)) {
      throw new RepositoryException("Password value does not match the confirmation password");
    }

    Session selfRegSession = null;
    try {
      selfRegSession = getSession();

      UserManager userManager = AccessControlUtil.getUserManager(selfRegSession);
      Authorizable authorizable = userManager.getAuthorizable(principalName);

      if (authorizable != null) {
        // user already exists!
        throw new RepositoryException(
            "A principal already exists with the requested name: " + principalName);
      } else {
        Map<String, RequestProperty> reqProperties = collectContent(request, response);

        User user = userManager.createUser(principalName, digestPassword(pwd));
        String userPath =
            AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PREFIX + user.getID();

        response.setPath(userPath);
        response.setLocation(externalizePath(request, userPath));
        response.setParentLocation(
            externalizePath(request, AuthorizableResourceProvider.SYSTEM_USER_MANAGER_USER_PATH));
        changes.add(Modification.onCreated(userPath));

        // write content from form
        writeContent(selfRegSession, user, reqProperties, changes);

        if (selfRegSession.hasPendingChanges()) {
          selfRegSession.save();
        }
      }
    } finally {
      ungetSession(selfRegSession);
    }
  }