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

* [Feature][Api] Refactor org.apache.dolphinscheduler.api.controller.ProjectController
This commit is contained in:
xuhhui 2022-07-26 15:24:48 +08:00 committed by GitHub
parent 5ebd53893c
commit 8004e97b4b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 1123 additions and 169 deletions

View File

@ -35,8 +35,6 @@ import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.utils.ParameterUtils;
import org.apache.dolphinscheduler.dao.entity.User;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
@ -70,7 +68,7 @@ public class ProjectController extends BaseController {
/**
* create project
*
* @param loginUser login user
* @param loginUser login user
* @param projectName project name
* @param description description
* @return returns an error if it exists
@ -87,15 +85,14 @@ public class ProjectController extends BaseController {
public Result createProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectName") String projectName,
@RequestParam(value = "description", required = false) String description) {
Map<String, Object> result = projectService.createProject(loginUser, projectName, description);
return returnDataList(result);
return projectService.createProject(loginUser, projectName, description);
}
/**
* update project
*
* @param loginUser login user
* @param code project code
* @param loginUser login user
* @param code project code
* @param projectName project name
* @param description description
* @return update result code
@ -116,15 +113,14 @@ public class ProjectController extends BaseController {
@RequestParam("projectName") String projectName,
@RequestParam(value = "description", required = false) String description,
@RequestParam(value = "userName") String userName) {
Map<String, Object> result = projectService.update(loginUser, code, projectName, description, userName);
return returnDataList(result);
return projectService.update(loginUser, code, projectName, description, userName);
}
/**
* query project details by code
*
* @param loginUser login user
* @param code project code
* @param code project code
* @return project detail information
*/
@ApiOperation(value = "queryProjectByCode", notes = "QUERY_PROJECT_BY_ID_NOTES")
@ -137,8 +133,7 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryProjectByCode(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("code") long code) {
Map<String, Object> result = projectService.queryByCode(loginUser, code);
return returnDataList(result);
return projectService.queryByCode(loginUser, code);
}
/**
@ -146,8 +141,8 @@ public class ProjectController extends BaseController {
*
* @param loginUser login user
* @param searchVal search value
* @param pageSize page size
* @param pageNo page number
* @param pageSize page size
* @param pageNo page number
* @return project list which the login user have permission to see
*/
@ApiOperation(value = "queryProjectListPaging", notes = "QUERY_PROJECT_LIST_PAGING_NOTES")
@ -179,7 +174,7 @@ public class ProjectController extends BaseController {
* delete project by code
*
* @param loginUser login user
* @param code project code
* @param code project code
* @return delete result code
*/
@ApiOperation(value = "delete", notes = "DELETE_PROJECT_BY_ID_NOTES")
@ -192,15 +187,14 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result deleteProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@PathVariable("code") Long code) {
Map<String, Object> result = projectService.deleteProject(loginUser, code);
return returnDataList(result);
return projectService.deleteProject(loginUser, code);
}
/**
* query unauthorized project
*
* @param loginUser login user
* @param userId user id
* @param userId user id
* @return the projects which user have not permission to see
*/
@ApiOperation(value = "queryUnauthorizedProject", notes = "QUERY_UNAUTHORIZED_PROJECT_NOTES")
@ -213,15 +207,14 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryUnauthorizedProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("userId") Integer userId) {
Map<String, Object> result = projectService.queryUnauthorizedProject(loginUser, userId);
return returnDataList(result);
return projectService.queryUnauthorizedProject(loginUser, userId);
}
/**
* query authorized project
*
* @param loginUser login user
* @param userId user id
* @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")
@ -234,15 +227,14 @@ public class ProjectController extends BaseController {
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryAuthorizedProject(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("userId") Integer userId) {
Map<String, Object> result = projectService.queryAuthorizedProject(loginUser, userId);
return returnDataList(result);
return projectService.queryAuthorizedProject(loginUser, userId);
}
/**
* query authorized user
*
* @param loginUser login user
* @param projectCode project code
* @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")
@ -254,9 +246,8 @@ public class ProjectController extends BaseController {
@ApiException(QUERY_AUTHORIZED_USER)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryAuthorizedUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
@RequestParam("projectCode") Long projectCode) {
Map<String, Object> result = this.projectService.queryAuthorizedUser(loginUser, projectCode);
return this.returnDataList(result);
@RequestParam("projectCode") Long projectCode) {
return projectService.queryAuthorizedUser(loginUser, projectCode);
}
/**
@ -271,8 +262,7 @@ public class ProjectController extends BaseController {
@ApiException(QUERY_AUTHORIZED_AND_USER_CREATED_PROJECT_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryProjectCreatedAndAuthorizedByUser(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
Map<String, Object> result = projectService.queryProjectCreatedAndAuthorizedByUser(loginUser);
return returnDataList(result);
return projectService.queryProjectCreatedAndAuthorizedByUser(loginUser);
}
/**
@ -287,7 +277,6 @@ public class ProjectController extends BaseController {
@ApiException(LOGIN_USER_QUERY_PROJECT_LIST_PAGING_ERROR)
@AccessLogAnnotation(ignoreRequestArgs = "loginUser")
public Result queryAllProjectList(@ApiIgnore @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
Map<String, Object> result = projectService.queryAllProjectList(loginUser);
return returnDataList(result);
return projectService.queryAllProjectList(loginUser);
}
}

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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));
}
}

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -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));
}
}

View File

@ -36,7 +36,7 @@ public interface ProjectService {
* @param desc description
* @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
@ -44,7 +44,7 @@ public interface ProjectService {
* @param projectCode project code
* @return project detail information
*/
Map<String, Object> queryByCode(User loginUser, long projectCode);
Result queryByCode(User loginUser, long projectCode);
/**
* 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, 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
@ -88,7 +97,7 @@ public interface ProjectService {
* @param projectCode project code
* @return delete result code
*/
Map<String, Object> deleteProject(User loginUser, Long projectCode);
Result deleteProject(User loginUser, Long projectCode);
/**
* updateProcessInstance project
@ -100,7 +109,7 @@ public interface ProjectService {
* @param userName project owner
* @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
@ -109,7 +118,7 @@ public interface ProjectService {
* @param userId user id
* @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
@ -118,7 +127,7 @@ public interface ProjectService {
* @param userId user id
* @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
@ -127,7 +136,7 @@ public interface ProjectService {
* @param projectCode project code
* @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
@ -142,13 +151,25 @@ public interface ProjectService {
* @param loginUser
* @return project list
*/
Map<String, Object> queryAllProjectList(User loginUser);
Result queryAllProjectList(User loginUser);
/**
* query authorized and user create project list by user id
* @param loginUser login user
* @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);
}

View File

@ -17,9 +17,13 @@
package org.apache.dolphinscheduler.api.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.permission.ResourcePermissionCheckService;
import org.apache.dolphinscheduler.api.service.ProjectService;
import org.apache.dolphinscheduler.api.utils.PageInfo;
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.ProjectUserMapper;
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.Collections;
import java.util.Date;
@ -52,8 +53,14 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.*;
import static org.apache.dolphinscheduler.api.utils.CheckUtils.checkDesc;
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 com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
* project service impl
@ -88,10 +95,11 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
*/
@Override
@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);
if (result.get(Constants.STATUS) != Status.SUCCESS) {
checkDesc(result, desc);
if (result.getCode() != Status.SUCCESS.getCode()) {
return result;
}
if (!canOperatorPermissions(loginUser, null,AuthorizationType.PROJECTS, PROJECT_CREATE)) {
@ -124,7 +132,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
}
if (projectMapper.insert(project) > 0) {
result.put(Constants.DATA_LIST, project);
result.setData(project);
putMsg(result, Status.SUCCESS);
permissionPostHandle(AuthorizationType.PROJECTS, loginUser.getId(), Collections.singletonList(project.getId()), logger);
} else {
@ -134,6 +142,21 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
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
*
@ -141,15 +164,15 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return project detail information
*/
@Override
public Map<String, Object> queryByCode(User loginUser, long projectCode) {
Map<String, Object> result = new HashMap<>();
public Result queryByCode(User loginUser, long projectCode) {
Result result = new Result();
Project project = projectMapper.queryByCode(projectCode);
boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result,PROJECT);
boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result, PROJECT);
if (!hasProjectAndPerm) {
return result;
}
if (project != null) {
result.put(Constants.DATA_LIST, project);
result.setData(project);
putMsg(result, Status.SUCCESS);
}
return result;
@ -173,17 +196,17 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
/**
* check project and authorization
*
* @param loginUser login user
* @param project project
* @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 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<>();
if (project == null) {
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
putMsg(result, Status.USER_NO_OPERATION_PROJECT_PERM, loginUser.getUserName(), projectCode);
} else {
@ -193,11 +216,11 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
}
@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;
if (project == null) {
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());
} else {
checkResult = true;
@ -206,11 +229,11 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
}
@Override
public boolean hasProjectAndPerm(User loginUser, Project project, Result result) {
public boolean hasProjectAndPerm(User loginUser, Project project, Result result, String permission) {
boolean checkResult = false;
if (project == null) {
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());
} else {
checkResult = true;
@ -261,13 +284,13 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return delete result code
*/
@Override
public Map<String, Object> deleteProject(User loginUser, Long projectCode) {
Map<String, Object> result = new HashMap<>();
public Result deleteProject(User loginUser, Long projectCode) {
Result result = new Result();
Project project = projectMapper.queryByCode(projectCode);
Map<String, Object> checkResult = getCheckResult(loginUser, project,PROJECT_DELETE);
if (checkResult != null) {
return checkResult;
checkProjectAndAuth(result, loginUser, project, project == null ? 0L : project.getCode(), PROJECT_DELETE);
if (result.getCode() != Status.SUCCESS.getCode()) {
return result;
}
List<ProcessDefinition> processDefinitionList = processDefinitionMapper.queryAllDefinitionList(project.getCode());
@ -278,6 +301,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
}
int delete = projectMapper.deleteById(project.getId());
if (delete > 0) {
result.setData(Boolean.TRUE);
putMsg(result, Status.SUCCESS);
} else {
putMsg(result, Status.DELETE_PROJECT_ERROR);
@ -312,16 +336,16 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return update result code
*/
@Override
public Map<String, Object> update(User loginUser, Long projectCode, String projectName, String desc, String userName) {
Map<String, Object> result = new HashMap<>();
public Result update(User loginUser, Long projectCode, String projectName, String desc, String userName) {
Result result = new Result();
Map<String, Object> descCheck = checkDesc(desc);
if (descCheck.get(Constants.STATUS) != Status.SUCCESS) {
return descCheck;
checkDesc(result, desc);
if (result.getCode() != Status.SUCCESS.getCode()) {
return result;
}
Project project = projectMapper.queryByCode(projectCode);
boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result,PROJECT_UPDATE);
boolean hasProjectAndPerm = hasProjectAndPerm(loginUser, project, result, PROJECT_UPDATE);
if (!hasProjectAndPerm) {
return result;
}
@ -341,6 +365,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
project.setUserId(user.getId());
int update = projectMapper.updateById(project);
if (update > 0) {
result.setData(project);
putMsg(result, Status.SUCCESS);
} else {
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
*/
@Override
public Map<String, Object> queryUnauthorizedProject(User loginUser, Integer userId) {
Map<String, Object> result = new HashMap<>();
public Result queryUnauthorizedProject(User loginUser, Integer userId) {
Result result = new Result();
Set<Integer> projectIds = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), logger);
if (projectIds.isEmpty()) {
result.put(Constants.DATA_LIST, Collections.emptyList());
result.setData(Collections.emptyList());
putMsg(result, Status.SUCCESS);
return result;
}
@ -377,7 +402,7 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
resultList = getUnauthorizedProjects(projectSet, authedProjectList);
}
result.put(Constants.DATA_LIST, resultList);
result.setData(resultList);
putMsg(result, Status.SUCCESS);
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
*/
@Override
public Map<String, Object> queryAuthorizedProject(User loginUser, Integer userId) {
Map<String, Object> result = new HashMap<>();
public Result queryAuthorizedProject(User loginUser, Integer userId) {
Result result = new Result();
List<Project> projects = projectMapper.queryAuthedProjectListByUserId(userId);
result.put(Constants.DATA_LIST, projects);
result.setData(projects);
putMsg(result, Status.SUCCESS);
return result;
@ -427,19 +452,19 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return users who have permission for the specified project
*/
@Override
public Map<String, Object> queryAuthorizedUser(User loginUser, Long projectCode) {
Map<String, Object> result = new HashMap<>();
public Result queryAuthorizedUser(User loginUser, Long projectCode) {
Result result = new Result();
// 1. check read permission
Project project = this.projectMapper.queryByCode(projectCode);
boolean hasProjectAndPerm = this.hasProjectAndPerm(loginUser, project, result,PROJECT);
boolean hasProjectAndPerm = this.hasProjectAndPerm(loginUser, project, result, PROJECT);
if (!hasProjectAndPerm) {
return result;
}
// 2. query authorized user list
List<User> users = this.userMapper.queryAuthedUserListByProjectId(project.getId());
result.put(Constants.DATA_LIST, users);
result.setData(users);
this.putMsg(result, Status.SUCCESS);
return result;
}
@ -468,18 +493,18 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return project list
*/
@Override
public Map<String, Object> queryProjectCreatedAndAuthorizedByUser(User loginUser) {
Map<String, Object> result = new HashMap<>();
public Result queryProjectCreatedAndAuthorizedByUser(User loginUser) {
Result result = new Result();
Set<Integer> projectIds = resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), logger);
if (projectIds.isEmpty()) {
result.put(Constants.DATA_LIST, Collections.emptyList());
result.setData(Collections.emptyList());
putMsg(result, Status.SUCCESS);
return result;
}
List<Project> projects = projectMapper.selectBatchIds(projectIds);
result.put(Constants.DATA_LIST, projects);
result.setData(projects);
putMsg(result, Status.SUCCESS);
return result;
@ -541,13 +566,34 @@ public class ProjectServiceImpl extends BaseServiceImpl implements ProjectServic
* @return project list
*/
@Override
public Map<String, Object> queryAllProjectList(User user) {
Map<String, Object> result = new HashMap<>();
public Result queryAllProjectList(User user) {
Result result = new Result();
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);
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);
}
}
}

View File

@ -17,6 +17,8 @@
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.enums.Status;
import org.apache.dolphinscheduler.api.exceptions.ServiceException;
@ -401,7 +403,7 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
Project project = projectMapper.queryByCode(projectCode);
// check project auth
boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result);
boolean hasProjectAndPerm = projectService.hasProjectAndPerm(loginUser, project, result, PROJECT);
if (!hasProjectAndPerm) {
return result;
}

View File

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

View File

@ -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());
}
}
}

View File

@ -15,12 +15,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.permission.ResourcePermissionCheckService;
import org.apache.dolphinscheduler.api.service.impl.BaseServiceImpl;
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.enums.AuthorizationType;
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.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.Test;
import org.junit.runner.RunWith;
@ -46,8 +60,6 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.dolphinscheduler.api.constants.ApiFuncIdentificationConstant.*;
/**
* project service test
**/
@ -84,23 +96,23 @@ public class ProjectServiceTest {
User loginUser = getLoginUser();
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);
Map<String, Object> result = projectService.createProject(loginUser, projectName, getDesc());
Result result = projectService.createProject(loginUser, projectName, getDesc());
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
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(getProject());
result = projectService.createProject(loginUser, projectName, projectName);
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
Mockito.when(projectMapper.insert(Mockito.any(Project.class))).thenReturn(1);
result = projectService.createProject(loginUser, "test", "test");
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
}
@ -118,7 +130,7 @@ public class ProjectServiceTest {
Project project = getProject();
//USER_NO_OPERATION_PROJECT_PERM
project.setUserId(2);
result = projectService.checkProjectAndAuth(loginUser, project, projectCode,PROJECT);
result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT);
logger.info(result.toString());
Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM, result.get(Constants.STATUS));
@ -126,14 +138,14 @@ public class ProjectServiceTest {
project.setUserId(1);
loginUser.setUserType(UserType.ADMIN_USER);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, project.getUserId(), PROJECT, baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{project.getId()}, 0, baseServiceLogger)).thenReturn(true);
result = projectService.checkProjectAndAuth(loginUser, project, projectCode,PROJECT);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {project.getId()}, 0, baseServiceLogger)).thenReturn(true);
result = projectService.checkProjectAndAuth(loginUser, project, projectCode, PROJECT);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
Map<String, Object> result2 = new HashMap<>();
result2 = projectService.checkProjectAndAuth(loginUser, null, projectCode,PROJECT);
result2 = projectService.checkProjectAndAuth(loginUser, null, projectCode, PROJECT);
Assert.assertEquals(Status.PROJECT_NOT_EXIST, result2.get(Constants.STATUS));
Project project1 = getProject();
@ -141,12 +153,12 @@ public class ProjectServiceTest {
project1.setUserId(2);
loginUser.setUserType(UserType.GENERAL_USER);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), PROJECT, baseServiceLogger)).thenReturn(true);
result2 = projectService.checkProjectAndAuth(loginUser, project1, projectCode,PROJECT);
result2 = projectService.checkProjectAndAuth(loginUser, project1, projectCode, PROJECT);
Assert.assertEquals(Status.USER_NO_OPERATION_PROJECT_PERM, result2.get(Constants.STATUS));
//success
project1.setUserId(1);
projectService.checkProjectAndAuth(loginUser, project1, projectCode,PROJECT);
projectService.checkProjectAndAuth(loginUser, project1, projectCode, PROJECT);
}
@ -162,7 +174,7 @@ public class ProjectServiceTest {
tempUser.setId(Integer.MAX_VALUE);
tempUser.setUserType(UserType.GENERAL_USER);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, tempUser.getId(), null, baseServiceLogger)).thenReturn(true);
boolean checkResult = projectService.hasProjectAndPerm(tempUser, project, result,null);
boolean checkResult = projectService.hasProjectAndPerm(tempUser, project, result, null);
logger.info(result.toString());
Assert.assertFalse(checkResult);
@ -171,8 +183,8 @@ public class ProjectServiceTest {
project.setUserId(1);
loginUser.setUserType(UserType.ADMIN_USER);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), null, baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{project.getId()}, 0, baseServiceLogger)).thenReturn(true);
checkResult = projectService.hasProjectAndPerm(loginUser, project, result,null);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {project.getId()}, 0, baseServiceLogger)).thenReturn(true);
checkResult = projectService.hasProjectAndPerm(loginUser, project, result, null);
logger.info(result.toString());
Assert.assertTrue(checkResult);
}
@ -183,31 +195,31 @@ public class ProjectServiceTest {
Mockito.when(projectMapper.queryByCode(1L)).thenReturn(getProject());
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), PROJECT_DELETE, baseServiceLogger)).thenReturn(true);
//PROJECT_NOT_FOUNT
Map<String, Object> result = projectService.deleteProject(loginUser, 11L);
Result result = projectService.deleteProject(loginUser, 11L);
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);
//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);
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
Mockito.when(processDefinitionMapper.queryAllDefinitionList(1L)).thenReturn(getProcessDefinitions());
loginUser.setUserType(UserType.ADMIN_USER);
loginUser.setId(1);
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);
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
Mockito.when(projectMapper.deleteById(1)).thenReturn(1);
Mockito.when(processDefinitionMapper.queryAllDefinitionList(1L)).thenReturn(new ArrayList<>());
result = projectService.deleteProject(loginUser, 1L);
logger.info(result.toString());
Assert.assertEquals(Status.SUCCESS, result.get(Constants.STATUS));
Assert.assertTrue(Status.SUCCESS.getCode() == result.getCode());
}
@Test
@ -216,23 +228,23 @@ public class ProjectServiceTest {
User loginUser = getLoginUser();
Project project = getProject();
project.setCode(2L);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), PROJECT_UPDATE , baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[]{1},loginUser.getId() , baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), PROJECT_UPDATE, baseServiceLogger)).thenReturn(true);
Mockito.when(resourcePermissionCheckService.resourcePermissionCheck(AuthorizationType.PROJECTS, new Object[] {1}, loginUser.getId(), baseServiceLogger)).thenReturn(true);
Mockito.when(projectMapper.queryByName(projectName)).thenReturn(project);
Mockito.when(projectMapper.queryByCode(2L)).thenReturn(getProject());
// 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());
Assert.assertEquals(Status.PROJECT_NOT_FOUND, result.get(Constants.STATUS));
Assert.assertTrue(Status.PROJECT_NOT_FOUND.getCode() == result.getCode());
//PROJECT_ALREADY_EXISTS
result = projectService.update(loginUser, 2L, projectName, "desc", userName);
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);
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
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);
result = projectService.update(loginUser, 2L, "test", "desc", "testUser");
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
loginUser.setUserType(UserType.ADMIN_USER);
Map<String, Object> result = projectService.queryAuthorizedProject(loginUser, 2);
Result result = projectService.queryAuthorizedProject(loginUser, 2);
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));
// test non-admin user
loginUser.setUserType(UserType.GENERAL_USER);
loginUser.setId(3);
result = projectService.queryAuthorizedProject(loginUser, 2);
projects = (List<Project>) result.get(Constants.DATA_LIST);
projects = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(projects));
}
@ -270,34 +282,34 @@ public class ProjectServiceTest {
final User loginUser = this.getLoginUser();
// 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());
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
loginUser.setId(100);
Mockito.when(this.projectMapper.queryByCode(Mockito.anyLong())).thenReturn(this.getProject());
result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L);
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
loginUser.setUserType(UserType.ADMIN_USER);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), PROJECT, baseServiceLogger)).thenReturn(true);
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);
Mockito.when(this.userMapper.queryAuthedUserListByProjectId(1)).thenReturn(this.getUserList());
result = this.projectService.queryAuthorizedUser(loginUser, 3682329499136L);
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));
loginUser.setId(1);
loginUser.setUserType(UserType.GENERAL_USER);
Mockito.when(resourcePermissionCheckService.operationPermissionCheck(AuthorizationType.PROJECTS, loginUser.getId(), PROJECT, baseServiceLogger)).thenReturn(true);
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);
logger.info("SUCCESS 2: {}", result.toString());
users = (List<User>) result.get(Constants.DATA_LIST);
users = (List<User>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(users));
}
@ -320,7 +332,7 @@ public class ProjectServiceTest {
@Test
public void testQueryProjectCreatedAndAuthorizedByUser() {
Map<String, Object> result = null;
Result result;
User loginUser = getLoginUser();
Set<Integer> set = new HashSet();
set.add(1);
@ -330,7 +342,7 @@ public class ProjectServiceTest {
Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), projectLogger)).thenReturn(set);
Mockito.when(projectMapper.selectBatchIds(set)).thenReturn(getList());
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));
//admin user
@ -338,7 +350,7 @@ public class ProjectServiceTest {
Mockito.when(resourcePermissionCheckService.userOwnedResourceIdsAcquisition(AuthorizationType.PROJECTS, loginUser.getId(), projectLogger)).thenReturn(set);
Mockito.when(projectMapper.selectBatchIds(set)).thenReturn(getList());
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));
@ -350,9 +362,9 @@ public class ProjectServiceTest {
User user = new User();
user.setId(0);
Map<String, Object> result = projectService.queryAllProjectList(user);
Result result = projectService.queryAllProjectList(user);
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));
}
@ -368,20 +380,20 @@ public class ProjectServiceTest {
List<Integer> list = new ArrayList<>(1);
list.add(1);
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());
Map<String, Object> result = projectService.queryUnauthorizedProject(loginUser, 2);
Mockito.when(projectMapper.listAuthorizedProjects(loginUser.getUserType().equals(UserType.ADMIN_USER) ? 0 : loginUser.getId(), list)).thenReturn(getList());
Result result = projectService.queryUnauthorizedProject(loginUser, 2);
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));
// test non-admin user
loginUser.setId(2);
loginUser.setUserType(UserType.GENERAL_USER);
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());
result = projectService.queryUnauthorizedProject(loginUser, 3);
logger.info(result.toString());
projects = (List<Project>) result.get(Constants.DATA_LIST);
projects = (List<Project>) result.getData();
Assert.assertTrue(CollectionUtils.isNotEmpty(projects));
}
@ -428,6 +440,7 @@ public class ProjectServiceTest {
/**
* Get general user
*
* @return
*/
private User getGeneralUser() {
@ -440,6 +453,7 @@ public class ProjectServiceTest {
/**
* Get user list
*
* @return
*/
private List<User> getUserList() {
@ -472,9 +486,9 @@ public class ProjectServiceTest {
private String getDesc() {
return "projectUserMapper.deleteProjectRelation(projectId,userId)projectUserMappe"
+ ".deleteProjectRelation(projectId,userId)projectUserMappe"
+ "r.deleteProjectRelation(projectId,userId)projectUserMapper"
+ ".deleteProjectRelation(projectId,userId)projectUserMapper.deleteProjectRelation(projectId,userId)";
+ ".deleteProjectRelation(projectId,userId)projectUserMappe"
+ "r.deleteProjectRelation(projectId,userId)projectUserMapper"
+ ".deleteProjectRelation(projectId,userId)projectUserMapper.deleteProjectRelation(projectId,userId)";
}
}