Skip to content

Commit 63bbed7

Browse files
author
ouyangyewei
committed
to feature #7110
1 parent 6beae19 commit 63bbed7

8 files changed

Lines changed: 134 additions & 0 deletions

File tree

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,30 @@ public Result grantProject(@ApiIgnore @RequestAttribute(value = Constants.SESSIO
234234
return returnDataList(result);
235235
}
236236

237+
/**
238+
* grant project by code
239+
*
240+
* @param loginUser login user
241+
* @param userId user id
242+
* @param projectCodes project code array
243+
* @return grant result code
244+
*/
245+
@ApiOperation(value = "grantProjectByCode", notes = "GRANT_PROJECT_BY_CODE_NOTES")
246+
@ApiImplicitParams({
247+
@ApiImplicitParam(name = "userId", value = "USER_ID", required = true, dataType = "Int", example = "100"),
248+
@ApiImplicitParam(name = "projectCodes", value = "PROJECT_CODES", required = true, type = "String")
249+
})
250+
@PostMapping(value = "/grant-project-by-code")
251+
@ResponseStatus(HttpStatus.OK)
252+
@ApiException(GRANT_PROJECT_ERROR)
253+
@AccessLogAnnotation
254+
public Result grantProjectByCode(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
255+
@RequestParam(value = "userId") int userId,
256+
@RequestParam(value = "projectCodes") String projectCodes) {
257+
Map<String, Object> result = this.usersService.grantProjectByCode(loginUser, userId, projectCodes);
258+
return returnDataList(result);
259+
}
260+
237261
/**
238262
* grant resource
239263
*

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,17 @@ Map<String, Object> updateUser(User loginUser, int userId, String userName, Stri
153153
Map<String, Object> grantProject(User loginUser, int userId, String projectIds);
154154

155155

156+
/**
157+
* grant project by code
158+
*
159+
* @param loginUser login user
160+
* @param userId user id
161+
* @param projectCodes project code array
162+
* @return grant result code
163+
*/
164+
Map<String, Object> grantProjectByCode(User loginUser, int userId, String projectCodes);
165+
166+
156167
/**
157168
* grant resource
158169
*

dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,58 @@ public Map<String, Object> grantProject(User loginUser, int userId, String proje
590590
return result;
591591
}
592592

593+
/**
594+
* grant project by code
595+
*
596+
* @param loginUser login user
597+
* @param userId user id
598+
* @param projectCodes project code array
599+
* @return grant result code
600+
*/
601+
@Override
602+
public Map<String, Object> grantProjectByCode(final User loginUser, final int userId, final String projectCodes) {
603+
Map<String, Object> result = new HashMap<>();
604+
result.put(Constants.STATUS, false);
605+
606+
// 1. only admin can operate
607+
if (this.check(result, !this.isAdmin(loginUser), Status.USER_NO_OPERATION_PERM)) {
608+
return result;
609+
}
610+
611+
// 2. check if user is existed
612+
User tempUser = this.userMapper.selectById(userId);
613+
if (tempUser == null) {
614+
putMsg(result, Status.USER_NOT_EXIST, userId);
615+
return result;
616+
}
617+
618+
// 3. if the selected projectCodes are empty, delete all items associated with the user
619+
if (this.check(result, StringUtils.isEmpty(projectCodes), Status.SUCCESS)) {
620+
this.projectUserMapper.deleteProjectRelation(0, userId);
621+
return result;
622+
}
623+
624+
// 4. maintain the relationship between project and user
625+
for (String projectCode : projectCodes.split(",")) {
626+
final Project project = this.projectMapper.queryByCode(Long.parseLong(projectCode));
627+
if (project != null) {
628+
final Date today = new Date();
629+
630+
ProjectUser projectUser = new ProjectUser();
631+
projectUser.setUserId(userId);
632+
projectUser.setProjectId(project.getId());
633+
projectUser.setPerm(7);
634+
projectUser.setCreateTime(today);
635+
projectUser.setUpdateTime(today);
636+
this.projectUserMapper.insert(projectUser);
637+
}
638+
}
639+
640+
putMsg(result, Status.SUCCESS);
641+
642+
return result;
643+
}
644+
593645
/**
594646
* grant resource
595647
*

dolphinscheduler-api/src/main/resources/i18n/messages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ UPDATE_USER_NOTES=update user
221221
DELETE_USER_BY_ID_NOTES=delete user by id
222222
GRANT_PROJECT_NOTES=GRANT PROJECT
223223
PROJECT_IDS=project ids(string format, multiple projects separated by ",")
224+
GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE
225+
PROJECT_CODES=project codes(string format, multiple project codes separated by ",")
224226
GRANT_RESOURCE_NOTES=grant resource file
225227
RESOURCE_IDS=resource ids(string format, multiple resources separated by ",")
226228
GET_USER_INFO_NOTES=get user info

dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ UPDATE_QUEUE_NOTES=update queue
268268
DELETE_USER_BY_ID_NOTES=delete user by id
269269
GRANT_PROJECT_NOTES=GRANT PROJECT
270270
PROJECT_IDS=project ids(string format, multiple projects separated by ",")
271+
GRANT_PROJECT_BY_CODE_NOTES=GRANT PROJECT BY CODE
272+
PROJECT_CODES=project codes(string format, multiple project codes separated by ",")
271273
GRANT_RESOURCE_NOTES=grant resource file
272274
RESOURCE_IDS=resource ids(string format, multiple resources separated by ",")
273275
GET_USER_INFO_NOTES=get user info

dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ UPDATE_QUEUE_NOTES=更新队列
256256
DELETE_USER_BY_ID_NOTES=删除用户通过ID
257257
GRANT_PROJECT_NOTES=授权项目
258258
PROJECT_IDS=项目IDS(字符串格式,多个项目以","分割)
259+
GRANT_PROJECT_BY_CODE_NOTES=授权项目
260+
PROJECT_CODES=项目Codes(字符串格式,多个项目Code以","分割)
259261
GRANT_RESOURCE_NOTES=授权资源文件
260262
RESOURCE_IDS=资源ID列表(字符串格式,多个资源ID以","分割)
261263
GET_USER_INFO_NOTES=获取用户信息

dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UsersControllerTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ public void testGrantProject() throws Exception {
107107
logger.info(mvcResult.getResponse().getContentAsString());
108108
}
109109

110+
@Test
111+
public void testGrantProjectByCode() throws Exception {
112+
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
113+
paramsMap.add("userId", "32");
114+
paramsMap.add("projectCodes", "3682329499136,3643998558592");
115+
116+
MvcResult mvcResult = mockMvc.perform(post("/users/grant-project-by-code")
117+
.header(SESSION_ID, sessionId)
118+
.params(paramsMap))
119+
.andExpect(status().isOk())
120+
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
121+
.andReturn();
122+
123+
Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
124+
Assert.assertEquals(Status.USER_NOT_EXIST.getCode(), result.getCode().intValue());
125+
logger.info(mvcResult.getResponse().getContentAsString());
126+
}
127+
110128
@Test
111129
public void testGrantResource() throws Exception {
112130
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();

dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,29 @@ public void testGrantProject() {
342342
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
343343
}
344344

345+
@Test
346+
public void testGrantProjectByCode() {
347+
when(userMapper.selectById(1)).thenReturn(getUser());
348+
349+
// user no permission
350+
User loginUser = new User();
351+
String projectCodes = "3682329499136,3643998558592";
352+
Map<String, Object> result = this.usersService.grantProjectByCode(loginUser, 1, projectCodes);
353+
logger.info(result.toString());
354+
Assert.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS));
355+
356+
// user not exist
357+
loginUser.setUserType(UserType.ADMIN_USER);
358+
result = this.usersService.grantProjectByCode(loginUser, 2, projectCodes);
359+
logger.info(result.toString());
360+
Assert.assertEquals(Status.USER_NOT_EXIST, result.get(Constants.STATUS));
361+
362+
// success
363+
result = this.usersService.grantProjectByCode(loginUser, 1, projectCodes);
364+
logger.info(result.toString());
365+
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
366+
}
367+
345368
@Test
346369
public void testGrantResources() {
347370
String resourceIds = "100000,120000";

0 commit comments

Comments
 (0)