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);
}
Aggregations