Skip to content

Commit

Permalink
feat: add list_profile / retrieve_profile / list_department_profile api
Browse files Browse the repository at this point in the history
  • Loading branch information
nannan00 committed Jan 18, 2024
1 parent a1d3def commit 911c2b0
Show file tree
Hide file tree
Showing 6 changed files with 599 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apis/login/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class LocalUserCredentialAuthenticateApi(LoginApiAccessControlMixin, generics.Cr
"""本地数据源用户的凭据认证"""

def post(self, request, *args, **kwargs):
# TODO: 所有报错都添加日志,便于后续排查
slz = LocalUserCredentialAuthenticateInputSLZ(data=request.data)
slz.is_valid(raise_exception=True)
data = slz.validated_data
Expand Down
87 changes: 87 additions & 0 deletions src/bk-user/bkuser/apis/open_v2/serializers/profilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,90 @@
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.
"""
from typing import List

from rest_framework import serializers

from bkuser.common.serializers import StringArrayField


class ProfileFieldsSLZ(serializers.Serializer):
"""用户字段校验"""

fields = StringArrayField(help_text="指定返回的用户字段", required=False)

def validated_fields(self, fields: List[str]) -> List[str]:
# 不再支持返回的字段:
# [基本] code, qq
# [登录&密码相关] password_valid_days, password_update_time, last_login_time, account_expiration_date,
# [时间相关] create_time, update_time
# 总返回固定值字段:logo, position, type, role
allowed_fields = {
# 基础字段
"id",
"username",
"display_name", # 目前返回值是姓名,即 full_name,后续根据表达式展示
"email",
"telephone",
"country_code",
"iso_code",
"time_zone",
"language",
"extras",
# 微信消息通知相关
"wx_userid",
"wx_openid",
# 原目录相关
"domain",
"category_id",
# 生命周期相关
"status",
"staff_status",
"enabled",
# 关联关系
"departments",
"leader",
}
# 忽略无效的指定字段
return list(set(fields) & allowed_fields)


class ProfileRetrieveInputSLZ(ProfileFieldsSLZ):
lookup_field = serializers.ChoiceField(
help_text="指定路径参数值的字段", choices=["id", "username"], required=False, default="username"
)
include_disabled = serializers.BooleanField(help_text="是否包含软删除用户", required=False, default=False)


class ProfileListInputSLZ(ProfileFieldsSLZ):
lookup_field = serializers.ChoiceField(
help_text="字段名称",
choices=[
"id",
"username",
"display_name",
"email",
"telephone",
# 微信消息通知相关
"wx_userid",
# 原目录相关
"domain",
"category_id",
# 生命周期相关
"status",
"staff_status",
"enabled",
# IAM 特有
"create_time",
],
required=False,
)
exact_lookups = StringArrayField(help_text="精确匹配字段", required=False)
fuzzy_lookups = StringArrayField(help_text="模糊匹配字段", required=False)
include_disabled = serializers.BooleanField(help_text="是否包含软删除用户", required=False, default=False)
no_page = serializers.BooleanField(help_text="全量返回", required=False, default=False)


class DepartmentProfileListInputSLZ(serializers.Serializer):
recursive = serializers.BooleanField(help_text="是否递归", required=False, default=False)
include_disabled = serializers.BooleanField(help_text="是否包含软删除部门", required=False, default=False)
8 changes: 4 additions & 4 deletions src/bk-user/bkuser/apis/open_v2/views/departments.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get(self, request, *args, **kwargs):
resp_data["parent"] = self._get_dept_parent_id(tenant_dept, dept_relation)
resp_data["level"] = self._get_dept_tree_level(dept_relation)

tenant_dept_full_name = self._get_dept_full_name(dept_relation)
tenant_dept_full_name = self._get_dept_full_name(tenant_dept, dept_relation)
children = self._get_dept_children(tenant_dept, tenant_dept_full_name)
resp_data["full_name"] = tenant_dept_full_name
resp_data["has_children"] = bool(children)
Expand All @@ -228,11 +228,11 @@ def get(self, request, *args, **kwargs):
return Response(resp_data)

@staticmethod
def _get_dept_full_name(dept_relation: DataSourceDepartmentRelation) -> str:
def _get_dept_full_name(tenant_dept: TenantDepartment, dept_relation: DataSourceDepartmentRelation) -> str:
"""获取部门组织路径信息"""
# TODO 考虑协同的情况,不能直接吐出到根部门的路径
if not dept_relation:
return ""
return tenant_dept.data_source_department.name

return "/".join(dept_relation.get_ancestors(include_self=True).values_list("department__name", flat=True))

Expand Down Expand Up @@ -430,7 +430,7 @@ def _get_dept_full_name(dept: DataSourceDepartment) -> str:
"""获取部门组织路径信息"""
dept_relation = DataSourceDepartmentRelation.objects.filter(department=dept).first()
if not dept_relation:
return ""
return dept.name

# TODO 考虑协同的情况,不能直接吐出到根部门的路径
return "/".join(dept_relation.get_ancestors(include_self=True).values_list("department__name", flat=True))
Loading

0 comments on commit 911c2b0

Please sign in to comment.