use of com.enonic.xp.security.User in project xp by enonic.
the class SecurityServiceImplTest method testCreateUser.
@Test
public void testCreateUser() throws Exception {
runAsAdmin(() -> {
final PrincipalKey userKey1 = PrincipalKey.ofUser(SYSTEM, "User1");
final CreateUserParams createUser1 = CreateUserParams.create().userKey(userKey1).displayName("User 1").email("user1@enonic.com").login("User1").password("123456").build();
final PrincipalKey userKey2 = PrincipalKey.ofUser(SYSTEM, "user2");
final CreateUserParams createUser2 = CreateUserParams.create().userKey(userKey2).displayName("User 2").email("user2@enonic.com").login("user2").build();
final User user1 = securityService.createUser(createUser1);
final User user2 = securityService.createUser(createUser2);
refresh();
final User createdUser1 = securityService.getUser(userKey1).get();
final User createdUser2 = securityService.getUser(userKey2).get();
assertEquals("User 1", user1.getDisplayName());
assertEquals("user1@enonic.com", user1.getEmail());
assertEquals("User1", user1.getLogin());
assertEquals("User 1", createdUser1.getDisplayName());
assertEquals("user1@enonic.com", createdUser1.getEmail());
assertEquals("User1", createdUser1.getLogin());
assertEquals("User 2", user2.getDisplayName());
assertEquals("user2@enonic.com", user2.getEmail());
assertEquals("user2", user2.getLogin());
assertEquals("User 2", createdUser2.getDisplayName());
assertEquals("user2@enonic.com", createdUser2.getEmail());
assertEquals("user2", createdUser2.getLogin());
});
}
use of com.enonic.xp.security.User in project xp by enonic.
the class SecurityInitializer method createAdminContext.
private Context createAdminContext() {
final User admin = User.create().key(SUPER_USER).login(SUPER_USER.getId()).build();
final AuthenticationInfo authInfo = AuthenticationInfo.create().principals(RoleKeys.ADMIN).user(admin).build();
return ContextBuilder.create().branch(SecurityConstants.BRANCH_SECURITY).repositoryId(SystemConstants.SYSTEM_REPO_ID).authInfo(authInfo).build();
}
use of com.enonic.xp.security.User in project xp by enonic.
the class SecurityServiceImpl method doUpdateUser.
private User doUpdateUser(final UpdateUserParams updateUserParams) {
return callWithContext(() -> {
final Node node = this.nodeService.getByPath(updateUserParams.getKey().toPath());
if (node == null) {
throw new PrincipalNotFoundException(updateUserParams.getKey());
}
final User existingUser = PrincipalNodeTranslator.userFromNode(node);
final User userToUpdate = updateUserParams.update(existingUser);
duplicateEmailValidation(userToUpdate.getKey(), userToUpdate.getEmail());
final UpdateNodeParams updateNodeParams = PrincipalNodeTranslator.toUpdateNodeParams(userToUpdate);
final Node updatedNode = nodeService.update(updateNodeParams);
this.nodeService.refresh(RefreshMode.SEARCH);
return PrincipalNodeTranslator.userFromNode(updatedNode);
});
}
use of com.enonic.xp.security.User in project xp by enonic.
the class SecurityServiceImpl method duplicateEmailValidation.
private void duplicateEmailValidation(final PrincipalKey key, final String email) {
if (email == null) {
return;
}
final CompareExpr idProviderExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.ID_PROVIDER_KEY), CompareExpr.Operator.EQ, ValueExpr.string(key.getIdProviderKey().toString()));
final CompareExpr emailExpr = CompareExpr.create(FieldExpr.from(PrincipalIndexPath.EMAIL_KEY), CompareExpr.Operator.EQ, ValueExpr.string(email));
final QueryExpr query = QueryExpr.from(LogicalExpr.and(idProviderExpr, emailExpr));
final Nodes nodes = callWithContext(() -> {
final FindNodesByQueryResult result = nodeService.findByQuery(NodeQuery.create().query(query).build());
return this.nodeService.getByIds(result.getNodeIds());
});
final User user = nodes.isEmpty() ? null : PrincipalNodeTranslator.userFromNode(nodes.first());
if (nodes.getSize() > 1 || (user != null && !user.getKey().equals(key))) {
throw new IllegalArgumentException("A user with email '" + email + "' already exists in id provider '" + key.getIdProviderKey() + "'");
}
}
use of com.enonic.xp.security.User in project xp by enonic.
the class SecurityServiceImpl method setPassword.
@Override
public User setPassword(final PrincipalKey key, final String password) {
Preconditions.checkArgument(key.isUser(), "Expected principal key of type User");
Preconditions.checkArgument(password != null && password.length() > 0, "Password cannot be empty");
return callWithContext(() -> {
final Node node = callWithContext(() -> this.nodeService.getByPath(key.toPath()));
if (node == null) {
throw new NodeNotFoundException("setPassword failed, user with key " + key + " not found");
}
final User user = PrincipalNodeTranslator.userFromNode(node);
if (user == null) {
throw new NodeNotFoundException("setPassword failed, user with key " + key + " not found");
}
final String authenticationHash = this.passwordEncoder.encodePassword(password);
final User userToUpdate = User.create(user).authenticationHash(authenticationHash).build();
final UpdateNodeParams updateNodeParams = PrincipalNodeTranslator.toUpdateNodeParams(userToUpdate);
final Node updatedNode = nodeService.update(updateNodeParams);
return PrincipalNodeTranslator.userFromNode(updatedNode);
});
}
Aggregations