Skip to content

Commit

Permalink
to feature #7110 (#7111)
Browse files Browse the repository at this point in the history
Co-authored-by: ouyangyewei <[email protected]>
  • Loading branch information
ouyangyewei429 and ouyangyewei authored Dec 4, 2021
1 parent fa8ccd3 commit 27ea43d
Show file tree
Hide file tree
Showing 10 changed files with 161 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 @@ -59,6 +59,7 @@

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -578,6 +579,62 @@ 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
Set<Long> projectCodeSet = Arrays.stream(projectCodes.split(Constants.COMMA)).map(Long::parseLong).collect(Collectors.toSet());
final List<Project> 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
*
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 @@ -338,6 +338,30 @@ 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
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.apache.ibatis.annotations.Param;

import java.util.Collection;
import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
Expand All @@ -38,6 +39,13 @@ public interface ProjectMapper extends BaseMapper<Project> {
*/
Project queryByCode(@Param("projectCode") long projectCode);

/**
* query project detail by code list
* @param codes codes
* @return project list
*/
List<Project> queryByCodes(@Param("codes") Collection<Long> codes);

/**
* TODO: delete
* query project detail by id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@
where code = #{projectCode}
</select>

<select id="queryByCodes" resultType="org.apache.dolphinscheduler.dao.entity.Project">
select
<include refid="baseSql"/>
from t_ds_project
where 1 = 1
<if test="codes != null and codes.size() != 0">
and code in
<foreach collection="codes" index="index" item="i" open="(" separator="," close=")">
#{i}
</foreach>
</if>
</select>

<select id="queryDetailByCode" resultType="org.apache.dolphinscheduler.dao.entity.Project">
select
<include refid="baseSqlV2">
Expand Down

0 comments on commit 27ea43d

Please sign in to comment.