Skip to content

Commit

Permalink
fix(backend): 权限规则接口优化 TencentBlueKing#5794
Browse files Browse the repository at this point in the history
  • Loading branch information
ygcyao committed Jul 24, 2024
1 parent fa15267 commit e97344a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ def _format_account_rules(self, account_rules_list: Dict) -> Dict:
for account_rules in account_rules_list["items"]:
account_rules["account"]["account_id"] = account_rules["account"].pop("id")

# 检查 rules 是否为 None
if account_rules.get("rules") is None:
account_rules["rules"] = []

for rule in account_rules["rules"]:
rule["rule_id"] = rule.pop("id")
rule["access_db"] = rule.pop("dbname")
Expand Down
15 changes: 15 additions & 0 deletions dbm-ui/backend/db_services/dbpermission/db_account/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ class FilterAccountRulesSerializer(serializers.Serializer):
access_db = serializers.CharField(help_text=_("访问DB"), required=False)
privilege = serializers.CharField(help_text=_("规则列表"), required=False)

limit = serializers.IntegerField(required=False, default=10)
offset = serializers.IntegerField(required=False, default=0)

def validate(self, attrs):
# 重命名字段
if "account_type" in attrs:
attrs["cluster_type"] = attrs.pop("account_type")
if "access_db" in attrs:
attrs["dbname"] = attrs.pop("access_db")
if "privilege" in attrs:
attrs["privs"] = attrs.pop("privilege").split(",")
if "rule_ids" in attrs:
attrs["ids"] = [int(i) for i in attrs.pop("rule_ids").split(",")]
return attrs


class QueryAccountRulesSerializer(serializers.Serializer):
user = serializers.CharField(help_text=_("账号名称"))
Expand Down
25 changes: 19 additions & 6 deletions dbm-ui/backend/db_services/dbpermission/db_account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from backend.bk_web import viewsets
from backend.bk_web.swagger import common_swagger_auto_schema
from backend.components import DBPrivManagerApi
from backend.db_services.dbpermission.db_account.dataclass import AccountMeta, AccountRuleMeta
from backend.db_services.dbpermission.db_account.handlers import AccountHandler
from backend.db_services.dbpermission.db_account.serializers import (
Expand Down Expand Up @@ -143,12 +144,24 @@ def add_account_rule(self, request, bk_biz_id):
],
)
def list_account_rules(self, request, bk_biz_id):
return self._view_common_handler(
request=request,
bk_biz_id=bk_biz_id,
meta=self.account_rule_meta,
func=self.account_handler.list_account_rules.__name__,
)
validated_data = self.params_validate(self.get_serializer_class())
base_info = {
"bk_biz_id": bk_biz_id,
"operator": request.user.username,
"account_type": validated_data.get("cluster_type"),
"context": {},
}
params = {**validated_data, "bk_biz_id": bk_biz_id}

# 判断无过滤条件 或者过滤条件只有user 则展示无规则用户
if validated_data["offset"] == 0 or validated_data.get("user"):
params.update({"no_rule_user": True})

rules_list = DBPrivManagerApi.list_account_rules(params)
if not rules_list["items"]:
return Response({"count": 0, "results": []})
account_rules_list = self.account_handler(**base_info)._format_account_rules(rules_list)
return Response({"count": account_rules_list["count"], "results": account_rules_list["items"]})

@common_swagger_auto_schema(
operation_summary=_("查询账号规则"),
Expand Down

0 comments on commit e97344a

Please sign in to comment.