使用Java代码中的用户权限在adobe CQ中创建新的AEM用户

时间:2015-09-07 10:05:45

标签: adobe aem sling

如何在java代码中创建具有用户权限的新adobe aem用户。

使用SlingAllMethodsServlet,您可以从Post Request获取参数。

您可以使用给定的信息创建用户。用户将被添加到CQ服务器中,并拥有您给他的权利。

   username = request.getParameter("username");
    password = request.getParameter("password");
    givenname = request.getParameter("givenname");
    name = request.getParameter("name");
    email = request.getParameter("email");

    ResourceResolver resourceResolver = request.getResourceResolver();

    Session session = resourceResolver.adaptTo(Session.class);

    UserManager userManager = resourceResolver.adaptTo(UserManager.class);
    User u = null;
    try {
        //check if user does exist, easy check with username, username is id in cq
        u = (User) userManager.getAuthorizable(username);
    } catch (RepositoryException e) {
        logger.error("User cannot be checked: " + e.getMessage());
        e.printStackTrace();
    }

    if(u != null) {
        logger.error("User"  + username + " exists already in CQ!! Can't create user again!");
        try {
            json.put("error", "User already exists");
            response.setStatus(HttpServletResponse.SC_CONFLICT);
        } catch (JSONException e) {
            logger.error(e.getMessage());

        }
    }else {
        /*Just create user if it does not exist*/
        try {

            user = userManager.createUser(username, password);
            ValueFactory valueFactory = session.getValueFactory();
            emailValue = valueFactory.createValue(email);
            givennameValue = valueFactory.createValue(givenname);
            nameValue = valueFactory.createValue(name);

            //User class just accepts Value Object
           user.setProperty("profile/" + UserProperties.EMAIL, emailValue);
           user.setProperty("profile/" + UserProperties.FAMILY_NAME,nameValue);
           user.setProperty("profile/" + UserProperties.GIVEN_NAME, nameValue);



        } catch (RepositoryException e) {
            logger.error("Failed while creating user: " + e.getMessage());
        }


        try {
            /* add Group to user */
            Group g = (Group) userManager.getAuthorizable(GROUP_NAME);
            g.addMember(user);

            session.save();
            session.logout();


        } catch (RepositoryException e) {
            logger.error("Can't add group to new created User : " + username  + e.getMessage());

        }

1 个答案:

答案 0 :(得分:1)

如何获得CQ管理会话/如何获得CQ管理员UserManager

如果您无法通过会话和用户管理器保存用户,那么您必须获得管理会话。

  1. 添加AccessControlUtil的依赖项(检查版本)

  2. 将SlingRepository变量添加为@Reference

  3. 以管理员身份登录以获取管理员会话(参数必须为空,或者是管理员用户,而不是必须创建SimpleCredentials(名称,密码.asChar)
  4. 使用AccessControlUtil和管理会话

    获取Usermanager
    @Reference
    private SlingRepository repository;
    
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    
        try {
            Session adminSession = repository.loginAdministrative(null);
    
            UserManager usermanager = AccessControlUtil.getUserManager(adminSession);
            //your stuff like manipulating user, get authorizab   
    
            adminSession.save();
    
        } catch (RepositoryException e) {
           e.printStackTrace();
        }
    }