diff --git a/docs/apidoc/bk-api-gateway/v3/zh/delete_account.md b/docs/apidoc/bk-api-gateway/v3/zh/delete_account.md index d926986b25..fe38eb940e 100644 --- a/docs/apidoc/bk-api-gateway/v3/zh/delete_account.md +++ b/docs/apidoc/bk-api-gateway/v3/zh/delete_account.md @@ -14,7 +14,7 @@ | Accept | string | 是 | 固定值。application/json| | Content-Type | string | 是 | 固定值。application/json| -#### Body参数 +#### Query参数 | 字段 | 类型 | 必选 | 描述 | |---------------|------------|--------|------------| @@ -26,11 +26,7 @@ - DELETE ```json -{ - "bk_scope_type": "biz", - "bk_scope_id": "1", - "id": 70 -} +/api/v3/delete_account?bk_scope_type=biz&bk_scope_id=1&id=70 ``` ### 返回结果示例 diff --git a/docs/apidoc/bk-api-gateway/v3/zh/get_account_list.md b/docs/apidoc/bk-api-gateway/v3/zh/get_account_list.md index aa4d0bd528..20ea5acdf2 100644 --- a/docs/apidoc/bk-api-gateway/v3/zh/get_account_list.md +++ b/docs/apidoc/bk-api-gateway/v3/zh/get_account_list.md @@ -50,6 +50,7 @@ "category": 1, "type": 1, "os": "Linux", + "can_use": true, "creator": "admin", "bk_scope_type": "biz", "bk_scope_id": "1", @@ -94,6 +95,7 @@ | type | int | 账号类型(1:Linux,2:Windows,9:MySQL,10:Oracle,11:DB2)| | db_system_account_id | long | 账号用途为DB账号时该字段生效,表示DB账号对应的系统账号ID | | os | string | 账号用途为系统账号时该字段生效,账号对应的OS | +| can_use | bool | 是否有使用该账号的权限 | | creator | string | 创建人 | | create_time | long | 创建时间Unix时间戳(ms) | | last_modify_user | string | 最近一次修改人 | diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties index 79e1128427..4450fb72f7 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties @@ -62,3 +62,10 @@ validation.constraints.InvalidFileUploadSettingAmount.message=文件大小只能 validation.constraints.InvalidPort.message=端口号只能为1-65535之间的整数 validation.constraints.NotContainSpecialChar.message=不可包含 \\|/:*<>"? 特殊字符 validation.constraints.NotBlankField.message=不能为空 +validation.constraints.AccountId_empty.message=账号ID不能为空 +validation.constraints.AccountName_empty.message=账号名称[account]不能为空 +validation.constraints.AccountType_empty.message=账号类型[type]不能为空 +validation.constraints.AccountCategory_empty.message=账号用途[category]不能为空 +validation.constraints.AccountPassword_tooLong.message=账号密码[password]不能超过255字符 +validation.constraints.AccountAlias_tooLong.message=账号别名[alias]不能超过255字符 +validation.constraints.AccountDescription_tooLong.message=账号描述[description]不能超过1024字符 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties index 91cabc329c..25eac98244 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties @@ -61,3 +61,10 @@ validation.constraints.EmptyDynamicGroupId.message=DynamicGroup ID cannot be emp validation.constraints.InvalidFileUploadSettingAmount.message=File size must be a positive float validation.constraints.InvalidPort.message=Port must be a integer which is between 1 and 65535 validation.constraints.NotContainSpecialChar.message=Cannot contain special char, such as \\|/:*<>"? +validation.constraints.AccountId_empty.message=Account id cannot be empty +validation.constraints.AccountName_empty.message=Account name (account) cannot be empty +validation.constraints.AccountType_empty.message=Account type cannot be empty +validation.constraints.AccountCategory_empty.message=Account category cannot be empty +validation.constraints.AccountPassword_tooLong.message=Account password length cannot be larger than 255 +validation.constraints.AccountAlias_tooLong.message=Account alias length cannot be larger than 255 +validation.constraints.AccountDescription_tooLong.message=Account description length cannot be larger than 1024 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties index 91cabc329c..25eac98244 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties @@ -61,3 +61,10 @@ validation.constraints.EmptyDynamicGroupId.message=DynamicGroup ID cannot be emp validation.constraints.InvalidFileUploadSettingAmount.message=File size must be a positive float validation.constraints.InvalidPort.message=Port must be a integer which is between 1 and 65535 validation.constraints.NotContainSpecialChar.message=Cannot contain special char, such as \\|/:*<>"? +validation.constraints.AccountId_empty.message=Account id cannot be empty +validation.constraints.AccountName_empty.message=Account name (account) cannot be empty +validation.constraints.AccountType_empty.message=Account type cannot be empty +validation.constraints.AccountCategory_empty.message=Account category cannot be empty +validation.constraints.AccountPassword_tooLong.message=Account password length cannot be larger than 255 +validation.constraints.AccountAlias_tooLong.message=Account alias length cannot be larger than 255 +validation.constraints.AccountDescription_tooLong.message=Account description length cannot be larger than 1024 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties index bb93596afc..4a816debe1 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties @@ -61,3 +61,10 @@ validation.constraints.EmptyDynamicGroupId.message=动态分组 ID 不可为空 validation.constraints.InvalidFileUploadSettingAmount.message=文件大小只能为大于0的浮点数 validation.constraints.InvalidPort.message=端口号只能为1-65535之间的整数 validation.constraints.NotContainSpecialChar.message=不可包含 \\|/:*<>"? 特殊字符 +validation.constraints.AccountId_empty.message=账号ID不能为空 +validation.constraints.AccountName_empty.message=账号名称[account]不能为空 +validation.constraints.AccountType_empty.message=账号类型[type]不能为空 +validation.constraints.AccountCategory_empty.message=账号用途[category]不能为空 +validation.constraints.AccountPassword_tooLong.message=账号密码[password]不能超过255字符 +validation.constraints.AccountAlias_tooLong.message=账号别名[alias]不能超过255字符 +validation.constraints.AccountDescription_tooLong.message=账号描述[description]不能超过1024字符 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties index 085c4306e6..75372f148e 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties @@ -61,3 +61,10 @@ validation.constraints.EmptyDynamicGroupId.message=动态分组ID不可为空 validation.constraints.InvalidFileUploadSettingAmount.message=文件大小只能为大于0的浮点数 validation.constraints.InvalidPort.message=端口号只能为1-65535之间的整数 validation.constraints.NotContainSpecialChar.message=不可包含 \\|/:*<>"? 特殊字符 +validation.constraints.AccountId_empty.message=账号ID不能为空 +validation.constraints.AccountName_empty.message=账号名称[account]不能为空 +validation.constraints.AccountType_empty.message=账号类型[type]不能为空 +validation.constraints.AccountCategory_empty.message=账号用途[category]不能为空 +validation.constraints.AccountPassword_tooLong.message=账号密码[password]不能超过255字符 +validation.constraints.AccountAlias_tooLong.message=账号别名[alias]不能超过255字符 +validation.constraints.AccountDescription_tooLong.message=账号描述[description]不能超过1024字符 diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/v3/EsbAccountV3Resource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/v3/EsbAccountV3Resource.java index f2dc18d1c8..026dd0e6a7 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/v3/EsbAccountV3Resource.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/v3/EsbAccountV3Resource.java @@ -28,9 +28,12 @@ import com.tencent.bk.job.common.constant.JobCommonHeaders; import com.tencent.bk.job.common.esb.model.EsbResp; import com.tencent.bk.job.common.esb.model.job.v3.EsbPageDataV3; +import com.tencent.bk.job.manage.model.esb.v3.request.EsbCreateAccountV3Req; +import com.tencent.bk.job.manage.model.esb.v3.request.EsbDeleteAccountV3Req; import com.tencent.bk.job.manage.model.esb.v3.request.EsbGetAccountListV3Req; import com.tencent.bk.job.manage.model.esb.v3.response.EsbAccountV3DTO; import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -65,4 +68,39 @@ EsbResp> getAccountList( @RequestParam(value = "start", required = false) Integer start, @RequestParam(value = "length", required = false) Integer length); + @GetMapping("/search_account") + EsbResp> searchAccount( + @RequestHeader(value = JobCommonHeaders.USERNAME) String username, + @RequestHeader(value = JobCommonHeaders.APP_CODE) String appCode, + @RequestParam(value = "bk_scope_type", required = false) String scopeType, + @RequestParam(value = "bk_scope_id", required = false) String scopeId, + @RequestParam(value = "category", required = false) Integer category, + @RequestParam(value = "account", required = false) String account, + @RequestParam(value = "alias", required = false) String alias, + @RequestParam(value = "start", required = false) Integer start, + @RequestParam(value = "length", required = false) Integer length); + + @PostMapping("/create_account") + EsbResp createAccount( + @RequestBody + @Validated + EsbCreateAccountV3Req req + ); + + @DeleteMapping("/delete_account") + EsbResp deleteAccount( + @RequestHeader(value = JobCommonHeaders.USERNAME) String username, + @RequestHeader(value = JobCommonHeaders.APP_CODE) String appCode, + @RequestParam(value = "bk_scope_type", required = false) String scopeType, + @RequestParam(value = "bk_scope_id", required = false) String scopeId, + @RequestParam(value = "id") Long id + ); + + @PostMapping("/delete_account") + EsbResp deleteAccountUsingPost( + @RequestBody + @Validated + EsbDeleteAccountV3Req req + ); + } diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/request/EsbCreateAccountV3Req.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/request/EsbCreateAccountV3Req.java new file mode 100644 index 0000000000..7d9456dc64 --- /dev/null +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/request/EsbCreateAccountV3Req.java @@ -0,0 +1,80 @@ +/* + * 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.esb.v3.request; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.tencent.bk.job.common.esb.model.EsbAppScopeReq; +import com.tencent.bk.job.common.util.json.SkipLogFields; +import io.swagger.annotations.ApiModel; +import lombok.Data; +import lombok.ToString; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +@Data +@ApiModel("账号创建请求") +@ToString(exclude = {"password"}) +public class EsbCreateAccountV3Req extends EsbAppScopeReq { + + /** + * 帐号名称 + */ + @NotEmpty(message = "{validation.constraints.AccountName_empty.message}") + private String account; + + /** + * 账号类型:1-Linux,2-Windows,9-Mysql,10-Oracle,11-DB2 + */ + @NotNull(message = "{validation.constraints.AccountType_empty.message}") + private Integer type; + + /** + * 账号用途:1-系统账号,2-数据库账号 + */ + @NotNull(message = "{validation.constraints.AccountCategory_empty.message}") + private Integer category; + + /** + * 系统账号的密码(Windows) + */ + @SkipLogFields + @Length(max = 255, message = "{validation.constraints.AccountPassword_tooLong.message}") + private String password; + + /** + * 别名 + */ + @Length(max = 255, message = "{validation.constraints.AccountAlias_tooLong.message}") + private String alias; + + /** + * 描述 + */ + @JsonProperty("description") + @Length(max = 1024, message = "{validation.constraints.AccountDescription_tooLong.message}") + private String remark; +} diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/request/EsbDeleteAccountV3Req.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/request/EsbDeleteAccountV3Req.java new file mode 100644 index 0000000000..03b3f9113b --- /dev/null +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/request/EsbDeleteAccountV3Req.java @@ -0,0 +1,43 @@ +/* + * 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.esb.v3.request; + +import com.tencent.bk.job.common.esb.model.EsbAppScopeReq; +import io.swagger.annotations.ApiModel; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@Data +@ApiModel("账号删除请求") +public class EsbDeleteAccountV3Req extends EsbAppScopeReq { + + /** + * 帐号ID + */ + @NotNull(message = "{validation.constraints.AccountId_empty.message}") + private Long id; + +} diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/response/EsbAccountV3DTO.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/response/EsbAccountV3DTO.java index 9f96c43fc1..64ce217a70 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/response/EsbAccountV3DTO.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/esb/v3/response/EsbAccountV3DTO.java @@ -65,4 +65,10 @@ public class EsbAccountV3DTO extends EsbAppScopeDTO { */ @JsonProperty("description") private String description; + + /** + * 是否有权限使用 + */ + @JsonProperty("can_use") + private Boolean canUse; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/impl/v3/EsbAccountResourceV3Impl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/impl/v3/EsbAccountResourceV3Impl.java index 78ec970916..bb0833698e 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/impl/v3/EsbAccountResourceV3Impl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/esb/impl/v3/EsbAccountResourceV3Impl.java @@ -24,35 +24,51 @@ package com.tencent.bk.job.manage.api.esb.impl.v3; +import com.tencent.bk.job.common.constant.AccountCategoryEnum; import com.tencent.bk.job.common.esb.metrics.EsbApiTimed; import com.tencent.bk.job.common.esb.model.EsbResp; import com.tencent.bk.job.common.esb.model.job.v3.EsbPageDataV3; import com.tencent.bk.job.common.metrics.CommonMetricNames; import com.tencent.bk.job.common.model.BaseSearchCondition; +import com.tencent.bk.job.common.model.PageData; +import com.tencent.bk.job.common.model.dto.AppResourceScope; import com.tencent.bk.job.common.service.AppScopeMappingService; +import com.tencent.bk.job.common.util.date.DateUtils; import com.tencent.bk.job.manage.api.esb.v3.EsbAccountV3Resource; +import com.tencent.bk.job.manage.auth.AccountAuthService; +import com.tencent.bk.job.manage.common.consts.account.AccountTypeEnum; import com.tencent.bk.job.manage.model.dto.AccountDTO; +import com.tencent.bk.job.manage.model.dto.AccountSearchDTO; +import com.tencent.bk.job.manage.model.esb.v3.request.EsbCreateAccountV3Req; +import com.tencent.bk.job.manage.model.esb.v3.request.EsbDeleteAccountV3Req; import com.tencent.bk.job.manage.model.esb.v3.request.EsbGetAccountListV3Req; import com.tencent.bk.job.manage.model.esb.v3.response.EsbAccountV3DTO; import com.tencent.bk.job.manage.service.AccountService; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; @RestController @Slf4j public class EsbAccountResourceV3Impl implements EsbAccountV3Resource { private final AccountService accountService; private final AppScopeMappingService appScopeMappingService; + private final AccountAuthService accountAuthService; @Autowired public EsbAccountResourceV3Impl(AccountService accountService, - AppScopeMappingService appScopeMappingService) { + AppScopeMappingService appScopeMappingService, + AccountAuthService accountAuthService) { this.accountService = accountService; this.appScopeMappingService = appScopeMappingService; + this.accountAuthService = accountAuthService; } @Override @@ -60,28 +76,34 @@ public EsbAccountResourceV3Impl(AccountService accountService, public EsbResp> getAccountListUsingPost(EsbGetAccountListV3Req request) { request.fillAppResourceScope(appScopeMappingService); long appId = request.getAppId(); - BaseSearchCondition baseSearchCondition = new BaseSearchCondition(); - int start = 0; - if (request.getStart() != null && request.getStart() > 0) { - start = request.getStart(); - } - baseSearchCondition.setStart(start); - int length = 20; - if (request.getLength() != null && request.getLength() > 0) { - length = request.getLength(); - } - baseSearchCondition.setLength(length); + BaseSearchCondition baseSearchCondition = buildBaseSearchCondition(request.getStart(), request.getLength()); List accountList = accountService.listAllAppAccount(appId, null, baseSearchCondition); List accountV3DTOList = convertToEsbAccountV3DTOList(accountList); Integer accountCount = accountService.countAllAppAccount(appId, null); EsbPageDataV3 esbPageData = new EsbPageDataV3<>(); esbPageData.setTotal(accountCount.longValue()); - esbPageData.setStart(start); - esbPageData.setLength(length); + esbPageData.setStart(baseSearchCondition.getStart()); + esbPageData.setLength(baseSearchCondition.getLength()); + addPermissionForData(request.getUserName(), request.getAppResourceScope(), accountV3DTOList); esbPageData.setData(accountV3DTOList); return EsbResp.buildSuccessResp(esbPageData); } + private BaseSearchCondition buildBaseSearchCondition(Integer rawStart, Integer rawLength) { + BaseSearchCondition baseSearchCondition = new BaseSearchCondition(); + int start = 0; + if (rawStart != null && rawStart > 0) { + start = rawStart; + } + baseSearchCondition.setStart(start); + int length = 20; + if (rawLength != null && rawLength > 0) { + length = rawLength; + } + baseSearchCondition.setLength(length); + return baseSearchCondition; + } + private List convertToEsbAccountV3DTOList(List accounts) { List esbAccounts = new ArrayList<>(); if (accounts == null || accounts.isEmpty()) { @@ -114,4 +136,102 @@ public EsbResp> getAccountList(String username, request.setLength(length); return getAccountListUsingPost(request); } + + @Override + public EsbResp> searchAccount(String username, + String appCode, + String scopeType, + String scopeId, + Integer category, + String account, + String alias, + Integer start, + Integer length) { + Long appId = appScopeMappingService.getAppIdByScope(scopeType, scopeId); + BaseSearchCondition baseSearchCondition = buildBaseSearchCondition(start, length); + AccountSearchDTO accountSearchDTO = new AccountSearchDTO(); + accountSearchDTO.setAppId(appId); + accountSearchDTO.setCategory(category); + accountSearchDTO.setAccount(account); + accountSearchDTO.setAlias(alias); + PageData pageData = accountService.accurateSearchPageAccount(accountSearchDTO, baseSearchCondition); + EsbPageDataV3 esbPageData = new EsbPageDataV3<>(); + esbPageData.setTotal(pageData.getTotal()); + esbPageData.setStart(pageData.getStart()); + esbPageData.setLength(pageData.getPageSize()); + List esbAccountV3DTOList = pageData.getData().stream() + .map(AccountDTO::toEsbAccountV3DTO).collect(Collectors.toList()); + addPermissionForData(username, new AppResourceScope(scopeType, scopeId, appId), esbAccountV3DTOList); + esbPageData.setData(esbAccountV3DTOList); + return EsbResp.buildSuccessResp(esbPageData); + } + + private void addPermissionForData(String username, + AppResourceScope appResourceScope, + List esbAccountV3DTOList) { + List accountIdList = esbAccountV3DTOList.stream() + .map(EsbAccountV3DTO::getId).collect(Collectors.toList()); + List canUseIdList = accountAuthService.batchAuthUseAccount(username, appResourceScope, accountIdList); + Set canUseIds = new HashSet<>(canUseIdList); + esbAccountV3DTOList.forEach(esbAccountV3DTO -> { + esbAccountV3DTO.setCanUse(canUseIds.contains(esbAccountV3DTO.getId())); + }); + } + + @Override + public EsbResp createAccount(EsbCreateAccountV3Req req) { + req.fillAppResourceScope(appScopeMappingService); + AccountDTO accountDTO = buildCreateAccountDTO(req.getUserName(), req.getAppId(), req); + AccountDTO createdAccountDTO = accountService.createAccount(req.getUserName(), accountDTO); + return EsbResp.buildSuccessResp(createdAccountDTO.toEsbAccountV3DTO()); + } + + @SuppressWarnings("DuplicatedCode") + public AccountDTO buildCreateAccountDTO(String operator, long appId, EsbCreateAccountV3Req req) { + AccountDTO accountDTO = new AccountDTO(); + accountDTO.setAppId(appId); + accountDTO.setAccount(req.getAccount()); + accountDTO.setCategory(AccountCategoryEnum.valOf(req.getCategory())); + accountDTO.setType(AccountTypeEnum.valueOf(req.getType())); + if (StringUtils.isBlank(req.getAlias())) { + accountDTO.setAlias(req.getAccount()); + } else { + accountDTO.setAlias(req.getAlias()); + } + accountDTO.setRemark(req.getRemark()); + + if (AccountCategoryEnum.SYSTEM.getValue().equals(req.getCategory())) { + if (AccountTypeEnum.WINDOW.getType().equals(req.getType())) { + accountDTO.setOs("Windows"); + } else { + accountDTO.setOs("Linux"); + } + accountDTO.setPassword(req.getPassword()); + } + + accountDTO.setCreator(operator); + accountDTO.setCreateTime(DateUtils.currentTimeMillis()); + accountDTO.setLastModifyUser(operator); + accountDTO.setLastModifyTime(DateUtils.currentTimeMillis()); + + return accountDTO; + } + + @Override + public EsbResp deleteAccount(String username, + String appCode, + String scopeType, + String scopeId, + Long id) { + Long appId = appScopeMappingService.getAppIdByScope(scopeType, scopeId); + AccountDTO accountDTO = accountService.getAccount(appId, id); + accountService.deleteAccount(username, appId, id); + return EsbResp.buildSuccessResp(accountDTO.toEsbAccountV3DTO()); + } + + @Override + public EsbResp deleteAccountUsingPost(EsbDeleteAccountV3Req req) { + req.fillAppResourceScope(appScopeMappingService); + return deleteAccount(req.getUserName(), req.getAppCode(), req.getScopeType(), req.getScopeId(), req.getId()); + } } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/AccountDAO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/AccountDAO.java index 996a0b07dc..92cf82628e 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/AccountDAO.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/AccountDAO.java @@ -30,6 +30,7 @@ import com.tencent.bk.job.manage.common.consts.account.AccountTypeEnum; import com.tencent.bk.job.manage.model.dto.AccountDTO; import com.tencent.bk.job.manage.model.dto.AccountDisplayDTO; +import com.tencent.bk.job.manage.model.dto.AccountSearchDTO; import java.util.Collection; import java.util.List; @@ -102,6 +103,16 @@ public interface AccountDAO { */ PageData listPageAccount(AccountDTO accountQuery, BaseSearchCondition baseSearchCondition); + /** + * 分页精确搜索账号列表 + * + * @param accountSearchDTO 账号搜索条件 + * @param baseSearchCondition 基础查询条件 + * @return 账号分页 + */ + PageData accurateSearchPageAccount(AccountSearchDTO accountSearchDTO, + BaseSearchCondition baseSearchCondition); + /** * 分页搜索账号列表 * diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/AccountDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/AccountDAOImpl.java index 143b998297..9907366a66 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/AccountDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/AccountDAOImpl.java @@ -34,6 +34,7 @@ import com.tencent.bk.job.manage.dao.AccountDAO; import com.tencent.bk.job.manage.model.dto.AccountDTO; import com.tencent.bk.job.manage.model.dto.AccountDisplayDTO; +import com.tencent.bk.job.manage.model.dto.AccountSearchDTO; import com.tencent.bk.job.manage.model.tables.Account; import com.tencent.bk.job.manage.model.tables.TaskTemplate; import com.tencent.bk.job.manage.model.tables.TaskTemplateStep; @@ -343,6 +344,32 @@ public PageData listPageAccount(AccountDTO accountQuery, return listPageAccountByConditions(baseSearchCondition, conditions, count); } + @Override + public PageData accurateSearchPageAccount(AccountSearchDTO accountSearchDTO, + BaseSearchCondition baseSearchCondition) { + List conditions = buildConditions(accountSearchDTO); + long count = countAccountByConditions(conditions); + return listPageAccountByConditions(baseSearchCondition, conditions, count); + } + + private List buildConditions(AccountSearchDTO accountSearchDTO) { + List conditions = new ArrayList<>(); + conditions.add(TB_ACCOUNT.IS_DELETED.eq(UByte.valueOf(0))); + if (accountSearchDTO.getAppId() != null) { + conditions.add(TB_ACCOUNT.APP_ID.eq(accountSearchDTO.getAppId())); + } + if (accountSearchDTO.getCategory() != null) { + conditions.add(TB_ACCOUNT.CATEGORY.eq(JooqDataTypeUtil.getByteFromInteger(accountSearchDTO.getCategory()))); + } + if (StringUtils.isNotBlank(accountSearchDTO.getAccount())) { + conditions.add(TB_ACCOUNT.ACCOUNT_.eq(accountSearchDTO.getAccount())); + } + if (StringUtils.isNotBlank(accountSearchDTO.getAlias())) { + conditions.add(TB_ACCOUNT.ALIAS.eq(accountSearchDTO.getAlias())); + } + return conditions; + } + public PageData listPageAccountByConditions(BaseSearchCondition baseSearchCondition, List conditions, long count) { @@ -414,6 +441,10 @@ private long getPageAccountCount(Long appId, String keyword, BaseSearchCondition */ private long getPageAccountCount(AccountDTO accountQuery, BaseSearchCondition baseSearchCondition) { List conditions = buildConditionList(accountQuery, baseSearchCondition); + return countAccountByConditions(conditions); + } + + private long countAccountByConditions(List conditions) { Long count = ctx.selectCount().from(TB_ACCOUNT).where(conditions).fetchOne(0, Long.class); assert count != null; return count; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/AccountSearchDTO.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/AccountSearchDTO.java new file mode 100644 index 0000000000..9024d245a1 --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/model/dto/AccountSearchDTO.java @@ -0,0 +1,51 @@ +/* + * 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.dto; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class AccountSearchDTO { + + /** + * 业务ID + */ + private Long appId; + /** + * 账号用途 + */ + private Integer category; + /** + * 账号名称 + */ + private String account; + /** + * 账号别名 + */ + private String alias; + +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/AccountService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/AccountService.java index 2876145fdc..da177c1937 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/AccountService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/AccountService.java @@ -30,6 +30,7 @@ import com.tencent.bk.job.manage.common.consts.account.AccountTypeEnum; import com.tencent.bk.job.manage.model.dto.AccountDTO; import com.tencent.bk.job.manage.model.dto.AccountDisplayDTO; +import com.tencent.bk.job.manage.model.dto.AccountSearchDTO; import com.tencent.bk.job.manage.model.web.request.AccountCreateUpdateReq; import java.util.Collection; @@ -127,6 +128,16 @@ public interface AccountService { */ PageData listPageAccount(AccountDTO accountQuery, BaseSearchCondition baseSearchCondition); + /** + * 分页精确搜索账号列表 + * + * @param accountSearchDTO 账号搜索条件 + * @param baseSearchCondition 基础查询条件 + * @return 账号分页 + */ + PageData accurateSearchPageAccount(AccountSearchDTO accountSearchDTO, + BaseSearchCondition baseSearchCondition); + /** * 分页搜索账号列表 * diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/AccountServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/AccountServiceImpl.java index 46fa60f805..68ff134957 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/AccountServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/AccountServiceImpl.java @@ -57,6 +57,7 @@ import com.tencent.bk.job.manage.dao.AccountDAO; import com.tencent.bk.job.manage.model.dto.AccountDTO; import com.tencent.bk.job.manage.model.dto.AccountDisplayDTO; +import com.tencent.bk.job.manage.model.dto.AccountSearchDTO; import com.tencent.bk.job.manage.model.web.request.AccountCreateUpdateReq; import com.tencent.bk.job.manage.model.web.request.globalsetting.AccountNameRule; import com.tencent.bk.job.manage.service.AccountService; @@ -313,6 +314,12 @@ public PageData listPageAccount(AccountDTO accountQuery, BaseSearchC return accountDAO.listPageAccount(accountQuery, baseSearchCondition); } + @Override + public PageData accurateSearchPageAccount(AccountSearchDTO accountSearchDTO, + BaseSearchCondition baseSearchCondition) { + return accountDAO.accurateSearchPageAccount(accountSearchDTO, baseSearchCondition); + } + @Override public PageData searchPageAccount(Long appId, String keyword, BaseSearchCondition baseSearchCondition) throws ServiceException {