Skip to content

Commit

Permalink
to feature apache#7110
Browse files Browse the repository at this point in the history
  • Loading branch information
ouyangyewei committed Dec 2, 2021
1 parent 6beae19 commit 63bbed7
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,30 @@ public Result grantProject(@ApiIgnore @RequestAttribute(value = Constants.SESSIO
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<String, Object> result = this.usersService.grantProjectByCode(loginUser, userId, projectCodes);
return returnDataList(result);
}

/**
* grant resource
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ Map<String, Object> updateUser(User loginUser, int userId, String userName, Stri
Map<String, Object> 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<String, Object> grantProjectByCode(User loginUser, int userId, String projectCodes);


/**
* grant resource
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,58 @@ public Map<String, Object> grantProject(User loginUser, int userId, String proje
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<String, Object> grantProjectByCode(final User loginUser, final int userId, final String projectCodes) {
Map<String, Object> 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
for (String projectCode : projectCodes.split(",")) {
final Project project = this.projectMapper.queryByCode(Long.parseLong(projectCode));
if (project != null) {
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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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=获取用户信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ public void testGrantProject() throws Exception {
logger.info(mvcResult.getResponse().getContentAsString());
}

@Test
public void testGrantProjectByCode() throws Exception {
MultiValueMap<String, String> 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<String, String> paramsMap = new LinkedMultiValueMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,29 @@ public void testGrantProject() {
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<String, Object> 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
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";
Expand Down

0 comments on commit 63bbed7

Please sign in to comment.