fix: 多俱乐部任职即可切换,权限按当前俱乐部计算

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-07-06 18:40:42 +08:00
parent df6c6912d9
commit 1e54d8b871
2 changed files with 22 additions and 50 deletions

View File

@@ -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):