Skip to content

Commit

Permalink
Merge pull request #2580 from jsonwan/github_fix/credential_page
Browse files Browse the repository at this point in the history
fix: 文件源选取凭证无法选取到第一页以后的数据 #2579
  • Loading branch information
wangyu096 authored Nov 3, 2023
2 parents d1d4c81 + 48c0e8f commit 9149b7b
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,6 +91,29 @@ Response<PageData<CredentialVO>> listCredentials(
Integer pageSize
);

@ApiOperation(value = "分页获取凭据基础信息", produces = "application/json")
@GetMapping("/basicInfo/list")
Response<PageData<CredentialBasicVO>> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


Expand Down Expand Up @@ -95,6 +95,31 @@ public Response<PageData<CredentialVO>> listCredentials(String username,
return Response.buildSuccessResp(finalPageData);
}

@Override
public Response<PageData<CredentialBasicVO>> listCredentialBasicInfo(String username,
AppResourceScope appResourceScope,
String scopeType,
String scopeId,
Integer start,
Integer pageSize) {
BaseSearchCondition baseSearchCondition = new BaseSearchCondition();
baseSearchCondition.setStart(start);
baseSearchCondition.setLength(pageSize);
PageData<CredentialDTO> pageData = credentialService.listCredentialBasicInfo(
appResourceScope.getAppId(),
baseSearchCondition
);
List<CredentialBasicVO> credentialBasicVOList =
pageData.getData().stream().map(CredentialDTO::toBasicVO).collect(Collectors.toList());
PageData<CredentialBasicVO> 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<CredentialVO> createCredential(String username,
Expand Down Expand Up @@ -138,7 +163,6 @@ private void addPermissionData(String username, AppResourceScope appResourceScop
// 添加权限数据
List<String> credentialIdList = credentialVOList.stream()
.map(CredentialVO::getId)
.map(Objects::toString)
.collect(Collectors.toList());
List<String> canManageIdList =
ticketAuthService.batchAuthManageTicket(username, appResourceScope, credentialIdList);
Expand All @@ -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<CredentialBasicVO> credentialBasicVOPageData) {
List<CredentialBasicVO> credentialBasicVOList = credentialBasicVOPageData.getData();
// 添加权限数据
List<String> credentialIdList = credentialBasicVOList.stream()
.map(CredentialBasicVO::getId)
.collect(Collectors.toList());
List<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public interface CredentialDAO {
List<ServiceCredentialDisplayDTO> listCredentialDisplayInfoByIds(Collection<String> ids);

PageData<CredentialDTO> listCredentials(CredentialDTO credentialQuery, BaseSearchCondition baseSearchCondition);

PageData<CredentialDTO> listCredentialBasicInfo(Long appId, BaseSearchCondition baseSearchCondition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,6 +210,13 @@ private List<Condition> buildConditionList(
*/
private long getPageCredentialCount(CredentialDTO credentialQuery, BaseSearchCondition baseSearchCondition) {
List<Condition> conditions = buildConditionList(credentialQuery, baseSearchCondition);
return getPageCredentialCount(conditions);
}

/**
* 查询符合条件的凭据数量
*/
private long getPageCredentialCount(Collection<Condition> conditions) {
Long count = dslContext
.selectCount()
.from(defaultTable)
Expand All @@ -231,9 +240,55 @@ public PageData<CredentialDTO> listCredentials(
return listPageCredentialByConditions(baseSearchCondition, conditions, count);
}

@Override
public PageData<CredentialDTO> listCredentialBasicInfo(Long appId, BaseSearchCondition baseSearchCondition) {
Collection<Condition> conditions = new ArrayList<>();
conditions.add(defaultTable.APP_ID.eq(appId));
long count = getPageCredentialCount(conditions);
return listPageCredentialBasicInfoByConditions(baseSearchCondition, conditions, count);
}

public PageData<CredentialDTO> listPageCredentialBasicInfoByConditions(
BaseSearchCondition baseSearchCondition,
Collection<Condition> 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<Record2<String, String>> records;
if (length != null && length > 0) {
records = query.limit(start, length).fetch();
} else {
records = query.offset(start).fetch();
}
List<CredentialDTO> 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<CredentialDTO> credentialPageData = new PageData<>();
credentialPageData.setTotal(count);
credentialPageData.setPageSize(length);
credentialPageData.setData(credentials);
credentialPageData.setStart(start);
return credentialPageData;
}

public PageData<CredentialDTO> listPageCredentialByConditions(
BaseSearchCondition baseSearchCondition,
List<Condition> conditions,
Collection<Condition> conditions,
long count
) {
Collection<SortField<?>> orderFields = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface CredentialService {

PageData<CredentialDTO> listCredentials(CredentialDTO credentialQuery, BaseSearchCondition baseSearchCondition);

PageData<CredentialDTO> listCredentialBasicInfo(Long appId, BaseSearchCondition baseSearchCondition);

CredentialDTO createCredential(String username, Long appId, CredentialCreateUpdateReq createUpdateReq);

CredentialDTO updateCredential(String username, Long appId, CredentialCreateUpdateReq createUpdateReq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public PageData<CredentialDTO> listCredentials(
return credentialDAO.listCredentials(credentialQuery, baseSearchCondition);
}

@Override
public PageData<CredentialDTO> listCredentialBasicInfo(Long appId, BaseSearchCondition baseSearchCondition) {
return credentialDAO.listCredentialBasicInfo(appId, baseSearchCondition);
}

@Override
@ActionAuditRecord(
actionId = ActionId.CREATE_TICKET,
Expand Down

0 comments on commit 9149b7b

Please sign in to comment.