Browse Source

to feature #7110 (#7111)

Co-authored-by: ouyangyewei <yewei.oyyw@alibaba-inc.com>
3.0.0/version-upgrade
ouyangyewei 3 years ago committed by GitHub
parent
commit
27ea43d5df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/UsersController.java
  2. 11
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java
  3. 57
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java
  4. 2
      dolphinscheduler-api/src/main/resources/i18n/messages.properties
  5. 2
      dolphinscheduler-api/src/main/resources/i18n/messages_en_US.properties
  6. 2
      dolphinscheduler-api/src/main/resources/i18n/messages_zh_CN.properties
  7. 18
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/UsersControllerTest.java
  8. 24
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java
  9. 8
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.java
  10. 13
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml

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

@ -234,6 +234,30 @@ public class UsersController extends BaseController {
return returnDataList(result); 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 * grant resource
* *

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

@ -153,6 +153,17 @@ public interface UsersService {
Map<String, Object> grantProject(User loginUser, int userId, String projectIds); 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 * grant resource
* *

57
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.io.IOException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Arrays;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -578,6 +579,62 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService {
return result; 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 * grant resource
* *

2
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 DELETE_USER_BY_ID_NOTES=delete user by id
GRANT_PROJECT_NOTES=GRANT PROJECT GRANT_PROJECT_NOTES=GRANT PROJECT
PROJECT_IDS=project ids(string format, multiple projects separated by ",") 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 GRANT_RESOURCE_NOTES=grant resource file
RESOURCE_IDS=resource ids(string format, multiple resources separated by ",") RESOURCE_IDS=resource ids(string format, multiple resources separated by ",")
GET_USER_INFO_NOTES=get user info GET_USER_INFO_NOTES=get user info

2
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 DELETE_USER_BY_ID_NOTES=delete user by id
GRANT_PROJECT_NOTES=GRANT PROJECT GRANT_PROJECT_NOTES=GRANT PROJECT
PROJECT_IDS=project ids(string format, multiple projects separated by ",") 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 GRANT_RESOURCE_NOTES=grant resource file
RESOURCE_IDS=resource ids(string format, multiple resources separated by ",") RESOURCE_IDS=resource ids(string format, multiple resources separated by ",")
GET_USER_INFO_NOTES=get user info GET_USER_INFO_NOTES=get user info

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

@ -256,6 +256,8 @@ UPDATE_QUEUE_NOTES=更新队列
DELETE_USER_BY_ID_NOTES=删除用户通过ID DELETE_USER_BY_ID_NOTES=删除用户通过ID
GRANT_PROJECT_NOTES=授权项目 GRANT_PROJECT_NOTES=授权项目
PROJECT_IDS=项目IDS(字符串格式,多个项目以","分割) PROJECT_IDS=项目IDS(字符串格式,多个项目以","分割)
GRANT_PROJECT_BY_CODE_NOTES=授权项目
PROJECT_CODES=项目Codes(字符串格式,多个项目Code以","分割)
GRANT_RESOURCE_NOTES=授权资源文件 GRANT_RESOURCE_NOTES=授权资源文件
RESOURCE_IDS=资源ID列表(字符串格式,多个资源ID以","分割) RESOURCE_IDS=资源ID列表(字符串格式,多个资源ID以","分割)
GET_USER_INFO_NOTES=获取用户信息 GET_USER_INFO_NOTES=获取用户信息

18
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()); 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 @Test
public void testGrantResource() throws Exception { public void testGrantResource() throws Exception {
MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>(); MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();

24
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)); 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 @Test
public void testGrantResources() { public void testGrantResources() {
String resourceIds = "100000,120000"; String resourceIds = "100000,120000";

8
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 org.apache.ibatis.annotations.Param;
import java.util.Collection;
import java.util.List; import java.util.List;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@ -38,6 +39,13 @@ public interface ProjectMapper extends BaseMapper<Project> {
*/ */
Project queryByCode(@Param("projectCode") long projectCode); 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 * TODO: delete
* query project detail by id * query project detail by id

13
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ProjectMapper.xml

@ -32,6 +32,19 @@
where code = #{projectCode} where code = #{projectCode}
</select> </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 id="queryDetailByCode" resultType="org.apache.dolphinscheduler.dao.entity.Project">
select select
<include refid="baseSqlV2"> <include refid="baseSqlV2">

Loading…
Cancel
Save