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 13, 2021
1 parent e41fc08 commit d394849
Show file tree
Hide file tree
Showing 8 changed files with 163 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 projectCode project code
* @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 = "projectCode", value = "PROJECT_CODE", required = true, type = "Long")
})
@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 = "projectCode") long projectCode) {
Map<String, Object> result = this.usersService.grantProjectByCode(loginUser, userId, projectCode);
return this.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 projectCode project code
* @return grant result code
*/
Map<String, Object> grantProjectByCode(User loginUser, int userId, long projectCode);


/**
* grant resource
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,53 @@ 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 projectCode project code
* @return grant result code
*/
@Override
public Map<String, Object> grantProjectByCode(final User loginUser, final int userId, final long projectCode) {
Map<String, Object> result = new HashMap<>();
result.put(Constants.STATUS, false);

// 1. check if user is existed
User tempUser = this.userMapper.selectById(userId);
if (tempUser == null) {
this.putMsg(result, Status.USER_NOT_EXIST, userId);
return result;
}

// 2. check if project is existed
Project project = this.projectMapper.queryByCode(projectCode);
if (project == null) {
this.putMsg(result, Status.PROJECT_NOT_FOUNT, projectCode);
return result;
}

// 3. only project owner can operate
if (!this.hasPerm(loginUser, project.getUserId())) {
this.putMsg(result, Status.USER_NO_OPERATION_PERM);
return result;
}

// 4. maintain the relationship between project and user
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);

this.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,7 @@ 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
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,7 @@ 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
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,7 @@ UPDATE_QUEUE_NOTES=更新队列
DELETE_USER_BY_ID_NOTES=删除用户通过ID
GRANT_PROJECT_NOTES=授权项目
PROJECT_IDS=项目IDS(字符串格式,多个项目以","分割)
GRANT_PROJECT_BY_CODE_NOTES=授权项目
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 @@ -108,6 +108,25 @@ 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("projectCode", "3682329499136");

MvcResult mvcResult = this.mockMvc
.perform(post("/users/grant-project-by-code")
.header(SESSION_ID, this.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 @@ -338,6 +338,49 @@ public void testGrantProject() {
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}

@Test
public void testGrantProjectByCode() {
// Mock Project, User
final long projectCode = 1L;
final int projectCreator = 1;
final int authorizer = 100;
Mockito.when(this.userMapper.selectById(authorizer)).thenReturn(this.getUser());
Mockito.when(this.userMapper.selectById(projectCreator)).thenReturn(this.getUser());
Mockito.when(this.projectMapper.queryByCode(projectCode)).thenReturn(this.getProject());

// ERROR: USER_NOT_EXIST
User loginUser = new User();
Map<String, Object> result = this.usersService.grantProjectByCode(loginUser, 999, projectCode);
logger.info(result.toString());
Assert.assertEquals(Status.USER_NOT_EXIST, result.get(Constants.STATUS));

// ERROR: PROJECT_NOT_FOUNT
result = this.usersService.grantProjectByCode(loginUser, authorizer, 999);
logger.info(result.toString());
Assert.assertEquals(Status.PROJECT_NOT_FOUNT, result.get(Constants.STATUS));

// ERROR: USER_NO_OPERATION_PERM
loginUser.setId(999);
loginUser.setUserType(UserType.GENERAL_USER);
result = this.usersService.grantProjectByCode(loginUser, authorizer, projectCode);
logger.info(result.toString());
Assert.assertEquals(Status.USER_NO_OPERATION_PERM, result.get(Constants.STATUS));

// SUCCESS: USER IS PROJECT OWNER
loginUser.setId(projectCreator);
loginUser.setUserType(UserType.GENERAL_USER);
result = this.usersService.grantProjectByCode(loginUser, authorizer, projectCode);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));

// SUCCESS: USER IS ADMINISTRATOR
loginUser.setId(999);
loginUser.setUserType(UserType.ADMIN_USER);
result = this.usersService.grantProjectByCode(loginUser, authorizer, projectCode);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
}

@Test
public void testGrantResources() {
String resourceIds = "100000,120000";
Expand Down Expand Up @@ -618,6 +661,22 @@ private User getDisabledUser() {
return user;
}

/**
* Get project
* @return
*/
private Project getProject() {
Project project = new Project();
project.setId(1);
project.setCode(1L);
project.setUserId(1);
project.setName("PJ-001");
project.setPerm(7);
project.setDefCount(0);
project.setInstRunningCount(0);
return project;
}

/**
* get user
*/
Expand Down

0 comments on commit d394849

Please sign in to comment.