Browse Source

[Feature][Api] Refactor org.apache.dolphinscheduler.api.controller.ProjectController (#10832)

* [Feature][Api] Refactor org.apache.dolphinscheduler.api.controller.ProjectController
3.1.0-release
xuhhui 2 years ago committed by GitHub
parent
commit
8004e97b4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java
  2. 290
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectV2Controller.java
  3. 36
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/PageQueryDto.java
  4. 34
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectCreateRequest.java
  5. 39
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectCreateResponse.java
  6. 38
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectDeleteResponse.java
  7. 40
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectListPagingResponse.java
  8. 42
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectListResponse.java
  9. 40
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectQueryRequest.java
  10. 39
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectQueryResponse.java
  11. 42
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectUpdateRequest.java
  12. 39
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectUpdateResponse.java
  13. 42
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/user/UserListResponse.java
  14. 41
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java
  15. 146
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProjectServiceImpl.java
  16. 4
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java
  17. 28
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProjectControllerTest.java
  18. 165
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProjectV2ControllerTest.java
  19. 82
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java

29
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectController.java

@ -35,8 +35,6 @@ import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.ParameterUtils; import org.apache.dolphinscheduler.common.utils.ParameterUtils;
import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.User;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.DeleteMapping;
@ -87,8 +85,7 @@ public class ProjectController extends BaseController {
public Result createProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result createProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectName") String projectName, @RequestParam("projectName") String projectName,
@RequestParam(value = "description", required = false) String description) { @RequestParam(value = "description", required = false) String description) {
Map<String, Object> result = projectService.createProject(loginUser, projectName, description); return projectService.createProject(loginUser, projectName, description);
return returnDataList(result);
} }
/** /**
@ -116,8 +113,7 @@ public class ProjectController extends BaseController {
@RequestParam("projectName") String projectName, @RequestParam("projectName") String projectName,
@RequestParam(value = "description", required = false) String description, @RequestParam(value = "description", required = false) String description,
@RequestParam(value = "userName") String userName) { @RequestParam(value = "userName") String userName) {
Map<String, Object> result = projectService.update(loginUser, code, projectName, description, userName); return projectService.update(loginUser, code, projectName, description, userName);
return returnDataList(result);
} }
/** /**
@ -137,8 +133,7 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryProjectByCode(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result queryProjectByCode(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("code") long code) { @PathVariable("code") long code) {
Map<String, Object> result = projectService.queryByCode(loginUser, code); return projectService.queryByCode(loginUser, code);
return returnDataList(result);
} }
/** /**
@ -192,8 +187,7 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result deleteProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result deleteProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("code") Long code) { @PathVariable("code") Long code) {
Map<String, Object> result = projectService.deleteProject(loginUser, code); return projectService.deleteProject(loginUser, code);
return returnDataList(result);
} }
/** /**
@ -213,8 +207,7 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryUnauthorizedProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result queryUnauthorizedProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("userId") Integer userId) { @RequestParam("userId") Integer userId) {
Map<String, Object> result = projectService.queryUnauthorizedProject(loginUser, userId); return projectService.queryUnauthorizedProject(loginUser, userId);
return returnDataList(result);
} }
/** /**
@ -234,8 +227,7 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryAuthorizedProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result queryAuthorizedProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("userId") Integer userId) { @RequestParam("userId") Integer userId) {
Map<String, Object> result = projectService.queryAuthorizedProject(loginUser, userId); return projectService.queryAuthorizedProject(loginUser, userId);
return returnDataList(result);
} }
/** /**
@ -255,8 +247,7 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryAuthorizedUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser, public Result queryAuthorizedUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectCode") Long projectCode) { @RequestParam("projectCode") Long projectCode) {
Map<String, Object> result = this.projectService.queryAuthorizedUser(loginUser, projectCode); return projectService.queryAuthorizedUser(loginUser, projectCode);
return this.returnDataList(result);
} }
/** /**
@ -271,8 +262,7 @@ public class ProjectController extends BaseController {
@ApiException(QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_ERROR) @ApiException(QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryProjectCreatedAndAuthorizedByUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { public Result queryProjectCreatedAndAuthorizedByUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
Map<String, Object> result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser); return projectService.queryProjectCreatedAndAuthorizedByUser(loginUser);
return returnDataList(result);
} }
/** /**
@ -287,7 +277,6 @@ public class ProjectController extends BaseController {
@ApiException(LOGIN_USER_QUERY_PROJECT_LIST_PAGING_ERROR) @ApiException(LOGIN_USER_QUERY_PROJECT_LIST_PAGING_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser") @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryAllProjectList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) { public Result queryAllProjectList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
Map<String, Object> result = projectService.queryAllProjectList(loginUser); return projectService.queryAllProjectList(loginUser);
return returnDataList(result);
} }
} }

290
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProjectV2Controller.java

@ -0,0 +1,290 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.controller;
import static org.apache.dolphinscheduler.api.enums.Status.CREATE_PROJECT_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.DELETE_PROJECT_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.LOGIN_USER_QUERY_PROJECT_LIST_PAGING_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_AUTHORIZED_PROJECT;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_AUTHORIZED_USER;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_PROJECT_DETAILS_BY_CODE_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.QUERY_UNAUTHORIZED_PROJECT_ERROR;
import static org.apache.dolphinscheduler.api.enums.Status.UPDATE_PROJECT_ERROR;
import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.dto.project.ProjectCreateRequest;
import org.apache.dolphinscheduler.api.dto.project.ProjectCreateResponse;
import org.apache.dolphinscheduler.api.dto.project.ProjectDeleteResponse;
import org.apache.dolphinscheduler.api.dto.project.ProjectListPagingResponse;
import org.apache.dolphinscheduler.api.dto.project.ProjectListResponse;
import org.apache.dolphinscheduler.api.dto.project.ProjectQueryRequest;
import org.apache.dolphinscheduler.api.dto.project.ProjectQueryResponse;
import org.apache.dolphinscheduler.api.dto.project.ProjectUpdateRequest;
import org.apache.dolphinscheduler.api.dto.project.ProjectUpdateResponse;
import org.apache.dolphinscheduler.api.dto.user.UserListResponse;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.ProjectService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.ParameterUtils;
import org.apache.dolphinscheduler.dao.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
/**
* project controller
*/
@Api(tags = "PROJECT_TAG")
@RestController
@RequestMapping("/v2/projects")
public class ProjectV2Controller extends BaseController {
@Autowired
private ProjectService projectService;
/**
* create project
*
* @param loginUser login user
* @param projectCreateRequest projectCreateRequest
* @return ProjectResponse ProjectResponse
*/
@ApiOperation(value = "create", notes = "CREATE_PROJECT_NOTES")
@PostMapping(consumes = {"application/json"})
@ResponseStatus(HttpStatus.CREATED)
@ApiException(CREATE_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectCreateResponse createProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestBody ProjectCreateRequest projectCreateRequest) {
Result result = projectService.createProject(loginUser, projectCreateRequest.getProjectName(),
projectCreateRequest.getDescription());
return new ProjectCreateResponse(result);
}
/**
* update project
*
* @param loginUser login user
* @param code project code
* @param projectUpdateReq projectUpdateRequest
* @return result Result
*/
@ApiOperation(value = "update", notes = "UPDATE_PROJECT_NOTES")
@PutMapping(value = "/{code}", consumes = {"application/json"})
@ResponseStatus(HttpStatus.OK)
@ApiException(UPDATE_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectUpdateResponse updateProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("code") Long code,
@RequestBody ProjectUpdateRequest projectUpdateReq) {
Result result = projectService.update(loginUser, code, projectUpdateReq.getProjectName(),
projectUpdateReq.getDescription(), projectUpdateReq.getUserName());
return new ProjectUpdateResponse(result);
}
/**
* query project details by project code
*
* @param loginUser login user
* @param code project code
* @return project detail information
*/
@ApiOperation(value = "queryProjectByCode", notes = "QUERY_PROJECT_BY_ID_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "code", value = "PROJECT_CODE", dataType = "Long", example = "123456", required = true)
})
@GetMapping(value = "/{code}")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_PROJECT_DETAILS_BY_CODE_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectQueryResponse queryProjectByCode(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("code") long code) {
Result result = projectService.queryByCode(loginUser, code);
return new ProjectQueryResponse(result);
}
/**
* query project list paging
*
* @param loginUser login user
* @param projectQueryReq projectQueryReq
* @return project list which the login user have permission to see
*/
@ApiOperation(value = "queryProjectListPaging", notes = "QUERY_PROJECT_LIST_PAGING_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "searchVal", value = "SEARCH_VAL", dataType = "String", example = "test"),
@ApiImplicitParam(name = "pageSize", value = "PAGE_SIZE", required = true, dataType = "Int", example = "10"),
@ApiImplicitParam(name = "pageNo", value = "PAGE_NO", required = true, dataType = "Int", example = "1")
})
@GetMapping(consumes = {"application/json"})
@ResponseStatus(HttpStatus.OK)
@ApiException(LOGIN_USER_QUERY_PROJECT_LIST_PAGING_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectListPagingResponse queryProjectListPaging(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
ProjectQueryRequest projectQueryReq) {
Result result = checkPageParams(projectQueryReq.getPageNo(), projectQueryReq.getPageSize());
if (!result.checkResult()) {
return new ProjectListPagingResponse(result);
}
String searchVal = ParameterUtils.handleEscapes(projectQueryReq.getSearchVal());
result = projectService.queryProjectListPaging(loginUser, projectQueryReq.getPageSize(),
projectQueryReq.getPageNo(), searchVal);
return new ProjectListPagingResponse(result);
}
/**
* delete project by code
*
* @param loginUser login user
* @param code project code
* @return delete result code
*/
@ApiOperation(value = "delete", notes = "DELETE_PROJECT_BY_ID_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "code", value = "PROJECT_CODE", dataType = "Long", example = "123456", required = true)
})
@DeleteMapping(value = "/{code}")
@ResponseStatus(HttpStatus.OK)
@ApiException(DELETE_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectDeleteResponse deleteProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("code") Long code) {
Result result = projectService.deleteProject(loginUser, code);
return new ProjectDeleteResponse(result);
}
/**
* query unauthorized project
*
* @param loginUser login user
* @param userId user id
* @return the projects which user have not permission to see
*/
@ApiOperation(value = "queryUnauthorizedProject", notes = "QUERY_UNAUTHORIZED_PROJECT_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "USER_ID", dataType = "Int", example = "100", required = true)
})
@GetMapping(value = "/unauth-project")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_UNAUTHORIZED_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectListResponse queryUnauthorizedProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("userId") Integer userId) {
Result result = projectService.queryUnauthorizedProject(loginUser, userId);
return new ProjectListResponse(result);
}
/**
* query authorized project
*
* @param loginUser login user
* @param userId user id
* @return projects which the user have permission to see, Except for items created by this user
*/
@ApiOperation(value = "queryAuthorizedProject", notes = "QUERY_AUTHORIZED_PROJECT_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "USER_ID", dataType = "Int", example = "100", required = true)
})
@GetMapping(value = "/authed-project")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_AUTHORIZED_PROJECT)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectListResponse queryAuthorizedProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("userId") Integer userId) {
Result result = projectService.queryAuthorizedProject(loginUser, userId);
return new ProjectListResponse(result);
}
/**
* query authorized user
*
* @param loginUser login user
* @param projectCode project code
* @return users who have permission for the specified project
*/
@ApiOperation(value = "queryAuthorizedUser", notes = "QUERY_AUTHORIZED_USER_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "projectCode", value = "PROJECT_CODE", dataType = "Long", example = "100", required = true)
})
@GetMapping(value = "/authed-user")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_AUTHORIZED_USER)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public UserListResponse queryAuthorizedUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectCode") Long projectCode) {
Result result = projectService.queryAuthorizedUser(loginUser, projectCode);
return new UserListResponse(result);
}
/**
* query authorized and user created project
*
* @param loginUser login user
* @return projects which the user create and authorized
*/
@ApiOperation(value = "queryProjectCreatedAndAuthorizedByUser", notes = "QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "loginUser", value = "LOGIN_USER", dataType = "Object", example = "\"{id:100}\"", required = true)
})
@GetMapping(value = "/created-and-authed")
@ResponseStatus(HttpStatus.OK)
@ApiException(QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectListResponse queryProjectCreatedAndAuthorizedByUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
Result result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser);
return new ProjectListResponse(result);
}
/**
* query all project list
*
* @param loginUser login user
* @return all project list
*/
@ApiOperation(value = "queryAllProjectList", notes = "QUERY_ALL_PROJECT_LIST_NOTES")
@ApiImplicitParams({
@ApiImplicitParam(name = "loginUser", value = "LOGIN_USER", dataType = "Object", example = "\"{id:100}\"", required = true)
})
@GetMapping(value = "/list")
@ResponseStatus(HttpStatus.OK)
@ApiException(LOGIN_USER_QUERY_PROJECT_LIST_PAGING_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public ProjectListResponse queryAllProjectList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
Result result = projectService.queryAllProjectList(loginUser);
return new ProjectListResponse(result);
}
}

36
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/PageQueryDto.java

@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* page query dto
*/
@ApiModel("QUERY-PAGE-INFO")
@Data
public class PageQueryDto {
@ApiModelProperty(example = "10", required = true)
private Integer pageSize;
@ApiModelProperty(example = "1", required = true)
private Integer pageNo;
}

34
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectCreateRequest.java

@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* project create request
*/
@Data
public class ProjectCreateRequest {
@ApiModelProperty(example = "pro123", required = true)
private String projectName;
@ApiModelProperty(example = "this is a project")
private String description;
}

39
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectCreateResponse.java

@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.Project;
import lombok.Data;
/**
* project create response
*/
@Data
public class ProjectCreateResponse extends Result {
private Project data;
public ProjectCreateResponse(Result result) {
super();
this.setCode(result.getCode());
this.setMsg(result.getMsg());
this.setData((Project) result.getData());
}
}

38
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectDeleteResponse.java

@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import org.apache.dolphinscheduler.api.utils.Result;
import lombok.Data;
/**
* project delete response
*/
@Data
public class ProjectDeleteResponse extends Result {
private Boolean data;
public ProjectDeleteResponse(Result result) {
super();
this.setCode(result.getCode());
this.setMsg(result.getMsg());
this.setData((Boolean) result.getData());
}
}

40
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectListPagingResponse.java

@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.Project;
import lombok.Data;
/**
* project List paging response
*/
@Data
public class ProjectListPagingResponse extends Result {
private PageInfo<Project> data;
public ProjectListPagingResponse(Result result) {
super();
this.setCode(result.getCode());
this.setMsg(result.getMsg());
this.setData(result.getData());
}
}

42
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectListResponse.java

@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.Project;
import java.util.List;
import lombok.Data;
/**
* project List response
*/
@Data
public class ProjectListResponse extends Result {
private List<Project> data;
public ProjectListResponse(Result result) {
super();
this.setCode(result.getCode());
this.setMsg(result.getMsg());
this.setData(JSONUtils.toList(JSONUtils.toJsonString(result.getData()), Project.class));
}
}

40
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectQueryRequest.java

@ -0,0 +1,40 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import org.apache.dolphinscheduler.api.dto.PageQueryDto;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* project query request
*/
@ApiModel("PROJECT-QUERY")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ProjectQueryRequest extends PageQueryDto {
@ApiModelProperty(example = "pro123")
private String searchVal;
}

39
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectQueryResponse.java

@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.Project;
import lombok.Data;
/**
* project query response
*/
@Data
public class ProjectQueryResponse extends Result {
private Project data;
public ProjectQueryResponse(Result result) {
super();
this.setCode(result.getCode());
this.setMsg(result.getMsg());
this.setData((Project) result.getData());
}
}

42
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectUpdateRequest.java

@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* project update request
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class ProjectUpdateRequest {
@ApiModelProperty(example = "admin", required = true)
private String userName;
@ApiModelProperty(example = "pro123", required = true)
private String projectName;
@ApiModelProperty(example = "this is a project")
private String description;
}

39
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/project/ProjectUpdateResponse.java

@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.project;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.Project;
import lombok.Data;
/**
* project update response
*/
@Data
public class ProjectUpdateResponse extends Result {
private Project data;
public ProjectUpdateResponse(Result result) {
super();
this.setCode(result.getCode());
this.setMsg(result.getMsg());
this.setData((Project) result.getData());
}
}

42
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/dto/user/UserListResponse.java

@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.dto.user;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.utils.JSONUtils;
import org.apache.dolphinscheduler.dao.entity.User;
import java.util.List;
import lombok.Data;
/**
* user List response
*/
@Data
public class UserListResponse extends Result {
private List<User> data;
public UserListResponse(Result result) {
super();
this.setCode(result.getCode());
this.setMsg(result.getMsg());
this.setData(JSONUtils.toList(JSONUtils.toJsonString(result.getData()), User.class));
}
}

41
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProjectService.java

@ -36,7 +36,7 @@ public interface ProjectService {
* @param desc description * @param desc description
* @return returns an error if it exists * @return returns an error if it exists
*/ */
Map<String, Object> createProject(User loginUser, String name, String desc); Result createProject(User loginUser, String name, String desc);
/** /**
* query project details by code * query project details by code
@ -44,7 +44,7 @@ public interface ProjectService {
* @param projectCode project code * @param projectCode project code
* @return project detail information * @return project detail information
*/ */
Map<String, Object> queryByCode(User loginUser, long projectCode); Result queryByCode(User loginUser, long projectCode);
/** /**
* query project details by name * query project details by name
@ -68,7 +68,16 @@ public interface ProjectService {
boolean hasProjectAndPerm(User loginUser, Project project, Map<String, Object> result,String perm); boolean hasProjectAndPerm(User loginUser, Project project, Map<String, Object> result,String perm);
boolean hasProjectAndPerm(User loginUser, Project project, Result result); /**
* has project and permission
*
* @param loginUser login user
* @param project project
* @param result result
* @param permission String
* @return true if the login user have permission to the project
*/
boolean hasProjectAndPerm(User loginUser, Project project, Result result, String permission);
/** /**
* admin can view all projects * admin can view all projects
@ -88,7 +97,7 @@ public interface ProjectService {
* @param projectCode project code * @param projectCode project code
* @return delete result code * @return delete result code
*/ */
Map<String, Object> deleteProject(User loginUser, Long projectCode); Result deleteProject(User loginUser, Long projectCode);
/** /**
* updateProcessInstance project * updateProcessInstance project
@ -100,7 +109,7 @@ public interface ProjectService {
* @param userName project owner * @param userName project owner
* @return update result code * @return update result code
*/ */
Map<String, Object> update(User loginUser, Long projectCode, String projectName, String desc, String userName); Result update(User loginUser, Long projectCode, String projectName, String desc, String userName);
/** /**
* query unauthorized project * query unauthorized project
@ -109,7 +118,7 @@ public interface ProjectService {
* @param userId user id * @param userId user id
* @return the projects which user have not permission to see * @return the projects which user have not permission to see
*/ */
Map<String, Object> queryUnauthorizedProject(User loginUser, Integer userId); Result queryUnauthorizedProject(User loginUser, Integer userId);
/** /**
* query authorized project * query authorized project
@ -118,7 +127,7 @@ public interface ProjectService {
* @param userId user id * @param userId user id
* @return projects which the user have permission to see, Except for items created by this user * @return projects which the user have permission to see, Except for items created by this user
*/ */
Map<String, Object> queryAuthorizedProject(User loginUser, Integer userId); Result queryAuthorizedProject(User loginUser, Integer userId);
/** /**
* query authorized user * query authorized user
@ -127,7 +136,7 @@ public interface ProjectService {
* @param projectCode project code * @param projectCode project code
* @return users who have permission for the specified project * @return users who have permission for the specified project
*/ */
Map<String, Object> queryAuthorizedUser(User loginUser, Long projectCode); Result queryAuthorizedUser(User loginUser, Long projectCode);
/** /**
* query authorized project * query authorized project
@ -142,13 +151,25 @@ public interface ProjectService {
* @param loginUser * @param loginUser
* @return project list * @return project list
*/ */
Map<String, Object> queryAllProjectList(User loginUser); Result queryAllProjectList(User loginUser);
/** /**
* query authorized and user create project list by user id * query authorized and user create project list by user id
* @param loginUser login user * @param loginUser login user
* @return project list * @return project list
*/ */
Map<String, Object> queryProjectCreatedAndAuthorizedByUser(User loginUser); Result queryProjectCreatedAndAuthorizedByUser(User loginUser);
/**
* check project and authorization
*
* @param result result
* @param loginUser login user
* @param project project
* @param projectCode project code
* @param perm String
* @return true if the login user have permission to see the project
*/
void checkProjectAndAuth(Result result, User loginUser, Project project, long projectCode, String perm);
} }

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

@ -17,9 +17,13 @@
package org.apache.dolphinscheduler.api.service.impl; package org.apache.dolphinscheduler.api.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT_CREATE;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT_DELETE;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT_UPDATE;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService;
import org.apache.dolphinscheduler.api.service.ProjectService; import org.apache.dolphinscheduler.api.service.ProjectService;
import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
@ -36,13 +40,10 @@ import org.apache.dolphinscheduler.dao.mapper.ProcessDefinitionMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectUserMapper;
import org.apache.dolphinscheduler.dao.mapper.UserMapper; import org.apache.dolphinscheduler.dao.mapper.UserMapper;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.commons.lang3.StringUtils;
import java.text.MessageFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
@ -52,8 +53,14 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.*; import org.slf4j.Logger;
import static org.apache.dolphinscheduler.api.utils.CheckUtils.checkDesc; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/** /**
* project service impl * project service impl
@ -88,10 +95,11 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
*/ */
@Override @Override
@Transactional @Transactional
public Map<String, Object> createProject(User loginUser, String name, String desc) { public Result createProject(User loginUser, String name, String desc) {
Result result = new Result();
Map<String, Object> result = checkDesc(desc); checkDesc(result, desc);
if (result.get(Constants.STATUS) != Status.SUCCESS) { if (result.getCode() != Status.SUCCESS.getCode()) {
return result; return result;
} }
if (!canOperatorPermissions(loginUser, null,AuthorizationType.PROJECTS, PROJECT_CREATE)) { if (!canOperatorPermissions(loginUser, null,AuthorizationType.PROJECTS, PROJECT_CREATE)) {
@ -124,7 +132,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
} }
if (projectMapper.insert(project) > 0) { if (projectMapper.insert(project) > 0) {
result.put(Constants.DATA_LIST, project); result.setData(project);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
permissionPostHandle(AuthorizationType.PROJECTS, loginUser.getId(), Collections.singletonList(project.getId()), logger); permissionPostHandle(AuthorizationType.PROJECTS, loginUser.getId(), Collections.singletonList(project.getId()), logger);
} else { } else {
@ -134,6 +142,21 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
return result; return result;
} }
/**
* check project description
*
* @param result
* @param desc desc
*/
public static void checkDesc(Result result, String desc) {
if (!StringUtils.isEmpty(desc) && desc.length() > 200) {
result.setCode(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode());
result.setMsg(MessageFormat.format(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getMsg(), "desc length"));
} else {
result.setCode(Status.SUCCESS.getCode());
}
}
/** /**
* query project details by code * query project details by code
* *
@ -141,15 +164,15 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return project detail information * @return project detail information
*/ */
@Override @Override
public Map<String, Object> queryByCode(User loginUser, long projectCode) { public Result queryByCode(User loginUser, long projectCode) {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
Project project = projectMapper.queryByCode(projectCode); Project project = projectMapper.queryByCode(projectCode);
boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result, PROJECT); boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result, PROJECT);
if (!hasProjectAndPerm) { if (!hasProjectAndPerm) {
return result; return result;
} }
if (project != null) { if (project != null) {
result.put(Constants.DATA_LIST, project); result.setData(project);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
} }
return result; return result;
@ -179,11 +202,11 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return true if the login user have permission to see the project * @return true if the login user have permission to see the project
*/ */
@Override @Override
public Map<String, Object> checkProjectAndAuth(User loginUser, Project project, long projectCode,String perm) { public Map<String, Object> checkProjectAndAuth(User loginUser, Project project, long projectCode, String permission) {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
if (project == null) { if (project == null) {
putMsg(result, Status.PROJECT_NOT_EXIST); putMsg(result, Status.PROJECT_NOT_EXIST);
} else if (!canOperatorPermissions(loginUser, new Object[]{project.getId()},AuthorizationType.PROJECTS,perm)) { } else if (!canOperatorPermissions(loginUser, new Object[] {project.getId()}, AuthorizationType.PROJECTS, permission)) {
// check read permission // check read permission
putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), projectCode); putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), projectCode);
} else { } else {
@ -193,11 +216,11 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
} }
@Override @Override
public boolean hasProjectAndPerm(User loginUser, Project project, Map<String, Object> result,String perm) { public boolean hasProjectAndPerm(User loginUser, Project project, Map<String, Object> result, String permission) {
boolean checkResult = false; boolean checkResult = false;
if (project == null) { if (project == null) {
putMsg(result, Status.PROJECT_NOT_FOUND, ""); putMsg(result, Status.PROJECT_NOT_FOUND, "");
} else if (!canOperatorPermissions(loginUser, new Object[]{project.getId()},AuthorizationType.PROJECTS,perm)) { } else if (!canOperatorPermissions(loginUser, new Object[] {project.getId()}, AuthorizationType.PROJECTS, permission)) {
putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), project.getCode()); putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), project.getCode());
} else { } else {
checkResult = true; checkResult = true;
@ -206,11 +229,11 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
} }
@Override @Override
public boolean hasProjectAndPerm(User loginUser, Project project, Result result) { public boolean hasProjectAndPerm(User loginUser, Project project, Result result, String permission) {
boolean checkResult = false; boolean checkResult = false;
if (project == null) { if (project == null) {
putMsg(result, Status.PROJECT_NOT_FOUND, ""); putMsg(result, Status.PROJECT_NOT_FOUND, "");
} else if (!canOperatorPermissions(loginUser, new Object[]{project.getId()},AuthorizationType.PROJECTS,PROJECT)) { } else if (!canOperatorPermissions(loginUser, new Object[] {project.getId()}, AuthorizationType.PROJECTS, permission)) {
putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), project.getName()); putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), project.getName());
} else { } else {
checkResult = true; checkResult = true;
@ -261,13 +284,13 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return delete result code * @return delete result code
*/ */
@Override @Override
public Map<String, Object> deleteProject(User loginUser, Long projectCode) { public Result deleteProject(User loginUser, Long projectCode) {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
Project project = projectMapper.queryByCode(projectCode); Project project = projectMapper.queryByCode(projectCode);
Map<String, Object> checkResult = getCheckResult(loginUser, project,PROJECT_DELETE); checkProjectAndAuth(result, loginUser, project, project == null ? 0L : project.getCode(), PROJECT_DELETE);
if (checkResult != null) { if (result.getCode() != Status.SUCCESS.getCode()) {
return checkResult; return result;
} }
List<ProcessDefinition> processDefinitionList = processDefinitionMapper.queryAllDefinitionList(project.getCode()); List<ProcessDefinition> processDefinitionList = processDefinitionMapper.queryAllDefinitionList(project.getCode());
@ -278,6 +301,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
} }
int delete = projectMapper.deleteById(project.getId()); int delete = projectMapper.deleteById(project.getId());
if (delete > 0) { if (delete > 0) {
result.setData(Boolean.TRUE);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
} else { } else {
putMsg(result, Status.DELETE_PROJECT_ERROR); putMsg(result, Status.DELETE_PROJECT_ERROR);
@ -312,12 +336,12 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return update result code * @return update result code
*/ */
@Override @Override
public Map<String, Object> update(User loginUser, Long projectCode, String projectName, String desc, String userName) { public Result update(User loginUser, Long projectCode, String projectName, String desc, String userName) {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
Map<String, Object> descCheck = checkDesc(desc); checkDesc(result, desc);
if (descCheck.get(Constants.STATUS) != Status.SUCCESS) { if (result.getCode() != Status.SUCCESS.getCode()) {
return descCheck; return result;
} }
Project project = projectMapper.queryByCode(projectCode); Project project = projectMapper.queryByCode(projectCode);
@ -341,6 +365,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
project.setUserId(user.getId()); project.setUserId(user.getId());
int update = projectMapper.updateById(project); int update = projectMapper.updateById(project);
if (update > 0) { if (update > 0) {
result.setData(project);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
} else { } else {
putMsg(result, Status.UPDATE_PROJECT_ERROR); putMsg(result, Status.UPDATE_PROJECT_ERROR);
@ -357,12 +382,12 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return the projects which user have not permission to see * @return the projects which user have not permission to see
*/ */
@Override @Override
public Map<String, Object> queryUnauthorizedProject(User loginUser, Integer userId) { public Result queryUnauthorizedProject(User loginUser, Integer userId) {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
Set<Integer> projectIds = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), logger); Set<Integer> projectIds = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), logger);
if (projectIds.isEmpty()) { if (projectIds.isEmpty()) {
result.put(Constants.DATA_LIST, Collections.emptyList()); result.setData(Collections.emptyList());
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result; return result;
} }
@ -377,7 +402,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
resultList = getUnauthorizedProjects(projectSet, authedProjectList); resultList = getUnauthorizedProjects(projectSet, authedProjectList);
} }
result.put(Constants.DATA_LIST, resultList); result.setData(resultList);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result; return result;
} }
@ -409,11 +434,11 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return projects which the user have permission to see, Except for items created by this user * @return projects which the user have permission to see, Except for items created by this user
*/ */
@Override @Override
public Map<String, Object> queryAuthorizedProject(User loginUser, Integer userId) { public Result queryAuthorizedProject(User loginUser, Integer userId) {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
List<Project> projects = projectMapper.queryAuthedProjectListByUserId(userId); List<Project> projects = projectMapper.queryAuthedProjectListByUserId(userId);
result.put(Constants.DATA_LIST, projects); result.setData(projects);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result; return result;
@ -427,8 +452,8 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return users who have permission for the specified project * @return users who have permission for the specified project
*/ */
@Override @Override
public Map<String, Object> queryAuthorizedUser(User loginUser, Long projectCode) { public Result queryAuthorizedUser(User loginUser, Long projectCode) {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
// 1. check read permission // 1. check read permission
Project project = this.projectMapper.queryByCode(projectCode); Project project = this.projectMapper.queryByCode(projectCode);
@ -439,7 +464,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
// 2. query authorized user list // 2. query authorized user list
List<User> users = this.userMapper.queryAuthedUserListByProjectId(project.getId()); List<User> users = this.userMapper.queryAuthedUserListByProjectId(project.getId());
result.put(Constants.DATA_LIST, users); result.setData(users);
this.putMsg(result, Status.SUCCESS); this.putMsg(result, Status.SUCCESS);
return result; return result;
} }
@ -468,18 +493,18 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return project list * @return project list
*/ */
@Override @Override
public Map<String, Object> queryProjectCreatedAndAuthorizedByUser(User loginUser) { public Result queryProjectCreatedAndAuthorizedByUser(User loginUser) {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
Set<Integer> projectIds = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), logger); Set<Integer> projectIds = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), logger);
if (projectIds.isEmpty()) { if (projectIds.isEmpty()) {
result.put(Constants.DATA_LIST, Collections.emptyList()); result.setData(Collections.emptyList());
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result; return result;
} }
List<Project> projects = projectMapper.selectBatchIds(projectIds); List<Project> projects = projectMapper.selectBatchIds(projectIds);
result.put(Constants.DATA_LIST, projects); result.setData(projects);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result; return result;
@ -541,13 +566,34 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return project list * @return project list
*/ */
@Override @Override
public Map<String, Object> queryAllProjectList(User user) { public Result queryAllProjectList(User user) {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
List<Project> projects = projectMapper.queryAllProject(user.getUserType() == UserType.ADMIN_USER ? 0 : user.getId()); List<Project> projects = projectMapper.queryAllProject(user.getUserType() == UserType.ADMIN_USER ? 0 : user.getId());
result.put(Constants.DATA_LIST, projects); result.setData(projects);
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
return result; return result;
} }
/**
* check project and authorization
*
* @param result result
* @param loginUser login user
* @param project project
* @param projectCode project code
* @return true if the login user have permission to see the project
*/
@Override
public void checkProjectAndAuth(Result result, User loginUser, Project project, long projectCode, String permission) {
if (project == null) {
putMsg(result, Status.PROJECT_NOT_EXIST);
} else if (!canOperatorPermissions(loginUser, new Object[] {project.getId()}, AuthorizationType.PROJECTS, permission)) {
// check read permission
putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), projectCode);
} else {
putMsg(result, Status.SUCCESS);
}
}
} }

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

@ -17,6 +17,8 @@
package org.apache.dolphinscheduler.api.service.impl; package org.apache.dolphinscheduler.api.service.impl;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT;
import org.apache.dolphinscheduler.api.dto.ScheduleParam; import org.apache.dolphinscheduler.api.dto.ScheduleParam;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.exceptions.ServiceException;
@ -401,7 +403,7 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
Project project = projectMapper.queryByCode(projectCode); Project project = projectMapper.queryByCode(projectCode);
// check project auth // check project auth
boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result); boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result, PROJECT);
if (!hasProjectAndPerm) { if (!hasProjectAndPerm) {
return result; return result;
} }

28
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProjectControllerTest.java

@ -21,7 +21,6 @@ import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl;
import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.Project; import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.dao.entity.Resource; import org.apache.dolphinscheduler.dao.entity.Resource;
@ -29,8 +28,6 @@ import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
@ -47,6 +44,8 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.Silent.class) @RunWith(MockitoJUnitRunner.Silent.class)
public class ProjectControllerTest { public class ProjectControllerTest {
protected User user;
@InjectMocks @InjectMocks
private ProjectController projectController; private ProjectController projectController;
@ -56,8 +55,6 @@ public class ProjectControllerTest {
@Mock @Mock
private ProjectMapper projectMapper; private ProjectMapper projectMapper;
protected User user;
@Before @Before
public void before() { public void before() {
User loginUser = new User(); User loginUser = new User();
@ -69,9 +66,8 @@ public class ProjectControllerTest {
@Test @Test
public void testUpdateProject() { public void testUpdateProject() {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
result.put("projectId", 1);
long projectCode = 1L; long projectCode = 1L;
String projectName = "test"; String projectName = "test";
@ -84,7 +80,7 @@ public class ProjectControllerTest {
@Test @Test
public void testQueryProjectByCode() { public void testQueryProjectByCode() {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
long projectCode = 1L; long projectCode = 1L;
Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject()); Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject());
@ -109,7 +105,7 @@ public class ProjectControllerTest {
@Test @Test
public void testQueryUnauthorizedProject() { public void testQueryUnauthorizedProject() {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
Mockito.when(projectService.queryUnauthorizedProject(user, 2)).thenReturn(result); Mockito.when(projectService.queryUnauthorizedProject(user, 2)).thenReturn(result);
Result response = projectController.queryUnauthorizedProject(user, 2); Result response = projectController.queryUnauthorizedProject(user, 2);
@ -118,7 +114,7 @@ public class ProjectControllerTest {
@Test @Test
public void testQueryAuthorizedProject() { public void testQueryAuthorizedProject() {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
Mockito.when(projectService.queryAuthorizedProject(user, 2)).thenReturn(result); Mockito.when(projectService.queryAuthorizedProject(user, 2)).thenReturn(result);
Result response = projectController.queryAuthorizedProject(user, 2); Result response = projectController.queryAuthorizedProject(user, 2);
@ -127,7 +123,7 @@ public class ProjectControllerTest {
@Test @Test
public void testQueryAuthorizedUser() { public void testQueryAuthorizedUser() {
Map<String, Object> result = new HashMap<>(); Result result = new Result();
this.putMsg(result, Status.SUCCESS); this.putMsg(result, Status.SUCCESS);
Mockito.when(this.projectService.queryAuthorizedUser(this.user, 3682329499136L)).thenReturn(result); Mockito.when(this.projectService.queryAuthorizedUser(this.user, 3682329499136L)).thenReturn(result);
@ -139,7 +135,7 @@ public class ProjectControllerTest {
public void testQueryAllProjectList() { public void testQueryAllProjectList() {
User user = new User(); User user = new User();
user.setId(0); user.setId(0);
Map<String, Object> result = new HashMap<>(); Result result = new Result();
putMsg(result, Status.SUCCESS); putMsg(result, Status.SUCCESS);
Mockito.when(projectService.queryAllProjectList(user)).thenReturn(result); Mockito.when(projectService.queryAllProjectList(user)).thenReturn(result);
Result response = projectController.queryAllProjectList(user); Result response = projectController.queryAllProjectList(user);
@ -155,12 +151,12 @@ public class ProjectControllerTest {
return project; return project;
} }
private void putMsg(Map<String, Object> result, Status status, Object... statusParams) { private void putMsg(Result result, Status status, Object... statusParams) {
result.put(Constants.STATUS, status); result.setCode(status.getCode());
if (statusParams != null && statusParams.length > 0) { if (statusParams != null && statusParams.length > 0) {
result.put(Constants.MSG, MessageFormat.format(status.getMsg(), statusParams)); result.setMsg(MessageFormat.format(status.getMsg(), statusParams));
} else { } else {
result.put(Constants.MSG, status.getMsg()); result.setMsg(status.getMsg());
} }
} }
} }

165
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProjectV2ControllerTest.java

@ -0,0 +1,165 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.api.controller;
import org.apache.dolphinscheduler.api.dto.project.ProjectListResponse;
import org.apache.dolphinscheduler.api.dto.project.ProjectQueryRequest;
import org.apache.dolphinscheduler.api.dto.project.ProjectQueryResponse;
import org.apache.dolphinscheduler.api.dto.project.ProjectUpdateRequest;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.Project;
import org.apache.dolphinscheduler.dao.entity.Resource;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.ProjectMapper;
import java.text.MessageFormat;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
/**
* project v2 controller test
*/
@RunWith(MockitoJUnitRunner.Silent.class)
public class ProjectV2ControllerTest {
protected User user;
@InjectMocks
private ProjectV2Controller projectV2Controller;
@Mock
private ProjectServiceImpl projectService;
@Mock
private ProjectMapper projectMapper;
@Before
public void before() {
User loginUser = new User();
loginUser.setId(1);
loginUser.setUserType(UserType.GENERAL_USER);
loginUser.setUserName("admin");
user = loginUser;
}
@Test
public void testUpdateProject() {
Result result = new Result();
putMsg(result, Status.SUCCESS);
long projectCode = 1L;
ProjectUpdateRequest projectUpdateReq = new ProjectUpdateRequest();
projectUpdateReq.setProjectName("james");
projectUpdateReq.setDescription("james lbj");
projectUpdateReq.setUserName("admin");
Mockito.when(projectService.update(user, projectCode, projectUpdateReq.getProjectName(),
projectUpdateReq.getDescription(), projectUpdateReq.getUserName())).thenReturn(result);
Result response = projectV2Controller.updateProject(user, projectCode, projectUpdateReq);
Assert.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue());
}
@Test
public void testQueryProjectByCode() {
Result result = new Result();
putMsg(result, Status.SUCCESS);
long projectCode = 1L;
Mockito.when(projectMapper.queryByCode(projectCode)).thenReturn(getProject());
Mockito.when(projectService.queryByCode(user, projectCode)).thenReturn(result);
ProjectQueryResponse response = projectV2Controller.queryProjectByCode(user, projectCode);
Assert.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue());
}
@Test
public void testQueryProjectListPaging() {
ProjectQueryRequest projectQueryReq = new ProjectQueryRequest();
projectQueryReq.setSearchVal("james");
projectQueryReq.setPageNo(1);
projectQueryReq.setPageSize(10);
Result result = Result.success(new PageInfo<Resource>(1, 10));
Mockito.when(projectService.queryProjectListPaging(user, projectQueryReq.getPageSize(),
projectQueryReq.getPageNo(), projectQueryReq.getSearchVal())).thenReturn(result);
Result response = projectV2Controller.queryProjectListPaging(user, projectQueryReq);
Assert.assertTrue(response != null && response.isSuccess());
}
@Test
public void testQueryUnauthorizedProject() {
Result result = new Result();
putMsg(result, Status.SUCCESS);
Mockito.when(projectService.queryUnauthorizedProject(user, 2)).thenReturn(result);
ProjectListResponse response = projectV2Controller.queryUnauthorizedProject(user, 2);
Assert.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue());
}
@Test
public void testQueryAuthorizedProject() {
Result result = new Result();
putMsg(result, Status.SUCCESS);
Mockito.when(projectService.queryAuthorizedProject(user, 2)).thenReturn(result);
ProjectListResponse response = projectV2Controller.queryAuthorizedProject(user, 2);
Assert.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue());
}
@Test
public void testQueryAuthorizedUser() {
Result result = new Result();
this.putMsg(result, Status.SUCCESS);
Mockito.when(this.projectService.queryAuthorizedUser(this.user, 3682329499136L)).thenReturn(result);
Result response = this.projectV2Controller.queryAuthorizedUser(this.user, 3682329499136L);
Assert.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue());
}
@Test
public void testQueryAllProjectList() {
User user = new User();
user.setId(0);
Result result = new Result();
putMsg(result, Status.SUCCESS);
Mockito.when(projectService.queryAllProjectList(user)).thenReturn(result);
Result response = projectV2Controller.queryAllProjectList(user);
Assert.assertEquals(Status.SUCCESS.getCode(), response.getCode().intValue());
}
private Project getProject() {
Project project = new Project();
project.setCode(1L);
project.setId(1);
project.setName("test");
project.setUserId(1);
return project;
}
private void putMsg(Result result, Status status, Object... statusParams) {
result.setCode(status.getCode());
if (statusParams != null && statusParams.length > 0) {
result.setMsg(MessageFormat.format(status.getMsg(), statusParams));
} else {
result.setMsg(status.getMsg());
}
}
}

82
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProjectServiceTest.java

@ -15,12 +15,19 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.apache.dolphinscheduler.api.service; package org.apache.dolphinscheduler.api.service;
import java.util.*;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT_CREATE;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT_DELETE;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.PROJECT_UPDATE;
import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService;
import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl; import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl;
import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl; import org.apache.dolphinscheduler.api.service.impl.ProjectServiceImpl;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.AuthorizationType; import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.UserType; import org.apache.dolphinscheduler.common.enums.UserType;
@ -35,7 +42,14 @@ import org.apache.dolphinscheduler.dao.mapper.UserMapper;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.apache.dolphinscheduler.api.permission.ResourcePermissionCheckService; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -46,8 +60,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.*;
/** /**
* project service test * project service test
**/ **/
@ -86,21 +98,21 @@ public class ProjectServiceTest {
loginUser.setId(1); loginUser.setId(1);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, 1, PROJECT_CREATE, baseServiceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, 1, PROJECT_CREATE, baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, null, 1, baseServiceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, null, 1, baseServiceLogger)).thenReturn(true);
Map<String, Object> result = projectService.createProject(loginUser, projectName, getDesc()); Result result = projectService.createProject(loginUser, projectName, getDesc());
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR, result.get(Constants.STATUS)); Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR.getCode(), 10001);
//project name exist //project name exist
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject()); Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject());
result = projectService.createProject(loginUser, projectName, projectName); result = projectService.createProject(loginUser, projectName, projectName);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS, result.get(Constants.STATUS)); Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS.getCode(), result.getCode().intValue());
//success //success
Mockito.when(projectMapper.insert(Mockito.any(Project.class))).thenReturn(1); Mockito.when(projectMapper.insert(Mockito.any(Project.class))).thenReturn(1);
result = projectService.createProject(loginUser, "test", "test"); result = projectService.createProject(loginUser, "test", "test");
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
} }
@ -183,15 +195,15 @@ public class ProjectServiceTest {
Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject()); Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject());
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), PROJECT_DELETE, baseServiceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), PROJECT_DELETE, baseServiceLogger)).thenReturn(true);
//PROJECT_NOT_FOUNT //PROJECT_NOT_FOUNT
Map<String, Object> result = projectService.deleteProject(loginUser, 11L); Result result = projectService.deleteProject(loginUser, 11L);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.PROJECT_NOT_EXIST, result.get(Constants.STATUS)); Assert.assertTrue(Status.PROJECT_NOT_EXIST.getCode() == result.getCode());
loginUser.setId(2); loginUser.setId(2);
//USER_NO_OPERATION_PROJECT_PERM //USER_NO_OPERATION_PROJECT_PERM
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {1}, loginUser.getId(), baseServiceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {1}, loginUser.getId(), baseServiceLogger)).thenReturn(true);
result = projectService.deleteProject(loginUser, 1L); result = projectService.deleteProject(loginUser, 1L);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM, result.get(Constants.STATUS)); Assert.assertTrue(Status.USER_NO_OPERATION_PROJECT_PERM.getCode() == result.getCode());
//DELETE_PROJECT_ERROR_DEFINES_NOT_NULL //DELETE_PROJECT_ERROR_DEFINES_NOT_NULL
Mockito.when(processDefinitionMapper.queryAllDefinitionList(1L)).thenReturn(getProcessDefinitions()); Mockito.when(processDefinitionMapper.queryAllDefinitionList(1L)).thenReturn(getProcessDefinitions());
@ -200,14 +212,14 @@ public class ProjectServiceTest {
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {1}, 0, baseServiceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {1}, 0, baseServiceLogger)).thenReturn(true);
result = projectService.deleteProject(loginUser, 1L); result = projectService.deleteProject(loginUser, 1L);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL, result.get(Constants.STATUS)); Assert.assertTrue(Status.DELETE_PROJECT_ERROR_DEFINES_NOT_NULL.getCode() == result.getCode());
//success //success
Mockito.when(projectMapper.deleteById(1)).thenReturn(1); Mockito.when(projectMapper.deleteById(1)).thenReturn(1);
Mockito.when(processDefinitionMapper.queryAllDefinitionList(1L)).thenReturn(new ArrayList<>()); Mockito.when(processDefinitionMapper.queryAllDefinitionList(1L)).thenReturn(new ArrayList<>());
result = projectService.deleteProject(loginUser, 1L); result = projectService.deleteProject(loginUser, 1L);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); Assert.assertTrue(Status.SUCCESS.getCode() == result.getCode());
} }
@Test @Test
@ -221,18 +233,18 @@ public class ProjectServiceTest {
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project); Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project);
Mockito.when(projectMapper.queryByCode(2L)).thenReturn(getProject()); Mockito.when(projectMapper.queryByCode(2L)).thenReturn(getProject());
// PROJECT_NOT_FOUNT // PROJECT_NOT_FOUNT
Map<String, Object> result = projectService.update(loginUser, 1L, projectName, "desc", "testUser"); Result result = projectService.update(loginUser, 1L, projectName, "desc", "testUser");
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.PROJECT_NOT_FOUND, result.get(Constants.STATUS)); Assert.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode());
//PROJECT_ALREADY_EXISTS //PROJECT_ALREADY_EXISTS
result = projectService.update(loginUser, 2L, projectName, "desc", userName); result = projectService.update(loginUser, 2L, projectName, "desc", userName);
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.PROJECT_ALREADY_EXISTS, result.get(Constants.STATUS)); Assert.assertTrue(Status.PROJECT_ALREADY_EXISTS.getCode() == result.getCode());
Mockito.when(userMapper.queryByUserNameAccurately(Mockito.any())).thenReturn(null); Mockito.when(userMapper.queryByUserNameAccurately(Mockito.any())).thenReturn(null);
result = projectService.update(loginUser, 2L, "test", "desc", "testuser"); result = projectService.update(loginUser, 2L, "test", "desc", "testuser");
Assert.assertEquals(Status.USER_NOT_EXIST, result.get(Constants.STATUS)); Assert.assertTrue(Status.USER_NOT_EXIST.getCode() == result.getCode());
//success //success
Mockito.when(userMapper.queryByUserNameAccurately(Mockito.any())).thenReturn(new User()); Mockito.when(userMapper.queryByUserNameAccurately(Mockito.any())).thenReturn(new User());
@ -240,7 +252,7 @@ public class ProjectServiceTest {
Mockito.when(projectMapper.updateById(Mockito.any(Project.class))).thenReturn(1); Mockito.when(projectMapper.updateById(Mockito.any(Project.class))).thenReturn(1);
result = projectService.update(loginUser, 2L, "test", "desc", "testUser"); result = projectService.update(loginUser, 2L, "test", "desc", "testUser");
logger.info(result.toString()); logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS)); Assert.assertTrue(Status.SUCCESS.getCode() == result.getCode());
} }
@ -252,16 +264,16 @@ public class ProjectServiceTest {
// test admin user // test admin user
loginUser.setUserType(UserType.ADMIN_USER); loginUser.setUserType(UserType.ADMIN_USER);
Map<String, Object> result = projectService.queryAuthorizedProject(loginUser, 2); Result result = projectService.queryAuthorizedProject(loginUser, 2);
logger.info(result.toString()); logger.info(result.toString());
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); List<Project> projects = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); Assert.assertTrue(CollectionUtils.isNotEmpty(projects));
// test non-admin user // test non-admin user
loginUser.setUserType(UserType.GENERAL_USER); loginUser.setUserType(UserType.GENERAL_USER);
loginUser.setId(3); loginUser.setId(3);
result = projectService.queryAuthorizedProject(loginUser, 2); result = projectService.queryAuthorizedProject(loginUser, 2);
projects = (List<Project>) result.get(Constants.DATA_LIST); projects = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); Assert.assertTrue(CollectionUtils.isNotEmpty(projects));
} }
@ -270,16 +282,16 @@ public class ProjectServiceTest {
final User loginUser = this.getLoginUser(); final User loginUser = this.getLoginUser();
// Failure 1: PROJECT_NOT_FOUND // Failure 1: PROJECT_NOT_FOUND
Map<String, Object> result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L); Result result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L);
logger.info("FAILURE 1: {}", result.toString()); logger.info("FAILURE 1: {}", result.toString());
Assert.assertEquals(Status.PROJECT_NOT_FOUND, result.get(Constants.STATUS)); Assert.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode());
// Failure 2: USER_NO_OPERATION_PROJECT_PERM // Failure 2: USER_NO_OPERATION_PROJECT_PERM
loginUser.setId(100); loginUser.setId(100);
Mockito.when(this.projectMapper.queryByCode(Mockito.anyLong())).thenReturn(this.getProject()); Mockito.when(this.projectMapper.queryByCode(Mockito.anyLong())).thenReturn(this.getProject());
result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L); result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L);
logger.info("FAILURE 2: {}", result.toString()); logger.info("FAILURE 2: {}", result.toString());
Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM, result.get(Constants.STATUS)); Assert.assertTrue(Status.USER_NO_OPERATION_PROJECT_PERM.getCode() == result.getCode());
// SUCCESS // SUCCESS
loginUser.setUserType(UserType.ADMIN_USER); loginUser.setUserType(UserType.ADMIN_USER);
@ -288,7 +300,7 @@ public class ProjectServiceTest {
Mockito.when(this.userMapper.queryAuthedUserListByProjectId(1)).thenReturn(this.getUserList()); Mockito.when(this.userMapper.queryAuthedUserListByProjectId(1)).thenReturn(this.getUserList());
result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L); result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L);
logger.info("SUCCESS 1: {}", result.toString()); logger.info("SUCCESS 1: {}", result.toString());
List<User> users = (List<User>) result.get(Constants.DATA_LIST); List<User> users = (List<User>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(users)); Assert.assertTrue(CollectionUtils.isNotEmpty(users));
loginUser.setId(1); loginUser.setId(1);
@ -297,7 +309,7 @@ public class ProjectServiceTest {
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {1}, 1, baseServiceLogger)).thenReturn(true); Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {1}, 1, baseServiceLogger)).thenReturn(true);
result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L); result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L);
logger.info("SUCCESS 2: {}", result.toString()); logger.info("SUCCESS 2: {}", result.toString());
users = (List<User>) result.get(Constants.DATA_LIST); users = (List<User>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(users)); Assert.assertTrue(CollectionUtils.isNotEmpty(users));
} }
@ -320,7 +332,7 @@ public class ProjectServiceTest {
@Test @Test
public void testQueryProjectCreatedAndAuthorizedByUser() { public void testQueryProjectCreatedAndAuthorizedByUser() {
Map<String, Object> result = null; Result result;
User loginUser = getLoginUser(); User loginUser = getLoginUser();
Set<Integer> set = new HashSet(); Set<Integer> set = new HashSet();
set.add(1); set.add(1);
@ -330,7 +342,7 @@ public class ProjectServiceTest {
Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), projectLogger)).thenReturn(set); Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), projectLogger)).thenReturn(set);
Mockito.when(projectMapper.selectBatchIds(set)).thenReturn(getList()); Mockito.when(projectMapper.selectBatchIds(set)).thenReturn(getList());
result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser); result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser);
List<Project> notAdminUserResult = (List<Project>) result.get(Constants.DATA_LIST); List<Project> notAdminUserResult = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(notAdminUserResult)); Assert.assertTrue(CollectionUtils.isNotEmpty(notAdminUserResult));
//admin user //admin user
@ -338,7 +350,7 @@ public class ProjectServiceTest {
Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), projectLogger)).thenReturn(set); Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), projectLogger)).thenReturn(set);
Mockito.when(projectMapper.selectBatchIds(set)).thenReturn(getList()); Mockito.when(projectMapper.selectBatchIds(set)).thenReturn(getList());
result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser); result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser);
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); List<Project> projects = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); Assert.assertTrue(CollectionUtils.isNotEmpty(projects));
@ -350,9 +362,9 @@ public class ProjectServiceTest {
User user = new User(); User user = new User();
user.setId(0); user.setId(0);
Map<String, Object> result = projectService.queryAllProjectList(user); Result result = projectService.queryAllProjectList(user);
logger.info(result.toString()); logger.info(result.toString());
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); List<Project> projects = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); Assert.assertTrue(CollectionUtils.isNotEmpty(projects));
} }
@ -369,9 +381,9 @@ public class ProjectServiceTest {
list.add(1); list.add(1);
Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), projectLogger)).thenReturn(set); Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), projectLogger)).thenReturn(set);
Mockito.when(projectMapper.listAuthorizedProjects(loginUser.getUserType().equals(UserType.ADMIN_USER) ? 0 : loginUser.getId(), list)).thenReturn(getList()); Mockito.when(projectMapper.listAuthorizedProjects(loginUser.getUserType().equals(UserType.ADMIN_USER) ? 0 : loginUser.getId(), list)).thenReturn(getList());
Map<String, Object> result = projectService.queryUnauthorizedProject(loginUser, 2); Result result = projectService.queryUnauthorizedProject(loginUser, 2);
logger.info(result.toString()); logger.info(result.toString());
List<Project> projects = (List<Project>) result.get(Constants.DATA_LIST); List<Project> projects = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); Assert.assertTrue(CollectionUtils.isNotEmpty(projects));
// test non-admin user // test non-admin user
@ -381,7 +393,7 @@ public class ProjectServiceTest {
Mockito.when(projectMapper.listAuthorizedProjects(loginUser.getUserType().equals(UserType.ADMIN_USER) ? 0 : loginUser.getId(), list)).thenReturn(getList()); Mockito.when(projectMapper.listAuthorizedProjects(loginUser.getUserType().equals(UserType.ADMIN_USER) ? 0 : loginUser.getId(), list)).thenReturn(getList());
result = projectService.queryUnauthorizedProject(loginUser, 3); result = projectService.queryUnauthorizedProject(loginUser, 3);
logger.info(result.toString()); logger.info(result.toString());
projects = (List<Project>) result.get(Constants.DATA_LIST); projects = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(projects)); Assert.assertTrue(CollectionUtils.isNotEmpty(projects));
} }
@ -428,6 +440,7 @@ public class ProjectServiceTest {
/** /**
* Get general user * Get general user
*
* @return * @return
*/ */
private User getGeneralUser() { private User getGeneralUser() {
@ -440,6 +453,7 @@ public class ProjectServiceTest {
/** /**
* Get user list * Get user list
*
* @return * @return
*/ */
private List<User> getUserList() { private List<User> getUserList() {

Loading…
Cancel
Save