Search in sources :

Example 16 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class UserServiceImpl method initPassword.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<String> initPassword(UserRequest request) throws UnExpectedRequestException {
    // Check Arguments
    checkRequest(request);
    // Check existence of user by id
    Long id = request.getUserId();
    User userInDb = userDao.findById(id);
    if (userInDb == null) {
        throw new UnExpectedRequestException("user id {&DOES_NOT_EXIST}, request: " + request);
    }
    // Generate random password and save user
    String password = RandomPasswordGenerator.generate(16);
    String passwordEncoded = Sha256Encoder.encode(password);
    userInDb.setPassword(passwordEncoded);
    userDao.saveUser(userInDb);
    LOGGER.info("Succeed to init password, user_id: {}, current_user: {}", id, HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&INIT_PASSWORD_SUCCESSFULLY}", password);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class UserServiceImpl method autoAddUser.

@Override
@Transactional(rollbackFor = { Exception.class })
public void autoAddUser(String username) throws RoleNotFoundException {
    User newUser = new User();
    String password = username;
    String passwordEncoded = Sha256Encoder.encode(password);
    newUser.setUserName(username);
    newUser.setPassword(passwordEncoded);
    User savedUser = userDao.saveUser(newUser);
    Role role = roleDao.findByRoleName("PROJECTOR");
    if (role == null) {
        throw new RoleNotFoundException();
    }
    UserRole userRole = new UserRole();
    userRole.setId(UuidGenerator.generate());
    userRole.setRole(role);
    userRole.setUser(savedUser);
    userRoleDao.saveUserRole(userRole);
    LOGGER.info("Succeed to save user_role. uuid: {}, user_id: {}, role_id: {}", userRole.getId(), savedUser.getId(), role.getId());
}
Also used : Role(com.webank.wedatasphere.qualitis.entity.Role) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) User(com.webank.wedatasphere.qualitis.entity.User) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) RoleNotFoundException(javax.management.relation.RoleNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 18 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class UserServiceImpl method deleteUser.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> deleteUser(UserRequest request) throws UnExpectedRequestException {
    // Check Arguments
    checkRequest(request);
    // Check existence of user by id
    Long userId = request.getUserId();
    User userInDb = userDao.findById(userId);
    if (userInDb == null) {
        throw new UnExpectedRequestException("user id {&DOES_NOT_EXIST}, request: " + request);
    }
    // Check personal template.
    checkTemplate(userInDb);
    List<UserRole> userRolesInDb = userRoleDao.findByUser(userInDb);
    if (null != userRolesInDb && !userRolesInDb.isEmpty()) {
        throw new UnExpectedRequestException("{&DELETE_ERROR_USER_ROLE_HAS_FOREIGN_KEY}");
    }
    List<UserSpecPermission> userSpecPermissionsInDb = userSpecPermissionDao.findByUser(userInDb);
    if (null != userSpecPermissionsInDb && !userSpecPermissionsInDb.isEmpty()) {
        throw new UnExpectedRequestException("{&DELETE_ERROR_USER_SPEC_PERMISSION_HAS_FOREIGN_KEY}");
    }
    // Delete user
    userDao.deleteUser(userInDb);
    LOGGER.info("Succeed to delete user, userId: {}, username: {}, current_user: {}", userInDb.getId(), userInDb.getUserName(), HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&DELETE_USER_SUCCESSFULLY}", null);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 19 with User

use of com.webank.wedatasphere.qualitis.entity.User in project Qualitis by WeBankFinTech.

the class UserServiceImpl method findAllUser.

@Override
public GeneralResponse<GetAllResponse<UserResponse>> findAllUser(PageRequest request) throws UnExpectedRequestException {
    // Check Arguments
    PageRequest.checkRequest(request);
    int page = request.getPage();
    int size = request.getSize();
    List<User> users = userDao.findAllUser(page, size);
    long total = userDao.countAll();
    List<UserResponse> userResponses = new ArrayList<>();
    for (User user : users) {
        UserResponse tmp = new UserResponse(user);
        userResponses.add(tmp);
    }
    GetAllResponse<UserResponse> response = new GetAllResponse<>();
    response.setTotal(total);
    response.setData(userResponses);
    LOGGER.info("Succeed to find all users, response: {}, current_user: {}", response, HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&FIND_ALL_USERS_SUCCESSFULLY}", response);
}
Also used : UserResponse(com.webank.wedatasphere.qualitis.response.user.UserResponse) AddUserResponse(com.webank.wedatasphere.qualitis.response.user.AddUserResponse) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) User(com.webank.wedatasphere.qualitis.entity.User) ArrayList(java.util.ArrayList) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse)

Example 20 with User

use of com.webank.wedatasphere.qualitis.entity.User 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

User (com.webank.wedatasphere.qualitis.entity.User)54 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)38 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)30 Transactional (org.springframework.transaction.annotation.Transactional)23 ArrayList (java.util.ArrayList)22 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)21 Department (com.webank.wedatasphere.qualitis.entity.Department)15 List (java.util.List)14 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)13 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)13 GetAllResponse (com.webank.wedatasphere.qualitis.response.GetAllResponse)12 RuleMetricDepartmentUser (com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser)10 Project (com.webank.wedatasphere.qualitis.project.entity.Project)10 ProjectUser (com.webank.wedatasphere.qualitis.project.entity.ProjectUser)7 Application (com.webank.wedatasphere.qualitis.entity.Application)6 Task (com.webank.wedatasphere.qualitis.entity.Task)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 UserDao (com.webank.wedatasphere.qualitis.dao.UserDao)5 ApplicationResponse (com.webank.wedatasphere.qualitis.response.ApplicationResponse)5 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)5