Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Programmatically enable disabled account

Avatar

Level 4

I'm trying to programmatically create, disable and enable CQ5 user account. I'm able to create and disable the account but I don't know how to enable it again. Here's my code

org.apache.jackrabbit.api.security.user.UserManager um = request.getResourceResolver().adaptTo(org.apache.jackrabbit.api.security.user.UserManager.class); org.apache.jackrabbit.api.security.user.User user = um.createUser(login, password); user.disable("Waiting for moderation!"); Node node = request.getResourceResolver().getResource(user.getPath()).adaptTo(Node.class); node.setProperty("rep:disabled", (String)null);     // This is how I'm trying to enable the account

but this doesn't work - I get exception that tells me that the property rep:disabled is not modifiable. How can I enable the disabled account?

BTW the code here is just for demonstration. In fact I have the disable and enable sections separated.

Thanks for any help.

EDIT the Exception
 

Exception javax.jcr.nodetype.ConstraintViolationException: Attempt to modify protected property rep:disabled of User 'testUser'
1 Accepted Solution

Avatar

Correct answer by
Level 10

To enable a disabled user -- call disable again -- and pass null:

 

String myId = tmpUser.getID();
        if (myId.equals("scottm") == true)  // this is a disabled user
          {
            %><p>Enab;e - ID is <%= myId %>
                <%
                tmpUser.disable(null) ;     
                %></p><%

         }

View solution in original post

4 Replies

Avatar

Correct answer by
Level 10

To enable a disabled user -- call disable again -- and pass null:

 

String myId = tmpUser.getID();
        if (myId.equals("scottm") == true)  // this is a disabled user
          {
            %><p>Enab;e - ID is <%= myId %>
                <%
                tmpUser.disable(null) ;     
                %></p><%

         }

Avatar

Level 10

I am looking into this with code. The API reference says:

disable(String reason) 
          Disable this user thus preventing future login if the reason is a non-null String.

Have you tried calling this method again and pass a null for the reason? This will set a null again. 

I am not sure why the author of this API did not create an enable method. 

Avatar

Level 3

Is this stil the case, do we have an "enable" method to enable a disabled user?

I cannot see it in the API: https://jackrabbit.apache.org/api/2.8/index.html?org/apache/jackrabbit/api/security/user/User.html

Avatar

Level 3

Hi ksuren

You still have to call disable(null) to enable a disabled user. It is mentioned in the api documentation with the following sentence:

Regards.