From 8fd3932bff374a03ef159bd2a2f495728c8e5d4e Mon Sep 17 00:00:00 2001 From: zixi0825 <649790970@qq.com> Date: Tue, 20 Oct 2020 20:25:33 +0800 Subject: [PATCH] [Fix][api] Add queryProjectCreatedAndAuthorizedByUser function (#3658) * fix getLoginUserCreated bug * fix getLoginUserCreated bug 1 * fix checkstyle * modify function name * modify comment * fix unit test error * rollback sqoop task hive target file Co-authored-by: sunchaohe Co-authored-by: dailidong --- .../api/controller/ProjectController.java | 17 +++++++------ .../dolphinscheduler/api/enums/Status.java | 6 ++--- .../api/service/ProjectService.java | 7 ++++++ .../api/service/impl/ProjectServiceImpl.java | 22 +++++++++++++++++ .../main/resources/i18n/messages.properties | 1 + .../resources/i18n/messages_en_US.properties | 1 + .../resources/i18n/messages_zh_CN.properties | 1 + .../api/service/ProjectServiceTest.java | 24 +++++++++++++++++-- .../dao/mapper/ProjectMapper.java | 7 ++++++ .../dao/mapper/ProjectMapper.xml | 4 ++++ .../src/js/conf/home/store/dag/actions.js | 2 +- 11 files changed, 77 insertions(+), 15 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java index dac97bca9d..1bf5003946 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java @@ -228,21 +228,20 @@ public class ProjectController extends BaseController { } /** - * query user created project + * query authorized and user created project * * @param loginUser login user - * @return projects which the user create + * @return projects which the user create and authorized */ - @ApiOperation(value = "queryProjectCreatedByUser", notes = "QUERY_USER_CREATED_PROJECT_NOTES") - - @GetMapping(value = "/login-user-created-project") + @ApiOperation(value = "queryProjectCreatedAndAuthorizedByUser", notes = "QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES") + @GetMapping(value = "/created-and-authorized-project") @ResponseStatus(HttpStatus.OK) - @ApiException(QUERY_USER_CREATED_PROJECT_ERROR) - public Result queryProjectCreatedByUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { - logger.info("login user {}, query authorized project by user id: {}.", + @ApiException(QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_ERROR) + public Result queryProjectCreatedAndAuthorizedByUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { + logger.info("login user {}, query authorized and user created project by user id: {}.", StringUtils.replaceNRTtoUnderline(loginUser.getUserName()), StringUtils.replaceNRTtoUnderline(String.valueOf(loginUser.getId()))); - Map result = projectService.queryProjectCreatedByUser(loginUser); + Map result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser); return returnDataList(result); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java index 6544787c00..c6ad1cff5c 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/enums/Status.java @@ -193,9 +193,9 @@ public enum Status { BATCH_COPY_PROCESS_DEFINITION_ERROR(10159, "batch copy process definition error", "复制工作流错误"), BATCH_MOVE_PROCESS_DEFINITION_ERROR(10160, "batch move process definition error", "移动工作流错误"), QUERY_WORKFLOW_LINEAGE_ERROR(10161, "query workflow lineage error", "查询血缘失败"), - DELETE_PROCESS_DEFINITION_BY_ID_FAIL(10162, "delete process definition by id fail, for there are {0} process instances in executing using it", "删除工作流定义失败,有[{0}]个运行中的工作流实例正在使用"), - CHECK_TENANT_CODE_ERROR(10163, "Please enter the English tenant code", "请输入英文租户编码"), - + QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_ERROR(10162, "query authorized and user created project error error", "查询授权的和用户创建的项目错误"), + DELETE_PROCESS_DEFINITION_BY_ID_FAIL(10163,"delete process definition by id fail, for there are {0} process instances in executing using it", "删除工作流定义失败,有[{0}]个运行中的工作流实例正在使用"), + CHECK_TENANT_CODE_ERROR(10164, "Please enter the English tenant code", "请输入英文租户编码"), UDF_FUNCTION_NOT_EXIST(20001, "UDF function not found", "UDF函数不存在"), UDF_FUNCTION_EXISTS(20002, "UDF function already exists", "UDF函数已存在"), diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java index ca0e1fc0ec..4753725d31 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java @@ -120,4 +120,11 @@ public interface ProjectService { */ Map queryAllProjectList(); + /** + * query authorized and user create project list by user id + * @param loginUser + * @return + */ + Map queryProjectCreatedAndAuthorizedByUser(User loginUser); + } \ No newline at end of file diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java index 395da6027f..fd47342c12 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java @@ -375,6 +375,28 @@ public class ProjectServiceImpl extends BaseService implements ProjectService { return result; } + /** + * query authorized and user create project list by user + * + * @param loginUser login user + * @return + */ + public Map queryProjectCreatedAndAuthorizedByUser(User loginUser) { + Map result = new HashMap<>(); + + List projects = null; + if (loginUser.getUserType() == UserType.ADMIN_USER) { + projects = projectMapper.selectList(null); + } else { + projects = projectMapper.queryProjectCreatedAndAuthorizedByUserId(loginUser.getId()); + } + + result.put(Constants.DATA_LIST, projects); + putMsg(result, Status.SUCCESS); + + return result; + } + /** * check whether have read permission * diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages.properties b/dolphinscheduler-api/src/main/resources/i18n/messages.properties index d1da3e94a8..b2d601c434 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages.properties @@ -254,6 +254,7 @@ QUERY_ALERT_GROUP_LIST_PAGING_NOTES=query alert group list paging EXPORT_PROCESS_DEFINITION_BY_ID_NOTES=export process definition by id BATCH_EXPORT_PROCESS_DEFINITION_BY_IDS_NOTES= batch export process definition by ids QUERY_USER_CREATED_PROJECT_NOTES= query user created project +QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES= query authorized and user created project COPY_PROCESS_DEFINITION_NOTES= copy process definition notes MOVE_PROCESS_DEFINITION_NOTES= move process definition notes TARGET_PROJECT_ID= target project id diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties index 267f93b14d..db1fa2585f 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties @@ -254,6 +254,7 @@ QUERY_ALERT_GROUP_LIST_PAGING_NOTES=query alert group list paging EXPORT_PROCESS_DEFINITION_BY_ID_NOTES=export process definition by id BATCH_EXPORT_PROCESS_DEFINITION_BY_IDS_NOTES= batch export process definition by ids QUERY_USER_CREATED_PROJECT_NOTES= query user created project +QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES= query authorized and user created project COPY_PROCESS_DEFINITION_NOTES= copy process definition notes MOVE_PROCESS_DEFINITION_NOTES= move process definition notes TARGET_PROJECT_ID= target project id diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties index 16262e6bbc..d32f9d6955 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties @@ -252,6 +252,7 @@ QUERY_ALERT_GROUP_LIST_PAGING_NOTES=分页查询告警组列表 EXPORT_PROCESS_DEFINITION_BY_ID_NOTES=通过工作流ID导出工作流定义 BATCH_EXPORT_PROCESS_DEFINITION_BY_IDS_NOTES=批量导出工作流定义 QUERY_USER_CREATED_PROJECT_NOTES= 查询用户创建的项目 +QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES= 查询授权和用户创建的项目 COPY_PROCESS_DEFINITION_NOTES= 复制工作流定义 MOVE_PROCESS_DEFINITION_NOTES= 移动工作流定义 TARGET_PROJECT_ID= 目标项目ID diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java index 85b23b3fcb..cc39925aa5 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java @@ -295,6 +295,28 @@ public class ProjectServiceTest { } + @Test + public void testQueryProjectCreatedAndAuthorizedByUser() { + + Map result = null; + User loginUser = getLoginUser(); + + // not admin user + Mockito.when(projectMapper.queryProjectCreatedAndAuthorizedByUserId(1)).thenReturn(getList()); + result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser); + List notAdminUserResult = (List) result.get(Constants.DATA_LIST); + Assert.assertTrue(CollectionUtils.isNotEmpty(notAdminUserResult)); + + //admin user + loginUser.setUserType(UserType.ADMIN_USER); + Mockito.when(projectMapper.selectList(null)).thenReturn(getList()); + result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser); + List projects = (List) result.get(Constants.DATA_LIST); + + Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); + + } + @Test public void testQueryAllProjectList() { @@ -340,13 +362,11 @@ public class ProjectServiceTest { * create admin user */ private User getLoginUser() { - User loginUser = new User(); loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserName(userName); loginUser.setId(1); return loginUser; - } /** diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java index 424cb8e869..b24fd2ede4 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java @@ -74,4 +74,11 @@ public interface ProjectMapper extends BaseMapper { */ List queryProjectExceptUserId(@Param("userId") int userId); + /** + * query project list by userId + * @param userId + * @return + */ + List queryProjectCreatedAndAuthorizedByUserId(@Param("userId") int userId); + } diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml index 5ab0756250..17a57c8a89 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml @@ -64,5 +64,9 @@ from t_ds_project where user_id = #{userId} + \ No newline at end of file diff --git a/dolphinscheduler-ui/src/js/conf/home/store/dag/actions.js b/dolphinscheduler-ui/src/js/conf/home/store/dag/actions.js index b35d07052a..15caf5a49a 100644 --- a/dolphinscheduler-ui/src/js/conf/home/store/dag/actions.js +++ b/dolphinscheduler-ui/src/js/conf/home/store/dag/actions.js @@ -227,7 +227,7 @@ export default { */ getAllItems ({ state }, payload) { return new Promise((resolve, reject) => { - io.get(`projects/login-user-created-project`, {}, res => { + io.get(`projects/created-and-authorized-project`, {}, res => { resolve(res) }).catch(e => { reject(e)