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