Search in sources :

Example 1 with AddUserResponse

use of com.webank.wedatasphere.qualitis.response.user.AddUserResponse in project Qualitis by WeBankFinTech.

the class UserServiceImpl method addUser.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<AddUserResponse> addUser(UserAddRequest request) throws UnExpectedRequestException {
    // Check Arguments
    checkRequest(request);
    // Check existence of user by username
    String username = request.getUsername();
    User userInDb = userDao.findByUsername(username);
    if (userInDb != null) {
        throw new UnExpectedRequestException("username: " + username + " {&ALREADY_EXIST}");
    }
    // Generate random password and save user
    User newUser = new User();
    String password = RandomPasswordGenerator.generate(16);
    String passwordEncoded = Sha256Encoder.encode(password);
    newUser.setUserName(username);
    newUser.setPassword(passwordEncoded);
    newUser.setChineseName(request.getChineseName());
    // Find department by department name
    Department departmentInDb = departmentDao.findById(request.getDepartmentId());
    if (null == departmentInDb) {
        throw new UnExpectedRequestException("Department ID of " + request.getDepartmentId() + " {&DOES_NOT_EXIST}");
    }
    newUser.setDepartment(departmentInDb);
    User savedUser = userDao.saveUser(newUser);
    AddUserResponse addUserResponse = new AddUserResponse(savedUser, password);
    LOGGER.info("Succeed to create user, response: {}, current_user: {}", addUserResponse, HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&CREATE_USER_SUCCESSFULLY}", addUserResponse);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Department(com.webank.wedatasphere.qualitis.entity.Department) User(com.webank.wedatasphere.qualitis.entity.User) AddUserResponse(com.webank.wedatasphere.qualitis.response.user.AddUserResponse) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Department (com.webank.wedatasphere.qualitis.entity.Department)1 User (com.webank.wedatasphere.qualitis.entity.User)1 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)1 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)1 AddUserResponse (com.webank.wedatasphere.qualitis.response.user.AddUserResponse)1 Transactional (org.springframework.transaction.annotation.Transactional)1