In this blog, we will be focusing on how to create 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
1
2
3
4
5
6
7
8
| 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 a user like a username, password, group, email, address, contact, etc. For this purpose, the AccountManager API can be used.
Sample Code for AccountManager API:
1
2
3
4
5
6
7
8
9
10
11
| 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 the form
1
| profilemap.put( "email" , request.getRequestParameters( "email" )); |
//like the email we can add additional attributes in profile
b) If the property is not in RequestParameters then—–
1
2
| String email=”shivanig @intelligrape .com”; profilemap.put(“email”, new RequestParameter[]{ new Parameters(email) }); |
//Create a POJO class to set values of additional attribute—-
1
2
3
4
5
6
| 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.
So here you see how you can create users in AEM and create profiles for them.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.