Creating User Profile in AEM

04 / May / 2015 by Shivani Garg 5 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

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();
}

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:

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);

The Map can be populated in two ways:

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

 profilemap.put("email", request.getRequestParameters("email")); 

//like email we can add additional attributes in profile

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

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

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

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

 

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

Tag -

FOUND THIS USEFUL? SHARE IT

comments (5)

  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 comment -