Search in sources :

Example 1 with BusinessRule.expect

use of com.epam.ta.reportportal.commons.validation.BusinessRule.expect 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");
}
Also used : ChangePasswordRQ(com.epam.ta.reportportal.ws.model.user.ChangePasswordRQ) Project(com.epam.ta.reportportal.entity.project.Project) OperationCompletionRS(com.epam.ta.reportportal.ws.model.OperationCompletionRS) ImageFormat(com.epam.ta.reportportal.entity.enums.ImageFormat) Autowired(org.springframework.beans.factory.annotation.Autowired) ErrorType(com.epam.ta.reportportal.ws.model.ErrorType) MediaType(org.apache.tika.mime.MediaType) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) UserUtils(com.epam.ta.reportportal.util.UserUtils) Predicates(com.epam.ta.reportportal.commons.Predicates) Metadata(org.apache.tika.metadata.Metadata) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) Predicates.equalTo(com.epam.ta.reportportal.commons.Predicates.equalTo) Service(org.springframework.stereotype.Service) UserRole(com.epam.ta.reportportal.entity.user.UserRole) INTERNAL(com.epam.ta.reportportal.entity.user.UserType.INTERNAL) ImageIO(javax.imageio.ImageIO) ValidationConstraints(com.epam.ta.reportportal.ws.model.ValidationConstraints) TikaInputStream(org.apache.tika.io.TikaInputStream) EditUserRQ(com.epam.ta.reportportal.ws.model.user.EditUserRQ) BusinessRule(com.epam.ta.reportportal.commons.validation.BusinessRule) EditUserHandler(com.epam.ta.reportportal.core.user.EditUserHandler) ImageReader(javax.imageio.ImageReader) Iterator(java.util.Iterator) Predicate(java.util.function.Predicate) BusinessRule.expect(com.epam.ta.reportportal.commons.validation.BusinessRule.expect) Optional.ofNullable(java.util.Optional.ofNullable) User(com.epam.ta.reportportal.entity.user.User) UserRepository(com.epam.ta.reportportal.dao.UserRepository) ProjectUtils(com.epam.ta.reportportal.entity.project.ProjectUtils) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) IOException(java.io.IOException) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) ImageInputStream(javax.imageio.stream.ImageInputStream) java.awt(java.awt) List(java.util.List) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) ProjectRepository(com.epam.ta.reportportal.dao.ProjectRepository) BusinessRule.fail(com.epam.ta.reportportal.commons.validation.BusinessRule.fail) MultipartFile(org.springframework.web.multipart.MultipartFile) Optional(java.util.Optional) UserBinaryDataService(com.epam.ta.reportportal.binary.UserBinaryDataService) InputStream(java.io.InputStream) Project(com.epam.ta.reportportal.entity.project.Project) ReportPortalUser(com.epam.ta.reportportal.commons.ReportPortalUser) User(com.epam.ta.reportportal.entity.user.User) Optional(java.util.Optional) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) UserRole(com.epam.ta.reportportal.entity.user.UserRole) ReportPortalException(com.epam.ta.reportportal.exception.ReportPortalException) IOException(java.io.IOException) OperationCompletionRS(com.epam.ta.reportportal.ws.model.OperationCompletionRS)

Aggregations

UserBinaryDataService (com.epam.ta.reportportal.binary.UserBinaryDataService)1 Predicates (com.epam.ta.reportportal.commons.Predicates)1 Predicates.equalTo (com.epam.ta.reportportal.commons.Predicates.equalTo)1 ReportPortalUser (com.epam.ta.reportportal.commons.ReportPortalUser)1 BusinessRule (com.epam.ta.reportportal.commons.validation.BusinessRule)1 BusinessRule.expect (com.epam.ta.reportportal.commons.validation.BusinessRule.expect)1 BusinessRule.fail (com.epam.ta.reportportal.commons.validation.BusinessRule.fail)1 EditUserHandler (com.epam.ta.reportportal.core.user.EditUserHandler)1 ProjectRepository (com.epam.ta.reportportal.dao.ProjectRepository)1 UserRepository (com.epam.ta.reportportal.dao.UserRepository)1 ImageFormat (com.epam.ta.reportportal.entity.enums.ImageFormat)1 Project (com.epam.ta.reportportal.entity.project.Project)1 ProjectUtils (com.epam.ta.reportportal.entity.project.ProjectUtils)1 User (com.epam.ta.reportportal.entity.user.User)1 UserRole (com.epam.ta.reportportal.entity.user.UserRole)1 INTERNAL (com.epam.ta.reportportal.entity.user.UserType.INTERNAL)1 ReportPortalException (com.epam.ta.reportportal.exception.ReportPortalException)1 UserUtils (com.epam.ta.reportportal.util.UserUtils)1 ErrorType (com.epam.ta.reportportal.ws.model.ErrorType)1 OperationCompletionRS (com.epam.ta.reportportal.ws.model.OperationCompletionRS)1