diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebCredentialResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebCredentialResource.java index 93776ac748..828f6472a1 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebCredentialResource.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebCredentialResource.java @@ -29,6 +29,7 @@ import com.tencent.bk.job.common.model.Response; import com.tencent.bk.job.common.model.dto.AppResourceScope; import com.tencent.bk.job.manage.model.web.request.CredentialCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.vo.CredentialBasicVO; import com.tencent.bk.job.manage.model.web.vo.CredentialVO; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -90,6 +91,29 @@ Response> listCredentials( Integer pageSize ); + @ApiOperation(value = "分页获取凭据基础信息", produces = "application/json") + @GetMapping("/basicInfo/list") + Response> listCredentialBasicInfo( + @ApiParam("用户名,网关自动传入") + @RequestHeader("username") + String username, + @ApiIgnore + @RequestAttribute(value = "appResourceScope") + AppResourceScope appResourceScope, + @ApiParam(value = "资源范围类型", required = true) + @PathVariable(value = "scopeType") + String scopeType, + @ApiParam(value = "资源范围ID", required = true) + @PathVariable(value = "scopeId") + String scopeId, + @ApiParam("分页-开始,不传默认为0") + @RequestParam(value = "start", required = false) + Integer start, + @ApiParam("分页-每页大小,不传默认拉取全量数据") + @RequestParam(value = "pageSize", required = false) + Integer pageSize + ); + @ApiOperation(value = "新增凭据", produces = "application/json") @PostMapping diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/vo/CredentialBasicVO.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/vo/CredentialBasicVO.java new file mode 100644 index 0000000000..ffdb666ec6 --- /dev/null +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/vo/CredentialBasicVO.java @@ -0,0 +1,47 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.model.web.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@ApiModel("凭据") +@Data +public class CredentialBasicVO { + /** + * 主键Id + */ + @ApiModelProperty("主键Id") + private String id; + /** + * 名称 + */ + @ApiModelProperty("名称") + private String name; + + @ApiModelProperty("是否可以使用") + private Boolean canUse; +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebCredentialResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebCredentialResourceImpl.java index ade3b3f3e5..9a56411778 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebCredentialResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebCredentialResourceImpl.java @@ -36,6 +36,7 @@ import com.tencent.bk.job.manage.auth.TicketAuthService; import com.tencent.bk.job.manage.model.dto.CredentialDTO; import com.tencent.bk.job.manage.model.web.request.CredentialCreateUpdateReq; +import com.tencent.bk.job.manage.model.web.vo.CredentialBasicVO; import com.tencent.bk.job.manage.model.web.vo.CredentialVO; import com.tencent.bk.job.manage.service.CredentialService; import lombok.extern.slf4j.Slf4j; @@ -43,7 +44,6 @@ import org.springframework.web.bind.annotation.RestController; import java.util.List; -import java.util.Objects; import java.util.stream.Collectors; @@ -95,6 +95,31 @@ public Response> listCredentials(String username, return Response.buildSuccessResp(finalPageData); } + @Override + public Response> listCredentialBasicInfo(String username, + AppResourceScope appResourceScope, + String scopeType, + String scopeId, + Integer start, + Integer pageSize) { + BaseSearchCondition baseSearchCondition = new BaseSearchCondition(); + baseSearchCondition.setStart(start); + baseSearchCondition.setLength(pageSize); + PageData pageData = credentialService.listCredentialBasicInfo( + appResourceScope.getAppId(), + baseSearchCondition + ); + List credentialBasicVOList = + pageData.getData().stream().map(CredentialDTO::toBasicVO).collect(Collectors.toList()); + PageData finalPageData = new PageData<>(); + finalPageData.setStart(pageData.getStart()); + finalPageData.setPageSize(pageData.getPageSize()); + finalPageData.setTotal(pageData.getTotal()); + finalPageData.setData(credentialBasicVOList); + addPermissionDataForBasicVO(username, appResourceScope, finalPageData); + return Response.buildSuccessResp(finalPageData); + } + @Override @AuditEntry(actionId = ActionId.CREATE_TICKET) public Response createCredential(String username, @@ -138,7 +163,6 @@ private void addPermissionData(String username, AppResourceScope appResourceScop // 添加权限数据 List credentialIdList = credentialVOList.stream() .map(CredentialVO::getId) - .map(Objects::toString) .collect(Collectors.toList()); List canManageIdList = ticketAuthService.batchAuthManageTicket(username, appResourceScope, credentialIdList); @@ -151,6 +175,21 @@ private void addPermissionData(String username, AppResourceScope appResourceScop credentialVOPageData.setCanCreate(checkCreateTicketPermission(username, appResourceScope).isPass()); } + private void addPermissionDataForBasicVO(String username, AppResourceScope appResourceScope, + PageData credentialBasicVOPageData) { + List credentialBasicVOList = credentialBasicVOPageData.getData(); + // 添加权限数据 + List credentialIdList = credentialBasicVOList.stream() + .map(CredentialBasicVO::getId) + .collect(Collectors.toList()); + List canUseIdList = + ticketAuthService.batchAuthUseTicket(username, appResourceScope, credentialIdList); + credentialBasicVOList.forEach(it -> { + it.setCanUse(canUseIdList.contains(it.getId())); + }); + credentialBasicVOPageData.setCanCreate(checkCreateTicketPermission(username, appResourceScope).isPass()); + } + public AuthResult checkCreateTicketPermission(String username, AppResourceScope appResourceScope) { // 需要拥有在业务下创建凭证的权限 return ticketAuthService.authCreateTicket(username, appResourceScope); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/CredentialDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/CredentialDAO.java index 7a65ceacf4..cce64a770f 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/CredentialDAO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/CredentialDAO.java @@ -44,4 +44,6 @@ public interface CredentialDAO { List listCredentialDisplayInfoByIds(Collection ids); PageData listCredentials(CredentialDTO credentialQuery, BaseSearchCondition baseSearchCondition); + + PageData listCredentialBasicInfo(Long appId, BaseSearchCondition baseSearchCondition); } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/CredentialDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/CredentialDAOImpl.java index 7f52f6db39..452c0d5ff9 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/CredentialDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/CredentialDAOImpl.java @@ -42,6 +42,8 @@ import org.jooq.Condition; import org.jooq.DSLContext; import org.jooq.Record; +import org.jooq.Record2; +import org.jooq.Result; import org.jooq.SortField; import org.jooq.UpdateConditionStep; import org.jooq.conf.ParamType; @@ -208,6 +210,13 @@ private List buildConditionList( */ private long getPageCredentialCount(CredentialDTO credentialQuery, BaseSearchCondition baseSearchCondition) { List conditions = buildConditionList(credentialQuery, baseSearchCondition); + return getPageCredentialCount(conditions); + } + + /** + * 查询符合条件的凭据数量 + */ + private long getPageCredentialCount(Collection conditions) { Long count = dslContext .selectCount() .from(defaultTable) @@ -231,9 +240,55 @@ public PageData listCredentials( return listPageCredentialByConditions(baseSearchCondition, conditions, count); } + @Override + public PageData listCredentialBasicInfo(Long appId, BaseSearchCondition baseSearchCondition) { + Collection conditions = new ArrayList<>(); + conditions.add(defaultTable.APP_ID.eq(appId)); + long count = getPageCredentialCount(conditions); + return listPageCredentialBasicInfoByConditions(baseSearchCondition, conditions, count); + } + + public PageData listPageCredentialBasicInfoByConditions( + BaseSearchCondition baseSearchCondition, + Collection conditions, + long count + ) { + Integer start = baseSearchCondition.getStart(); + Integer length = baseSearchCondition.getLength(); + val query = + dslContext.select( + defaultTable.ID, + defaultTable.NAME + ).from(defaultTable) + .where(conditions) + .orderBy(defaultTable.LAST_MODIFY_TIME.desc()); + Result> records; + if (length != null && length > 0) { + records = query.limit(start, length).fetch(); + } else { + records = query.offset(start).fetch(); + } + List credentials = new ArrayList<>(); + if (records.size() != 0) { + records.forEach(record -> { + CredentialDTO credentialDTO = new CredentialDTO(); + credentialDTO.setId(record.get(defaultTable.ID)); + credentialDTO.setName(record.get(defaultTable.NAME)); + credentials.add(credentialDTO); + }); + } + + PageData credentialPageData = new PageData<>(); + credentialPageData.setTotal(count); + credentialPageData.setPageSize(length); + credentialPageData.setData(credentials); + credentialPageData.setStart(start); + return credentialPageData; + } + public PageData listPageCredentialByConditions( BaseSearchCondition baseSearchCondition, - List conditions, + Collection conditions, long count ) { Collection> orderFields = new ArrayList<>(); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/CredentialDTO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/CredentialDTO.java index 00c6b59749..d06bf84668 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/CredentialDTO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/CredentialDTO.java @@ -28,6 +28,7 @@ import com.tencent.bk.job.manage.common.consts.CredentialTypeEnum; import com.tencent.bk.job.manage.model.esb.v3.response.EsbCredentialSimpleInfoV3DTO; import com.tencent.bk.job.manage.model.inner.resp.ServiceCredentialDTO; +import com.tencent.bk.job.manage.model.web.vo.CredentialBasicVO; import com.tencent.bk.job.manage.model.web.vo.CredentialVO; import lombok.AllArgsConstructor; import lombok.Data; @@ -99,6 +100,13 @@ public CredentialVO toVO() { return credentialVO; } + public CredentialBasicVO toBasicVO() { + CredentialBasicVO credentialBasicVO = new CredentialBasicVO(); + credentialBasicVO.setId(id); + credentialBasicVO.setName(name); + return credentialBasicVO; + } + public ServiceCredentialDTO toServiceCredentialDTO() { ServiceCredentialDTO serviceCredentialDTO = new ServiceCredentialDTO(); serviceCredentialDTO.setId(id); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/CredentialService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/CredentialService.java index bbcb1eb02a..cf72b44f85 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/CredentialService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/CredentialService.java @@ -37,6 +37,8 @@ public interface CredentialService { PageData listCredentials(CredentialDTO credentialQuery, BaseSearchCondition baseSearchCondition); + PageData listCredentialBasicInfo(Long appId, BaseSearchCondition baseSearchCondition); + CredentialDTO createCredential(String username, Long appId, CredentialCreateUpdateReq createUpdateReq); CredentialDTO updateCredential(String username, Long appId, CredentialCreateUpdateReq createUpdateReq); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/CredentialServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/CredentialServiceImpl.java index af03349cca..c9a6a93cf2 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/CredentialServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/CredentialServiceImpl.java @@ -74,6 +74,11 @@ public PageData listCredentials( return credentialDAO.listCredentials(credentialQuery, baseSearchCondition); } + @Override + public PageData listCredentialBasicInfo(Long appId, BaseSearchCondition baseSearchCondition) { + return credentialDAO.listCredentialBasicInfo(appId, baseSearchCondition); + } + @Override @ActionAuditRecord( actionId = ActionId.CREATE_TICKET,