Creating User Profile in AEM

04 / May / 2015 by Shivani Garg 6 comments

Hello everyone,

In this blog, we will be focusing on how to create a user and user profiles in AEM.  It is generally needed when we create login and signup forms.

Depending on the use case, users can be created using:

i) In the UserManager API ,we can create a user, password under a specific group and provide permissions to them.

Sample code for UserManager API

[java]
ResourceResolver resolver=request.getResourceResolver();
Session session=request.getResourceResolver().adaptTo(Session.class);
UserManager userManager = resolver.adaptTo(UserManager.class);
Group group=userManager.createGroup(" Group Name");
User user=userManager.createUser("username",”pwd”);
if (! userManager.isAutoSave()) {
session.save();
}
[/java]

ii) The other case is when you have to create a profile for user like username, password, group, email, address, contact etc. For this purpose AccountManager API can be used.

Sample Code for AccountManager API:

[java]
AccountManagerFactory accountManagerFactory;
@Reference
private ResourceResolverFactory resolverFactory;
@Override
Map<String, Object> serviceParams = new HashMap<String, Object>();
serviceParams.put(SUBSERVICE, "signUpService");
ResourceResolverresolver=resolverFactory.getServiceResourceResolver(serviceParams)
Session session = resolver.adaptTo(Session.class);
AccountManager accountManager = accountManagerFactory.createAccountManager(session);
Map<String,RequestParameter[]> profilemap = new HashMap<>();
accountManager.getOrCreateAccount(“userName”, “password”, "admin", profilemap);

[/java]

The Map can be populated in two ways:

a) If the value of the profile property is coming from form

[java] profilemap.put("email", request.getRequestParameters("email")); [/java]

//like email we can add additional attributes in profile

b) If the property is not in RequestParameters then—–

[java] String email=”shivanig@intelligrape.com”;
profilemap.put(“email”,new RequestParameter[]{new Parameters(email) }); [/java]

//Create a POJO class to set values of additional attribute—-

[java]final class Parameters implements RequestParameter {
private final String parameter;
private Parameters(String parameter)
{
this.parameter = parameter;
}}
[/java]

 

If you want to see your created Users,Go to Crxde , /home/Users ……..You can see your users and profiles also.

blog
So here you see how you can create users in AEM and create profiles for them.

Regards: Shivani Garg

FOUND THIS USEFUL? SHARE IT

comments (6)

  1. Jay Gandhi

    b) If the property is not in RequestParameters then—– – is not working for me. property is not get added in profilemap.

    Reply
  2. Dhiraj Agarwal

    getting the following exception in aem 6.1 while using accountmanager API
    org.apache.sling.api.resource.LoginException: Cannot derive user name for bundle com.ott.ott-matrix [505] and sub service signUpService

    I think you need to make changes in Apache Sling Service User Mapper Service to map a user to the subservice. I just dont know how to add the subservice here to a system user.

    Reply
  3. Aniket

    Nice its helpfull can you tell how to set user and group folder and user node name…because it taking random value..can you help

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *