diff --git a/jituan/services/admin_context.py b/jituan/services/admin_context.py index 077be20..b8e7b34 100644 --- a/jituan/services/admin_context.py +++ b/jituan/services/admin_context.py @@ -43,7 +43,7 @@ def resolve_admin_effective_club(user, request): return requested_club, requested_scope, None ctx = build_admin_club_context(user) - allowed = get_allowed_club_ids(user) + allowed = get_switchable_club_ids(user) if requested_scope == DATA_SCOPE_ALL: if not ctx.get('is_group_admin'): @@ -94,13 +94,16 @@ 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 return _pack( scope=DATA_SCOPE_SINGLE, - club_id=CLUB_ID_DEFAULT, + club_id=club_id, is_group_admin=False, assignments=[], - clubs=all_clubs, - can_switch_club=False, + clubs=visible_clubs, + can_switch_club=len(visible_clubs) > 1, role_code='CLUB_ADMIN', role_name='子公司客服(默认)', ) @@ -110,18 +113,22 @@ def build_admin_club_context(user): a.club_id is None or a.data_scope == DATA_SCOPE_ALL for a in assignments ) - allowed_club_ids = {a.club_id for a in assignments if a.club_id} + switchable_ids = get_switchable_club_ids(user) if has_group: scope = DATA_SCOPE_ALL club_id = CLUB_ID_DEFAULT + visible_clubs = all_clubs else: scope = DATA_SCOPE_SINGLE - club_id = primary.club_id or CLUB_ID_DEFAULT - - visible_clubs = all_clubs if has_group else [ - c for c in all_clubs if c['club_id'] in allowed_club_ids - ] + primary_cid = primary.club_id or CLUB_ID_DEFAULT + if primary_cid in switchable_ids: + club_id = primary_cid + elif switchable_ids: + club_id = sorted(switchable_ids)[0] + else: + club_id = primary_cid + visible_clubs = [c for c in all_clubs if c['club_id'] in switchable_ids] role_code = primary.role_code or 'CLUB_ADMIN' return _pack( @@ -171,6 +178,47 @@ 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 + + def can_manage_roles_for_club(user, permissions, club_id): """ 是否可为指定俱乐部分配/移除功能角色。