Search in sources :

Example 1 with UsersEntity

use of it.istat.mec.users.domain.UsersEntity in project catalogo-strumenti by istat-methodology.

the class UserService method updateUser.

public String updateUser(Integer id, UpdateUserRequest request) throws Exception {
    String msg = "";
    if (!usersDao.findById(id).isPresent())
        throw new NoDataException("User not present");
    UsersEntity user = usersDao.findById(id).get();
    user = Translators.translateUpdate(request, user);
    if (request.getRoleid() != null) {
        if (!userRolesDao.findById(request.getRoleid()).isPresent())
            throw new NoDataException("Role not present");
        UserRolesEntity userRole = userRolesDao.findById(request.getRoleid()).get();
        user.setRole(userRole);
    }
    usersDao.save(user);
    msg = "User succesfully updated!";
    return msg;
}
Also used : UserRolesEntity(it.istat.mec.users.domain.UserRolesEntity) UsersEntity(it.istat.mec.users.domain.UsersEntity) NoDataException(it.istat.mec.users.exceptions.NoDataException)

Example 2 with UsersEntity

use of it.istat.mec.users.domain.UsersEntity in project catalogo-strumenti by istat-methodology.

the class UserService method resetPasswordById.

public String resetPasswordById(Integer id, ResetPasswordRequest request) throws Exception {
    String msg = "";
    if (!usersDao.findById(id).isPresent())
        throw new NoDataException("User not present");
    UsersEntity user = usersDao.findById(id).get();
    if ((request.getPassword() != null && !request.getPassword().equals(""))) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String newPass = passwordEncoder.encode(request.getPassword());
        user.setPassword(newPass);
        usersDao.save(user);
        msg = "01 Password succesfully updated!";
    } else {
        msg = "02 The password field is mandatory";
    }
    usersDao.save(user);
    return msg;
}
Also used : UsersEntity(it.istat.mec.users.domain.UsersEntity) NoDataException(it.istat.mec.users.exceptions.NoDataException) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Example 3 with UsersEntity

use of it.istat.mec.users.domain.UsersEntity in project catalogo-strumenti by istat-methodology.

the class Translators method translate.

public static UsersEntity translate(CreateUserRequest x) {
    final ModelMapper modelMapper = new ModelMapper();
    final UsersEntity user = modelMapper.map(x, UsersEntity.class);
    return user;
}
Also used : UsersEntity(it.istat.mec.users.domain.UsersEntity) ModelMapper(org.modelmapper.ModelMapper)

Example 4 with UsersEntity

use of it.istat.mec.users.domain.UsersEntity in project catalogo-strumenti by istat-methodology.

the class UserService method updatePasswordByEmail.

public String updatePasswordByEmail(String email, UpdatePasswordRequest request) throws Exception {
    String msg = "";
    if (!usersDao.findByEmail(email).isPresent())
        throw new NoDataException("User not present");
    UsersEntity user = usersDao.findByEmail(email).get();
    if ((request.getNewpass() != null && !request.getNewpass().equals("")) && (request.getOldpass() != null && !request.getOldpass().equals(""))) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String newPass = passwordEncoder.encode(request.getNewpass());
        if (passwordEncoder.matches(request.getOldpass(), user.getPassword())) {
            user.setPassword(newPass);
            usersDao.save(user);
            msg = "01 Password succesfully updated!";
        } else {
            msg = "02 The old password doesn't match with password stored in db";
        }
    } else {
        msg = "03 The Old password and New password fields are mandatory";
    }
    usersDao.save(user);
    return msg;
}
Also used : UsersEntity(it.istat.mec.users.domain.UsersEntity) NoDataException(it.istat.mec.users.exceptions.NoDataException) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Example 5 with UsersEntity

use of it.istat.mec.users.domain.UsersEntity in project catalogo-strumenti by istat-methodology.

the class UserService method updatePasswordById.

public String updatePasswordById(Integer id, UpdatePasswordRequest request) throws Exception {
    String msg = "";
    if (!usersDao.findById(id).isPresent())
        throw new NoDataException("User not present");
    UsersEntity user = usersDao.findById(id).get();
    if ((request.getNewpass() != null && !request.getNewpass().equals("")) && (request.getOldpass() != null && !request.getOldpass().equals(""))) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String newPass = passwordEncoder.encode(request.getNewpass());
        if (passwordEncoder.matches(request.getOldpass(), user.getPassword())) {
            user.setPassword(newPass);
            usersDao.save(user);
            msg = "01 Password succesfully updated!";
        } else {
            msg = "02 The old password doesn't match with password stored in db";
        }
    } else {
        msg = "03 The Old password and New password fields are mandatory";
    }
    usersDao.save(user);
    return msg;
}
Also used : UsersEntity(it.istat.mec.users.domain.UsersEntity) NoDataException(it.istat.mec.users.exceptions.NoDataException) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)

Aggregations

UsersEntity (it.istat.mec.users.domain.UsersEntity)6 NoDataException (it.istat.mec.users.exceptions.NoDataException)4 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)4 UserRolesEntity (it.istat.mec.users.domain.UserRolesEntity)2 ModelMapper (org.modelmapper.ModelMapper)1