Search in sources :

Example 1 with ProjectUserResponse

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

the class ProjectUserServiceImpl method getAllProjectUser.

@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = true, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<List<ProjectUserResponse>> getAllProjectUser(Long projectId) throws UnExpectedRequestException, PermissionDeniedRequestException {
    List<ProjectUserResponse> projectUserResponses = new ArrayList<>();
    Project projectInDb = projectDao.findById(projectId);
    if (projectInDb == null) {
        throw new UnExpectedRequestException("{&PROJECT}: [ID=" + projectId + "] {&DOES_NOT_EXIST}");
    }
    String userName = HttpUtils.getUserName(httpRequest);
    List<ProjectUser> projectUsers = projectUserDao.findByProject(projectInDb);
    List<String> projectUserNames = projectUsers.stream().map(ProjectUser::getUserName).collect(Collectors.toList());
    if (!projectUserNames.contains(userName)) {
        throw new PermissionDeniedRequestException("{&HAS_NO_PERMISSION_TO_ACCESS}", 403);
    }
    for (ProjectUser currentProjectUser : projectUsers) {
        List<String> userNames = projectUserResponses.stream().map(ProjectUserResponse::getAuthorizedUser).collect(Collectors.toList());
        String currentUser = currentProjectUser.getUserName();
        if (userNames.contains(currentUser)) {
            continue;
        }
        String projectCreator = projectInDb.getCreateUser();
        ProjectUserResponse projectUserResponse = new ProjectUserResponse(projectInDb.getName(), projectCreator, currentUser);
        List<Integer> permissions = getPermissionList(projectUsers, currentUser);
        projectUserResponse.setPermissions(permissions);
        projectUserResponses.add(projectUserResponse);
    }
    return new GeneralResponse<>("200", "{&SUCCESS_TO_GET_PROJECT_USER}", projectUserResponses);
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) ArrayList(java.util.ArrayList) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) ProjectUserResponse(com.webank.wedatasphere.qualitis.project.response.ProjectUserResponse) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ProjectUserResponse

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

the class ProjectUserServiceImpl method authorizePermission.

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<ProjectUserResponse> authorizePermission(AuthorizeProjectUserRequest authorizeProjectUserRequest, Long loginUserId, boolean modify) throws UnExpectedRequestException, PermissionDeniedRequestException, RoleNotFoundException {
    List<ProjectUser> projectUsers = new ArrayList<>();
    AuthorizeProjectUserRequest.checkRequest(authorizeProjectUserRequest);
    Project projectInDb = projectDao.findById(authorizeProjectUserRequest.getProjectId());
    String projectUser = authorizeProjectUserRequest.getProjectUser();
    LOGGER.info("User[id={}] start to authorize user[name={}]", loginUserId, projectUser);
    if (projectInDb == null) {
        throw new UnExpectedRequestException("{&PROJECT}: [ID=" + authorizeProjectUserRequest.getProjectId() + "] {&DOES_NOT_EXIST}");
    }
    User projectUserInDb = userDao.findByUsername(projectUser);
    if (projectUserInDb == null) {
        LOGGER.warn("Project user is from outside, qualitis will auto add user. Name: " + projectUser);
        userService.autoAddUser(projectUser);
    }
    User loginUser = userDao.findById(loginUserId);
    if (!checkPermission(projectInDb, loginUser.getUserName(), ProjectUserPermissionEnum.CREATOR.getCode())) {
        throw new PermissionDeniedRequestException("{&NO_PERMISSION_MODIFYING_PROJECT}", 403);
    }
    if (loginUser.getUserName().equals(projectUser)) {
        return null;
    }
    List<Integer> permissions = new ArrayList<>();
    if (modify) {
        projectUserDao.deleteByProjectAndUserName(projectInDb, projectUser);
        LOGGER.info("Success to delete original project user permissions.");
    }
    for (Integer permission : authorizeProjectUserRequest.getProjectPermissions()) {
        ProjectUser tmp = new ProjectUser(permission, projectInDb, projectUser);
        LOGGER.info("User[name={}] get permission[ID={}].", projectUser, permission);
        projectUsers.add(tmp);
        permissions.add(permission);
    }
    projectUserDao.saveAll(projectUsers);
    // projectEventService.record(projectInDb.getId(), loginUser.getUserName(), "authorized", projectUser, EventTypeEnum.MODIFY_PROJECT.getCode());
    ProjectUserResponse projectUserResponse = new ProjectUserResponse(projectInDb.getName(), loginUser.getUserName(), projectUser);
    projectUserResponse.setPermissions(permissions);
    return new GeneralResponse<>("200", "{&SUCCESS_TO_ADD_PROJECT_USER}", projectUserResponse);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) Project(com.webank.wedatasphere.qualitis.project.entity.Project) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) ProjectUserResponse(com.webank.wedatasphere.qualitis.project.response.ProjectUserResponse) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)2 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)2 Project (com.webank.wedatasphere.qualitis.project.entity.Project)2 ProjectUser (com.webank.wedatasphere.qualitis.project.entity.ProjectUser)2 ProjectUserResponse (com.webank.wedatasphere.qualitis.project.response.ProjectUserResponse)2 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)2 ArrayList (java.util.ArrayList)2 Transactional (org.springframework.transaction.annotation.Transactional)2 User (com.webank.wedatasphere.qualitis.entity.User)1