Search in sources :

Example 6 with UserRole

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

the class RuleMetricServiceImpl method getRuleMetricDetail.

@Override
public GeneralResponse<RuleMetricResponse> getRuleMetricDetail(long id) throws UnExpectedRequestException, PermissionDeniedRequestException {
    if (id <= 0) {
        throw new UnExpectedRequestException("{&REQUEST_CAN_NOT_BE_NULL}");
    }
    // Check rule metric existence.
    RuleMetric ruleMetricInDb = ruleMetricDao.findById(id);
    if (ruleMetricInDb == null) {
        throw new UnExpectedRequestException("Rule Metric ID [" + id + "] {&DOES_NOT_EXIST}");
    }
    String userName = HttpUtils.getUserName(httpServletRequest);
    LOGGER.info("Start to get rule metric, rule metric ID: [{}], user: [{}]", id, userName);
    User loginUser = userDao.findByUsername(userName);
    List<UserRole> userRoles = userRoleDao.findByUser(loginUser);
    Integer roleType = roleService.getRoleType(userRoles);
    RuleMetricDepartmentUser ruleMetricDepartmentUser = ruleMetricDepartmentUserDao.findByRuleMetric(ruleMetricInDb);
    if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
        LOGGER.info("SYS_ADMIN will get rule metric.");
    } else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
        LOGGER.info("DEPARTMENT_ADMIN will get rule metric.");
        List<Department> managedDepartment = new ArrayList<>();
        for (UserRole userRole : userRoles) {
            Department department = userRole.getRole().getDepartment();
            if (department != null) {
                managedDepartment.add(department);
            }
        }
        if (ruleMetricDepartmentUser.getDepartment() != null) {
            List<Department> res = managedDepartment.stream().filter(department -> department.getId() == ruleMetricDepartmentUser.getDepartment().getId()).collect(Collectors.toList());
            if (res.size() > 0) {
                LOGGER.info("Rule metric[{}] comes from department: {}", ruleMetricInDb.toString(), ruleMetricDepartmentUser.getDepartment().getName());
            } else {
                throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
            }
        } else if (ruleMetricInDb.getLevel().equals(RuleMetricLevelEnum.DEFAULT_METRIC.getCode())) {
            LOGGER.info("DEPARTMENT_ADMIN will get first level rule metric.");
        } else {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
    } else {
        LOGGER.info("PROJECTOR will get rule metric.");
        Department department = loginUser.getDepartment();
        if (ruleMetricInDb.getLevel().equals(RuleMetricLevelEnum.DEPARTMENT_METRIC) && !department.equals(ruleMetricDepartmentUser.getDepartment())) {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
        if (ruleMetricInDb.getLevel().equals(RuleMetricLevelEnum.PERSONAL_METRIC) && !loginUser.equals(ruleMetricDepartmentUser.getUser())) {
            throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
        }
    }
    RuleMetricResponse ruleMetricResponse = new RuleMetricResponse(ruleMetricInDb);
    return new GeneralResponse<>("200", "{&GET_RULE_METRIC_SUCCESSFULLY}", ruleMetricResponse);
}
Also used : ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) User(com.webank.wedatasphere.qualitis.entity.User) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) RuleMetricResponse(com.webank.wedatasphere.qualitis.response.RuleMetricResponse) Department(com.webank.wedatasphere.qualitis.entity.Department) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with UserRole

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

the class RuleMetricServiceImpl method queryRuleMetric.

@Override
public GeneralResponse<GetAllResponse<RuleMetricResponse>> queryRuleMetric(RuleMetricQueryRequest request) throws UnExpectedRequestException {
    CommonChecker.checkObject(request, "Rule Metric query request");
    if (StringUtils.isBlank(request.getSubSystemName())) {
        request.setSubSystemName("");
    }
    if (StringUtils.isBlank(request.getRuleMetricName())) {
        request.setRuleMetricName("%");
    } else {
        request.setRuleMetricName("%" + request.getRuleMetricName() + "%");
    }
    String userName = HttpUtils.getUserName(httpServletRequest);
    LOGGER.info("Start to get all rule metric, page request: [{}], user: [{}]", request.toString(), userName);
    User loginUser = userDao.findByUsername(userName);
    List<UserRole> userRoles = userRoleDao.findByUser(loginUser);
    Integer roleType = roleService.getRoleType(userRoles);
    List<RuleMetric> ruleMetrics = new ArrayList<>();
    long total = 0;
    if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
        LOGGER.info("SYS_ADMIN will get all rule metrics in query.");
        ruleMetrics.addAll(ruleMetricDao.queryAllRuleMetrics(request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable(), request.getPage(), request.getSize()));
        total = ruleMetricDao.countQueryAllRuleMetrics(request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable());
    } else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
        LOGGER.info("DEPARTMENT_ADMIN will get rule metrics of all management departments and all projectors.");
        List<Department> departments = new ArrayList<>();
        for (UserRole temp : userRoles) {
            Department department = temp.getRole().getDepartment();
            if (department != null) {
                departments.add(department);
            }
        }
        ruleMetrics.addAll(ruleMetricDao.queryRuleMetrics(RuleMetricLevelEnum.DEFAULT_METRIC.getCode(), departments, null, request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable(), request.getPage(), request.getSize()));
        total = ruleMetricDao.countQueryRuleMetrics(RuleMetricLevelEnum.DEFAULT_METRIC.getCode(), departments, null, request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable());
    } else {
        LOGGER.info("PROJECTOR  will get rule metrics of department and own.");
        List<Department> departments = new ArrayList<>();
        departments.add(loginUser.getDepartment());
        ruleMetrics.addAll(ruleMetricDao.queryRuleMetrics(RuleMetricLevelEnum.DEFAULT_METRIC.getCode(), departments, loginUser, request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable(), request.getPage(), request.getSize()));
        total = ruleMetricDao.countQueryRuleMetrics(RuleMetricLevelEnum.DEFAULT_METRIC.getCode(), departments, loginUser, request.getSubSystemName(), request.getRuleMetricName(), request.getEnCode(), request.getType(), request.getAvailable());
    }
    GetAllResponse<RuleMetricResponse> response = new GetAllResponse<>();
    List<RuleMetricResponse> ruleMetricResponses = new ArrayList<>();
    for (RuleMetric ruleMetric : ruleMetrics) {
        ruleMetricResponses.add(new RuleMetricResponse(ruleMetric));
    }
    response.setData(ruleMetricResponses);
    response.setTotal(total);
    return new GeneralResponse<>("200", "{&RULE_METRIC_QUERY_SUCCESS}", response);
}
Also used : ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) User(com.webank.wedatasphere.qualitis.entity.User) ArrayList(java.util.ArrayList) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) RuleMetricResponse(com.webank.wedatasphere.qualitis.response.RuleMetricResponse) Department(com.webank.wedatasphere.qualitis.entity.Department) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) List(java.util.List) ArrayList(java.util.ArrayList) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse)

Example 8 with UserRole

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

the class UserRoleDaoTest method test.

@Test
@Transactional
public void test() {
    User saveUser = saveUser();
    Role saveRole = saveRole();
    // 保存是否成功
    UserRole entity = new UserRole();
    entity.setId(UuidGenerator.generate());
    entity.setUser(saveUser);
    entity.setRole(saveRole);
    UserRole saveEntity = dao.saveUserRole(entity);
    assertNotNull(saveEntity.getId());
    // 总数量大于0
    long size = dao.countAll();
    assertTrue(size > 0);
    // 分页查询有结果
    List<UserRole> datas = dao.findAllUserRole(0, 5);
    assertTrue(datas.size() > 0);
    // 保存到数据库的对象是否和保存的值一致
    UserRole findByIdEntity = dao.findByUuid(saveEntity.getId());
    assertNotNull(findByIdEntity);
    assertEquals(findByIdEntity.getUser().getUserName(), saveEntity.getUser().getUserName());
    assertEquals(findByIdEntity.getRole().getName(), saveEntity.getRole().getName());
    UserRole findByUserAndRoleEntity = dao.findByUserAndRole(saveUser, saveRole);
    assertNotNull(findByUserAndRoleEntity);
    assertEquals(findByUserAndRoleEntity.getUser().getUserName(), saveEntity.getUser().getUserName());
    assertEquals(findByUserAndRoleEntity.getRole().getName(), saveEntity.getRole().getName());
    // 根据username查询的数据库对象是否和保存的值一致
    List<UserRole> findByRoleEntity = dao.findByRole(saveRole);
    assertTrue(findByRoleEntity.size() > 0);
    assertEquals(findByRoleEntity.get(0).getUser().getUserName(), saveEntity.getUser().getUserName());
    // 根据username查询的数据库对象是否和保存的值一致
    List<UserRole> findByUserEntity = dao.findByUser(saveUser);
    assertTrue(findByUserEntity.size() > 0);
    assertEquals(findByUserEntity.get(0).getRole().getName(), saveEntity.getRole().getName());
    // 删除后,是否还能找到对象
    dao.deleteUserRole(saveEntity);
    UserRole deleteEntity = dao.findByUuid(saveEntity.getId());
    assertNull(deleteEntity);
}
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) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with UserRole

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

the class UserRoleServiceImpl method deleteUserRole.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> deleteUserRole(DeleteUserRoleRequest request) throws UnExpectedRequestException {
    // Check Arguments
    checkRequest(request);
    // Check existence of user role
    String uuid = request.getUuid();
    UserRole userRoleInDb = userRoleDao.findByUuid(uuid);
    if (userRoleInDb == null) {
        throw new UnExpectedRequestException("user role id {&DOES_NOT_EXIST}, request: " + request);
    }
    // Delete user role
    userRoleDao.deleteUserRole(userRoleInDb);
    LOGGER.info("Succeed to delete user_role. uuid: {}, current_user: {}", request.getUuid(), HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&DELETE_USER_ROLE_SUCCESSFULLY}", null);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with UserRole

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

Aggregations

UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)24 User (com.webank.wedatasphere.qualitis.entity.User)21 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)15 Department (com.webank.wedatasphere.qualitis.entity.Department)14 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)12 ArrayList (java.util.ArrayList)11 Transactional (org.springframework.transaction.annotation.Transactional)11 List (java.util.List)10 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)9 RuleMetricDepartmentUser (com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser)9 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)8 ExcelRuleMetric (com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric)7 GetAllResponse (com.webank.wedatasphere.qualitis.response.GetAllResponse)7 Role (com.webank.wedatasphere.qualitis.entity.Role)6 RuleMetricResponse (com.webank.wedatasphere.qualitis.response.RuleMetricResponse)6 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)5 TemplateDepartment (com.webank.wedatasphere.qualitis.rule.entity.TemplateDepartment)5 TemplateUser (com.webank.wedatasphere.qualitis.rule.entity.TemplateUser)5 RuleTemplateResponse (com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse)4 TemplateDataSourceType (com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)3