use of com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException in project Qualitis by WeBankFinTech.
the class OuterExecutionServiceImpl method ruleListExecution.
@Override
public GeneralResponse<?> ruleListExecution(RuleListExecutionRequest request, Integer invokeCode) throws UnExpectedRequestException, PermissionDeniedRequestException {
LOGGER.info("Execute application by rules. rule_ids: {}", request.getRuleList());
// Check Arguments.
RuleListExecutionRequest.checkRequest(request);
String loginUser = getLoginUser(httpServletRequest, request.getCreateUser(), request.getAsync());
// Check the existence of rule.
List<Rule> rules = ruleDao.findByIds(request.getRuleList());
if (CollectionUtils.isEmpty(rules)) {
throw new UnExpectedRequestException("{&NO_RULE_CAN_BE_EXECUTED}");
}
List<Long> ruleIds = rules.stream().map(Rule::getId).distinct().collect(Collectors.toList());
// Check rule exists on the request.
List<Long> notExistRules = request.getRuleList().stream().filter(ruleId -> !ruleIds.contains(ruleId)).collect(Collectors.toList());
if (!notExistRules.isEmpty()) {
throw new UnExpectedRequestException("The ids of rule: " + notExistRules.toString() + " {&DOES_NOT_EXIST}");
}
LOGGER.info("Succeed to find all rules.");
checkPermissionCreateUserProxyExecuteUser(request.getCreateUser(), request.getExecutionUser());
StringBuffer runDate = new StringBuffer();
StringBuffer partition = new StringBuffer();
Map<String, String> execParamMap = new HashMap<>(5);
parseExecParams(partition, runDate, request.getExecutionParam(), execParamMap);
ApplicationProjectResponse applicationProjectResponse = new ApplicationProjectResponse();
List<ApplicationSubmitRequest> applicationSubmitRequests = new ArrayList<>(rules.size());
List<Project> projects = rules.stream().map(Rule::getProject).distinct().collect(Collectors.toList());
GeneralResponse<ApplicationTaskSimpleResponse> generalResponse;
// Check permissions of projects.
for (Project projectInDb : projects) {
List<Integer> permissions = new ArrayList<>();
permissions.add(ProjectUserPermissionEnum.OPERATOR.getCode());
projectService.checkProjectPermission(projectInDb, loginUser, permissions);
List<Rule> currentRules = rules.stream().filter(rule -> rule.getProject().getId().equals(projectInDb.getId())).collect(Collectors.toList());
List<RuleGroup> currentRuleGroups = currentRules.stream().map(Rule::getRuleGroup).distinct().collect(Collectors.toList());
for (RuleGroup ruleGroup : currentRuleGroups) {
List<Rule> currentRulesOfGroup = currentRules.stream().filter(rule -> rule.getRuleGroup().getRuleGroupName().equals(ruleGroup.getRuleGroupName())).distinct().collect(Collectors.toList());
List<Long> currentRuleIds = currentRulesOfGroup.stream().map(Rule::getId).distinct().collect(Collectors.toList());
// Dynamic partition.
try {
submitRulesWithDynamicPartition(applicationSubmitRequests, projectInDb.getId(), ruleGroup.getId(), currentRulesOfGroup, currentRuleIds, request.getExecutionUser(), request.getDyNamicPartition(), request.getClusterName(), partition, request.getDyNamicPartitionPrefix());
if (CollectionUtils.isEmpty(currentRuleIds)) {
continue;
}
} catch (ResourceAccessException e) {
// Record submit failed applicatoin.
generateAbnormalApplicationInfo(projectInDb.getId(), ruleGroup.getId(), request.getCreateUser(), request.getExecutionUser(), new Date(), invokeCode, partition.toString(), request.getStartupParamName(), request.getExecutionParam(), e, ApplicationCommentEnum.METADATA_ISSUES.getCode(), ApplicationStatusEnum.TASK_SUBMIT_FAILED.getCode(), rules);
LOGGER.error("One group execution[id={}] of the rule list execution start failed!", ruleGroup.getId());
} catch (NoPartitionException e) {
// Record submit failed applicatoin.
generateAbnormalApplicationInfo(projectInDb.getId(), ruleGroup.getId(), request.getCreateUser(), request.getExecutionUser(), new Date(), invokeCode, partition.toString(), request.getStartupParamName(), request.getExecutionParam(), e, ApplicationCommentEnum.METADATA_ISSUES.getCode(), ApplicationStatusEnum.TASK_SUBMIT_FAILED.getCode(), rules);
LOGGER.error("One group execution[id={}] of the rule list execution start failed!", ruleGroup.getId());
} catch (Exception e) {
// Record submit failed applicatoin.
generateAbnormalApplicationInfo(projectInDb.getId(), ruleGroup.getId(), request.getCreateUser(), request.getExecutionUser(), new Date(), invokeCode, partition.toString(), request.getStartupParamName(), request.getExecutionParam(), e, ApplicationCommentEnum.METADATA_ISSUES.getCode(), ApplicationStatusEnum.TASK_SUBMIT_FAILED.getCode(), rules);
LOGGER.error("One group execution[id={}] of the rule list execution start failed!", ruleGroup.getId());
}
applicationSubmitRequests.add(new ApplicationSubmitRequest(projectInDb.getId(), ruleGroup.getId(), currentRuleIds, partition));
}
projectEventService.record(projectInDb.getId(), loginUser, "submit rule list execution", "rule name[" + Arrays.toString(currentRules.toArray()) + "].", EventTypeEnum.SUBMIT_PROJECT.getCode());
}
for (ApplicationSubmitRequest applicationSubmitRequest : applicationSubmitRequests) {
generalResponse = (GeneralResponse<ApplicationTaskSimpleResponse>) outerExecutionService.submitRules(applicationSubmitRequest.getRuleIds(), applicationSubmitRequest.getPartition(), loginUser, request.getExecutionUser(), DEFAULT_NODE_NAME, applicationSubmitRequest.getProjectId(), applicationSubmitRequest.getRuleGroupId(), request.getStartupParamName(), request.getClusterName(), request.getSetFlag(), execParamMap, request.getExecutionParam(), runDate, invokeCode);
applicationProjectResponse.getApplicationTaskSimpleResponses().add(generalResponse.getData());
}
return new GeneralResponse<>("200", "{&SUCCEED_TO_DISPATCH_TASK}", applicationProjectResponse);
}
use of com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException in project Qualitis by WeBankFinTech.
the class RuleTemplateServiceImpl method modifyRuleTemplate.
@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public RuleTemplateResponse modifyRuleTemplate(ModifyRuleTemplateRequest request) throws UnExpectedRequestException, PermissionDeniedRequestException {
AddRuleTemplateRequest addRuleTemplateRequest = ModifyRuleTemplateRequest.checkRequest(request);
// Check template existence
Template templateInDb = checkRuleTemplate(request.getTemplateId());
// Check user info.
User userInDb = userDao.findById(HttpUtils.getUserId(httpServletRequest));
List<UserRole> userRoles = userRoleDao.findByUser(userInDb);
Integer roleType = roleService.getRoleType(userRoles);
if (roleType.equals(RoleDefaultTypeEnum.PROJECTOR.getCode())) {
LOGGER.info("The projector is going to modify template.");
if (templateInDb.getLevel().intValue() != RuleTemplateLevelEnum.PERSONAL_TEMPLATE.getCode()) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
if (!userInDb.equals(templateInDb.getCreateUser())) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
} else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
LOGGER.info("The department admin is going to modify template.");
if (templateInDb.getLevel().intValue() == RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode()) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
List<User> createUsers = new ArrayList<>();
for (UserRole userRole : userRoles) {
Department department = userRole.getRole().getDepartment();
if (department != null) {
List<User> userList = userDao.findByDepartment(department);
createUsers.addAll(userList);
}
}
if (!createUsers.contains(templateInDb.getCreateUser())) {
throw new PermissionDeniedRequestException("User {&HAS_NO_PERMISSION_TO_ACCESS}", 403);
}
} else {
LOGGER.info("The system admin is going to modify template.");
}
// delete output meta
templateOutputMetaService.deleteByTemplate(templateInDb);
// delete mid_table input meta
templateMidTableInputMetaService.deleteByTemplate(templateInDb);
// delete statistics input meta
templateStatisticsInputMetaService.deleteByTemplate(templateInDb);
// delete template type relationship
templateDataSourceTypeDao.deleteByTemplate(templateInDb);
// Save template.
if (!templateInDb.getName().equals(request.getTemplateName())) {
checkTemplateName(request.getTemplateName());
}
templateInDb.setName(request.getTemplateName());
templateInDb.setClusterNum(request.getClusterNum());
templateInDb.setDbNum(request.getDbNum());
templateInDb.setTableNum(request.getTableNum());
templateInDb.setFieldNum(request.getFieldNum());
templateInDb.setActionType(request.getActionType());
templateInDb.setMidTableAction(request.getMidTableAction());
templateInDb.setSaveMidTable(request.getSaveMidTable());
templateInDb.setShowSql(request.getMidTableAction());
templateInDb.setTemplateType(request.getTemplateType());
templateInDb.setModifyUser(userInDb);
Template savedTemplate = ruleTemplateDao.saveTemplate(templateInDb);
List<TemplateDataSourceType> templateDataSourceTypes = templateDataSourceTypeDao.findByTemplate(savedTemplate);
List<Integer> templateDataSourceTypeIntegers = templateDataSourceTypes.stream().map(TemplateDataSourceType::getDataSourceTypeId).collect(Collectors.toList());
for (Integer type : request.getDatasourceType()) {
if (!templateDataSourceTypeIntegers.contains(type)) {
TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType(type, savedTemplate);
templateDataSourceTypeDao.save(templateDataSourceType);
}
}
LOGGER.info("Succeed to save rule template, template_id: {}", savedTemplate.getId());
// Save template info.
createAndSaveTemplateInfo(savedTemplate, addRuleTemplateRequest);
return new RuleTemplateResponse(savedTemplate);
}
Aggregations