From 27ea43d5dfda2556fa5fb02179e7c906408007ad Mon Sep 17 00:00:00 2001 From: ouyangyewei Date: Sat, 4 Dec 2021 21:43:14 +0800 Subject: [PATCH] to feature #7110 (#7111) Co-authored-by: ouyangyewei --- .../api/controller/UsersController.java | 24 ++++++++ .../api/service/UsersService.java | 11 ++++ .../api/service/impl/UsersServiceImpl.java | 57 +++++++++++++++++++ .../main/resources/i18n/messages.properties | 2 + .../resources/i18n/messages_en_US.properties | 2 + .../resources/i18n/messages_zh_CN.properties | 2 + .../api/controller/UsersControllerTest.java | 18 ++++++ .../api/service/UsersServiceTest.java | 24 ++++++++ .../dao/mapper/ProjectMapper.java | 8 +++ .../dao/mapper/ProjectMapper.xml | 13 +++++ 10 files changed, 161 insertions(+) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java index 11de0389ad..4a12460bc8 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java @@ -234,6 +234,30 @@ public class UsersController extends BaseController { return returnDataList(result); } + /** + * grant project by code + * + * @param loginUser login user + * @param userId user id + * @param projectCodes project code array + * @return grant result code + */ + @ApiOperation(value = "grantProjectByCode", notes = "GRANT_PROJECT_BY_CODE_NOTES") + @ApiImplicitParams({ + @ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int", example = "100"), + @ApiImplicitParam(name = "projectCodes", value = "PROJECT_CODES", required = true, type = "String") + }) + @PostMapping(value = "/grant-project-by-code") + @ResponseStatus(HttpStatus.OK) + @ApiException(GRANT_PROJECT_ERROR) + @AccessLogAnnotation + public Result grantProjectByCode(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "userId") int userId, + @RequestParam(value = "projectCodes") String projectCodes) { + Map result = this.usersService.grantProjectByCode(loginUser, userId, projectCodes); + return returnDataList(result); + } + /** * grant resource * diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java index a5c004b964..c50a37b755 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java @@ -153,6 +153,17 @@ public interface UsersService { Map grantProject(User loginUser, int userId, String projectIds); + /** + * grant project by code + * + * @param loginUser login user + * @param userId user id + * @param projectCodes project code array + * @return grant result code + */ + Map grantProjectByCode(User loginUser, int userId, String projectCodes); + + /** * grant resource * diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java index 5199d4f39d..52a78a6162 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java @@ -59,6 +59,7 @@ import org.apache.commons.lang.StringUtils; import java.io.IOException; import java.text.MessageFormat; +import java.util.Arrays; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; @@ -578,6 +579,62 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService { return result; } + /** + * grant project by code + * + * @param loginUser login user + * @param userId user id + * @param projectCodes project code array + * @return grant result code + */ + @Override + public Map grantProjectByCode(final User loginUser, final int userId, final String projectCodes) { + Map result = new HashMap<>(); + result.put(Constants.STATUS, false); + + // 1. only admin can operate + if (this.check(result, !this.isAdmin(loginUser), Status.USER_NO_OPERATION_PERM)) { + return result; + } + + // 2. check if user is existed + User tempUser = this.userMapper.selectById(userId); + if (tempUser == null) { + putMsg(result, Status.USER_NOT_EXIST, userId); + return result; + } + + // 3. if the selected projectCodes are empty, delete all items associated with the user + if (this.check(result, StringUtils.isEmpty(projectCodes), Status.SUCCESS)) { + this.projectUserMapper.deleteProjectRelation(0, userId); + return result; + } + + // 4. maintain the relationship between project and user + Set projectCodeSet = Arrays.stream(projectCodes.split(Constants.COMMA)).map(Long::parseLong).collect(Collectors.toSet()); + final List projectList = this.projectMapper.queryByCodes(projectCodeSet); + if (CollectionUtils.isEmpty(projectList)) { + logger.info("project not exists"); + putMsg(result, Status.PROJECT_NOT_FOUNT, projectCodes); + return result; + } + for (final Project project : projectList) { + final Date today = new Date(); + + ProjectUser projectUser = new ProjectUser(); + projectUser.setUserId(userId); + projectUser.setProjectId(project.getId()); + projectUser.setPerm(7); + projectUser.setCreateTime(today); + projectUser.setUpdateTime(today); + this.projectUserMapper.insert(projectUser); + } + + putMsg(result, Status.SUCCESS); + + return result; + } + /** * grant resource * diff --git a/dolphinscheduler-api/src/main/resources/i18n/messages.properties b/dolphinscheduler-api/src/main/resources/i18n/messages.properties index 62d3615664..ab1cbe6d12 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages.properties @@ -221,6 +221,8 @@ UPDATE_USER_NOTES=update user DELETE_USER_BY_ID_NOTES=delete user by id GRANT_PROJECT_NOTES=GRANT PROJECT PROJECT_IDS=project ids(string format, multiple projects separated by ",") +GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE +PROJECT_CODES=project codes(string format, multiple project codes separated by ",") GRANT_RESOURCE_NOTES=grant resource file RESOURCE_IDS=resource ids(string format, multiple resources separated by ",") GET_USER_INFO_NOTES=get user info 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 db705be6ab..074aa94f8b 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties @@ -268,6 +268,8 @@ UPDATE_QUEUE_NOTES=update queue DELETE_USER_BY_ID_NOTES=delete user by id GRANT_PROJECT_NOTES=GRANT PROJECT PROJECT_IDS=project ids(string format, multiple projects separated by ",") +GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE +PROJECT_CODES=project codes(string format, multiple project codes separated by ",") GRANT_RESOURCE_NOTES=grant resource file RESOURCE_IDS=resource ids(string format, multiple resources separated by ",") GET_USER_INFO_NOTES=get user info 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 ec88f74fb5..2cd80ca551 100644 --- a/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties +++ b/dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties @@ -256,6 +256,8 @@ UPDATE_QUEUE_NOTES=更新队列 DELETE_USER_BY_ID_NOTES=删除用户通过ID GRANT_PROJECT_NOTES=授权项目 PROJECT_IDS=项目IDS(字符串格式,多个项目以","分割) +GRANT_PROJECT_BY_CODE_NOTES=授权项目 +PROJECT_CODES=项目Codes(字符串格式,多个项目Code以","分割) GRANT_RESOURCE_NOTES=授权资源文件 RESOURCE_IDS=资源ID列表(字符串格式,多个资源ID以","分割) GET_USER_INFO_NOTES=获取用户信息 diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UsersControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UsersControllerTest.java index a8546de00e..d6a426f730 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UsersControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UsersControllerTest.java @@ -107,6 +107,24 @@ public class UsersControllerTest extends AbstractControllerTest { logger.info(mvcResult.getResponse().getContentAsString()); } + @Test + public void testGrantProjectByCode() throws Exception { + MultiValueMap paramsMap = new LinkedMultiValueMap<>(); + paramsMap.add("userId", "32"); + paramsMap.add("projectCodes", "3682329499136,3643998558592"); + + MvcResult mvcResult = mockMvc.perform(post("/users/grant-project-by-code") + .header(SESSION_ID, sessionId) + .params(paramsMap)) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + + Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); + Assert.assertEquals(Status.USER_NOT_EXIST.getCode(), result.getCode().intValue()); + logger.info(mvcResult.getResponse().getContentAsString()); + } + @Test public void testGrantResource() throws Exception { MultiValueMap paramsMap = new LinkedMultiValueMap<>(); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java index e586db8cbf..2bb8dec8af 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java @@ -338,6 +338,30 @@ public class UsersServiceTest { Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); } + @Test + public void testGrantProjectByCode() { + when(userMapper.selectById(1)).thenReturn(getUser()); + + // user no permission + User loginUser = new User(); + String projectCodes = "3682329499136,3643998558592"; + Map result = this.usersService.grantProjectByCode(loginUser, 1, projectCodes); + logger.info(result.toString()); + Assert.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS)); + + // user not exist + loginUser.setUserType(UserType.ADMIN_USER); + result = this.usersService.grantProjectByCode(loginUser, 2, projectCodes); + logger.info(result.toString()); + Assert.assertEquals(Status.USER_NOT_EXIST, result.get(Constants.STATUS)); + + // success + Mockito.when(this.projectMapper.queryByCodes(Mockito.anyCollection())).thenReturn(Lists.newArrayList(new Project())); + result = this.usersService.grantProjectByCode(loginUser, 1, projectCodes); + logger.info(result.toString()); + Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); + } + @Test public void testGrantResources() { String resourceIds = "100000,120000"; 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 509fedbe2f..ce1f845687 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 @@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.dao.entity.ProjectUser; import org.apache.ibatis.annotations.Param; +import java.util.Collection; import java.util.List; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @@ -38,6 +39,13 @@ public interface ProjectMapper extends BaseMapper { */ Project queryByCode(@Param("projectCode") long projectCode); + /** + * query project detail by code list + * @param codes codes + * @return project list + */ + List queryByCodes(@Param("codes") Collection codes); + /** * TODO: delete * query project detail by id 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 d1cc5f7d62..e9c0aaefb2 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 @@ -32,6 +32,19 @@ where code = #{projectCode} + +