use of com.epam.ta.reportportal.entity.user.UserType.INTERNAL in project service-api by reportportal.
the class EditUserHandlerImpl method editUser.
@Override
public OperationCompletionRS editUser(String username, EditUserRQ editUserRQ, ReportPortalUser editor) {
User user = userRepository.findByLogin(username).orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, username));
if (null != editUserRQ.getRole()) {
BusinessRule.expect(editor.getUserRole(), equalTo(UserRole.ADMINISTRATOR)).verify(ACCESS_DENIED, "Current Account Role can't update roles.");
BusinessRule.expect(user, u -> !u.getLogin().equalsIgnoreCase(editor.getUsername())).verify(ErrorType.ACCESS_DENIED, "You cannot update your role.");
UserRole newRole = UserRole.findByName(editUserRQ.getRole()).orElseThrow(() -> new ReportPortalException(BAD_REQUEST_ERROR, "Incorrect specified Account Role parameter."));
user.setRole(newRole);
}
if (null != editUserRQ.getEmail() && !editUserRQ.getEmail().equals(user.getEmail())) {
String updEmail = editUserRQ.getEmail().toLowerCase().trim();
expect(user.getUserType(), equalTo(INTERNAL)).verify(ACCESS_DENIED, "Unable to change email for external user");
expect(UserUtils.isEmailValid(updEmail), equalTo(true)).verify(BAD_REQUEST_ERROR, " wrong email: " + updEmail);
final Optional<User> byEmail = userRepository.findByEmail(updEmail);
expect(byEmail, Predicates.not(Optional::isPresent)).verify(USER_ALREADY_EXISTS, updEmail);
List<Project> userProjects = projectRepository.findUserProjects(username);
userProjects.forEach(project -> ProjectUtils.updateProjectRecipients(user.getEmail(), updEmail, project));
user.setEmail(updEmail);
try {
projectRepository.saveAll(userProjects);
} catch (Exception exp) {
throw new ReportPortalException("PROJECT update exception while USER editing.", exp);
}
}
if (null != editUserRQ.getFullName()) {
expect(user.getUserType(), equalTo(INTERNAL)).verify(ACCESS_DENIED, "Unable to change full name for external user");
user.setFullName(editUserRQ.getFullName());
}
try {
userRepository.save(user);
} catch (Exception exp) {
throw new ReportPortalException("Error while User editing.", exp);
}
return new OperationCompletionRS("User with login = '" + user.getLogin() + "' successfully updated");
}
Aggregations