From 1e54d8b871ca7736e67c81812c82b80cc0e132f3 Mon Sep 17 00:00:00 2001 From: XingQue Date: Mon, 6 Jul 2026 18:40:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A4=9A=E4=BF=B1=E4=B9=90=E9=83=A8?= =?UTF-8?q?=E4=BB=BB=E8=81=8C=E5=8D=B3=E5=8F=AF=E5=88=87=E6=8D=A2=EF=BC=8C?= =?UTF-8?q?=E6=9D=83=E9=99=90=E6=8C=89=E5=BD=93=E5=89=8D=E4=BF=B1=E4=B9=90?= =?UTF-8?q?=E9=83=A8=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- jituan/services/admin_context.py | 65 +++++++++----------------------- jituan/services/club_rbac.py | 7 +++- 2 files changed, 22 insertions(+), 50 deletions(-) diff --git a/jituan/services/admin_context.py b/jituan/services/admin_context.py index b8e7b34..181a72e 100644 --- a/jituan/services/admin_context.py +++ b/jituan/services/admin_context.py @@ -24,9 +24,14 @@ def get_allowed_club_ids(user): ).exists() or assignments.filter(data_scope=DATA_SCOPE_ALL).exists() if has_group: return {c.club_id for c in Club.query.filter(status=1)} - return { + allowed = { a.club_id for a in assignments if a.club_id } + if not allowed: + cid = (getattr(user, 'ClubID', None) or '').strip() + if cid: + allowed = {cid} + return allowed def resolve_admin_effective_club(user, request): @@ -43,7 +48,7 @@ def resolve_admin_effective_club(user, request): return requested_club, requested_scope, None ctx = build_admin_club_context(user) - allowed = get_switchable_club_ids(user) + allowed = get_allowed_club_ids(user) if requested_scope == DATA_SCOPE_ALL: if not ctx.get('is_group_admin'): @@ -94,9 +99,9 @@ def build_admin_club_context(user): ) if not assignments: - switchable = get_switchable_club_ids(user) - visible_clubs = [c for c in all_clubs if c['club_id'] in switchable] - club_id = next(iter(switchable), CLUB_ID_DEFAULT) if switchable else CLUB_ID_DEFAULT + allowed_ids = get_allowed_club_ids(user) + visible_clubs = [c for c in all_clubs if c['club_id'] in allowed_ids] + club_id = next(iter(allowed_ids), CLUB_ID_DEFAULT) if allowed_ids else CLUB_ID_DEFAULT return _pack( scope=DATA_SCOPE_SINGLE, club_id=club_id, @@ -113,7 +118,7 @@ def build_admin_club_context(user): a.club_id is None or a.data_scope == DATA_SCOPE_ALL for a in assignments ) - switchable_ids = get_switchable_club_ids(user) + allowed_ids = get_allowed_club_ids(user) if has_group: scope = DATA_SCOPE_ALL @@ -122,13 +127,13 @@ def build_admin_club_context(user): else: scope = DATA_SCOPE_SINGLE primary_cid = primary.club_id or CLUB_ID_DEFAULT - if primary_cid in switchable_ids: + if primary_cid in allowed_ids: club_id = primary_cid - elif switchable_ids: - club_id = sorted(switchable_ids)[0] + elif allowed_ids: + club_id = sorted(allowed_ids)[0] else: club_id = primary_cid - visible_clubs = [c for c in all_clubs if c['club_id'] in switchable_ids] + visible_clubs = [c for c in all_clubs if c['club_id'] in allowed_ids] role_code = primary.role_code or 'CLUB_ADMIN' return _pack( @@ -179,44 +184,8 @@ def can_manage_admin_assignments(user, permissions=None): def get_switchable_club_ids(user): - """ - 顶栏可切换俱乐部:须已分配该俱乐部,且在该俱乐部下至少有一个功能角色。 - 无角色则不可切换,避免切过去后菜单空白却误显全部页面。 - """ - from gvsdsdk.models import UserRole - from jituan.services.club_rbac import filter_role_uuids_for_club_scope - from jituan.constants import DATA_SCOPE_SINGLE - - if is_system_super_admin(user): - return {c.club_id for c in Club.query.filter(status=1)} - - assignments = AdminAssignment.query.filter(yonghuid=user.UserUID, status=1) - has_group = ( - assignments.filter(club_id__isnull=True).exists() - or assignments.filter(data_scope=DATA_SCOPE_ALL).exists() - ) - if has_group: - return {c.club_id for c in Club.query.filter(status=1)} - - allowed = get_allowed_club_ids(user) - if not allowed: - cid = (getattr(user, 'ClubID', None) or '').strip() - if cid: - allowed = {cid} - - role_uuids = list( - UserRole.objects.filter(UserUUID=user.UserUUID).values_list('RoleUUID', flat=True) - ) - if not allowed or not role_uuids: - return set() - - switchable = set() - for cid in allowed: - if filter_role_uuids_for_club_scope( - role_uuids, cid, DATA_SCOPE_SINGLE, list(allowed), - ): - switchable.add(cid) - return switchable + """顶栏可切换俱乐部:有任职/数据范围即可切换;菜单按当前俱乐部功能角色展示。""" + return get_allowed_club_ids(user) def can_manage_roles_for_club(user, permissions, club_id): diff --git a/jituan/services/club_rbac.py b/jituan/services/club_rbac.py index 68a66c7..179608c 100644 --- a/jituan/services/club_rbac.py +++ b/jituan/services/club_rbac.py @@ -149,8 +149,11 @@ def filter_role_uuids_for_club_scope(role_uuids, club_id, scope, allowed_club_id ) return list(qs.filter(ClubID='').values_list('RoleUUID', flat=True)) cid = (club_id or CLUB_ID_DEFAULT).strip() - # 本俱乐部角色 + 历史全局角色(club_id 为空);否则旧角色绑定的 002ac 等会菜单/API 不一致 - return list(qs.filter(Q(ClubID=cid) | Q(ClubID='')).values_list('RoleUUID', flat=True)) + # 优先本俱乐部专属角色,避免全局角色权限串到其他俱乐部 + club_specific = qs.filter(ClubID=cid) + if club_specific.exists(): + return list(club_specific.values_list('RoleUUID', flat=True)) + return list(qs.filter(ClubID='').values_list('RoleUUID', flat=True)) def resolve_permission_codes_for_club(user, club_id, allowed_club_ids=None):