Hi,
Can we write into /home/users using ServiceResoruceResolver in AEM 6.5? My understanding is we have to use Adminstrative session to write into /home/users but wanted to confirm.
Regards,
Kishore
Solved! Go to Solution.
Views
Replies
Total Likes
You need to be part of user-administrator group in order create/update users/groups.
Views
Replies
Total Likes
String userID = request.getParameter("userID");
String userFirstName = request.getParameter("userFirstName");
String userLastName = request.getParameter("userLastName");
String userEmail = request.getParameter("userEmail");
String password = request.getParameter("password");
//Getting ResourceResolver and Session using System Users
Map<String, Object> param = new HashMap<>();
param.put(ResourceResolverFactory.SUBSERVICE, SYSTEM_USER);
resourceResolver = resourceResolverFactory.getServiceResourceResolver(param);
Session session = resourceResolver.adaptTo(Session.class);
assert session != null;
//Getting UserManager from ResourceResolver
UserManager userManager = resourceResolver.adaptTo(UserManager.class);
assert userManager != null;
//Creation of a new user with userID
User createdUser = null;
if (userManager.getAuthorizable(userID) == null) {
createdUser = userManager.createUser(userID, password);
//Setting the createdUser Profile Property
ValueFactory valueFactory = session.getValueFactory();
Value firstNameValue = valueFactory.createValue(userFirstName, PropertyType.STRING);
createdUser.setProperty("./profile/givenName", firstNameValue);
Value lastNameValue = valueFactory.createValue(userLastName, PropertyType.STRING);
createdUser.setProperty("./profile/familyName", lastNameValue);
Value emailValue = valueFactory.createValue(userEmail, PropertyType.STRING);
createdUser.setProperty("./profile/email", emailValue);
session.save();
logger.info("User successfully created with ID : {}", createdUser.getID());
} else {
logger.info("User already exist..");
}
Refrences:
https://unlocklearning.in/aem-user-group-creation-and-permission-programmatically/
Views
Replies
Total Likes
You need to be part of user-administrator group in order create/update users/groups.
Views
Replies
Total Likes
Thank you, it works.
Views
Replies
Total Likes
Views
Likes
Replies