While creating user programatically in AEM it is giving javax.jcr.AccessDeniedException: Access denied. | Community
Skip to main content
MandaPavanKumar
Level 2
Jan 19, 2023 11:24 AM UTC
Solved

While creating user programatically in AEM it is giving javax.jcr.AccessDeniedException: Access denied.

Hi All,

    Can anyone please help me when I am craeting the user programatically it is giving me the javax.jcr.AccessDeniedException: Access denied.

 

Thanks,

Pavan

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Nitin_laad
Nitin_laadAccepted solution
New Participant
Jan 20, 2023 8:03 PM UTC

Hi @mandapavankumar you can try below sample code, assuming system user is having all required permission in publish environment - 

@Activate public void createLocalUser() { try { Map<String, Object> param = new HashMap<>(); param.put(ResourceResolverFactory.SUBSERVICE, "system-user"); ResourceResolver resolver = resolverFactory.getServiceResourceResolver(param); UserManager userManager = resolver.adaptTo(UserManager.class); Authorizable user = userManager.createUser("newlocaluser", "password"); user.setProperty("firstName", "test"); user.setProperty("lastName", "user"); user.addGroup("group1"); userManager.updateUser(user); LOG.info("Local user newlocaluser created successfully."); resolver.close(); } catch (LoginException | RepositoryException e) { LOG.error("Error creating local user: {}", e.getMessage()); }

 

15 replies

Shivam153
Level 3
Jan 23, 2023 6:17 AM UTC

You can use repository initialization concept here.

https://sling.apache.org/documentation/bundles/repository-initialization.html

 

Hope it will work!

Thanks!

Translate
MandaPavanKumar
Level 2
Jan 24, 2023 7:49 AM UTC

Hi Shivam,

     Correct me if I am wrong that when the site was published at dispatcher level. And now if we try to create the user at AEM end is it possible ? can you please hlpe me on this.

Thanks,

Pavan

Translate
Nitin_laad
Nitin_laadAccepted solution
New Participant
Jan 20, 2023 8:03 PM UTC

Hi @mandapavankumar you can try below sample code, assuming system user is having all required permission in publish environment - 

@Activate public void createLocalUser() { try { Map<String, Object> param = new HashMap<>(); param.put(ResourceResolverFactory.SUBSERVICE, "system-user"); ResourceResolver resolver = resolverFactory.getServiceResourceResolver(param); UserManager userManager = resolver.adaptTo(UserManager.class); Authorizable user = userManager.createUser("newlocaluser", "password"); user.setProperty("firstName", "test"); user.setProperty("lastName", "user"); user.addGroup("group1"); userManager.updateUser(user); LOG.info("Local user newlocaluser created successfully."); resolver.close(); } catch (LoginException | RepositoryException e) { LOG.error("Error creating local user: {}", e.getMessage()); }

 

Translate
MandaPavanKumar
Level 2
Jan 23, 2023 6:13 AM UTC

Hi Nitin,

     Let me try it and will you know if I face any issue.

Thanks,

Pavan

Translate
Kiran_Vedantam
New Participant
Jan 19, 2023 8:54 PM UTC

Hi @mandapavankumar 

 

Please check similar query with the solution here:: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/programmatic-user-creation-in-aem/m-p/191683

 

Hope this helps!


Thanks,
Kiran Vedantam.

Translate
MandaPavanKumar
Level 2
Jan 20, 2023 3:15 AM UTC

I followed the same implementation still it is showing access denied

Translate
Siva_Sogalapalli
New Participant
Jan 19, 2023 11:43 AM UTC

Below is the sample code to create user:

Map<String, Object> param = new HashMap<String, Object>();

param.put(ResourceResolverFactory.SUBSERVICE, "service-userid");

ResourceResolver resolver = resourceFact.getServiceResourceResolver(param);

org.apache.jackrabbit.api.security.user.UserManager userManager = resolver.adaptTo(org.apache.jackrabbit.api.security.user.UserManager.class);
if(userManager == null)
{
log.error("userManager == null!");
return;
}else {
String username = "mytestuser";
String password = "password";
org.apache.jackrabbit.api.security.user.User user = userManager.createUser(username, password);
}

 

Note: please make sure system user has permissions to create user. 

Translate
MandaPavanKumar
Level 2
Jan 19, 2023 11:46 AM UTC

Hi Siva,

      Can you please correct me that in this line the login is reffering to username right.

 org.apache.jackrabbit.api.security.user.User user = userManager.createUser(login, password);

Thanks,

Pavan

Translate
Siva_Sogalapalli
New Participant
Jan 19, 2023 11:51 AM UTC

yes, you're right, it was typo mistake, corrected it. 

 

Translate
Umesh_Thakur
New Participant
Jan 19, 2023 11:37 AM UTC

did you check the permission of the system user, you are using to save the user node.?

Please verify that.

Translate
MandaPavanKumar
Level 2
Jan 19, 2023 11:44 AM UTC

Yes I ahve verified the permissions to the system user and that user is having enough permissions to create user

 

Translate