fix: 后台停用/禁用真正生效(重复集团任职与超管绕过)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -7,19 +7,19 @@ from jituan.models import AdminAssignment
|
||||
|
||||
|
||||
def _club_filter(qs, club_id):
|
||||
if club_id is None:
|
||||
if club_id is None or club_id == '':
|
||||
return qs.filter(Q(club_id__isnull=True) | Q(club_id=''))
|
||||
return qs.filter(club_id=club_id)
|
||||
|
||||
|
||||
def find_assignment(yonghuid, club_id, role_code):
|
||||
qs = AdminAssignment.query.filter(yonghuid=yonghuid, role_code=role_code)
|
||||
return _club_filter(qs, club_id).first()
|
||||
return _club_filter(qs, club_id).order_by('-id').first()
|
||||
|
||||
|
||||
def find_active_assignment(yonghuid, club_id, role_code):
|
||||
qs = AdminAssignment.query.filter(yonghuid=yonghuid, role_code=role_code, status=1)
|
||||
return _club_filter(qs, club_id).first()
|
||||
return _club_filter(qs, club_id).order_by('-id').first()
|
||||
|
||||
|
||||
def clear_other_primary_assignments(yonghuid, except_id=None):
|
||||
@@ -53,10 +53,17 @@ def create_or_reactivate_assignment(
|
||||
if is_primary:
|
||||
_clear_other_primary(yonghuid)
|
||||
|
||||
inactive = find_assignment(yonghuid, club_id, role_code)
|
||||
if inactive and inactive.status != 1:
|
||||
# MySQL 下 club_id=NULL 唯一约束不生效,可能有多条停用重复行,全部复活改只复活最新一条
|
||||
inactive_qs = _club_filter(
|
||||
AdminAssignment.objects.filter(yonghuid=yonghuid, role_code=role_code).exclude(status=1),
|
||||
club_id,
|
||||
).order_by('-id')
|
||||
inactive = inactive_qs.first()
|
||||
if inactive:
|
||||
if is_primary:
|
||||
_clear_other_primary(yonghuid)
|
||||
# 同键其它停用重复行保持停用,只启用一条
|
||||
inactive_qs.exclude(pk=inactive.pk).update(status=0, UpdateTime=timezone.now())
|
||||
AdminAssignment.objects.filter(pk=inactive.id).update(
|
||||
status=1,
|
||||
data_scope=data_scope,
|
||||
@@ -79,22 +86,41 @@ def create_or_reactivate_assignment(
|
||||
|
||||
|
||||
def deactivate_assignment(assignment_id):
|
||||
"""
|
||||
停用任职。必须连同「同人+同角色+同俱乐部」的全部有效行一起停用:
|
||||
MySQL 对 club_id=NULL 的 unique 不生效,集团任职常会积多条,只停一条等于没停。
|
||||
"""
|
||||
try:
|
||||
pk = int(assignment_id)
|
||||
except (TypeError, ValueError):
|
||||
return False, '任职 id 无效'
|
||||
|
||||
updated = AdminAssignment.objects.filter(pk=pk, status=1).update(
|
||||
row = AdminAssignment.objects.filter(pk=pk).first()
|
||||
if not row:
|
||||
return False, '任职记录不存在'
|
||||
|
||||
qs = AdminAssignment.objects.filter(
|
||||
yonghuid=row.yonghuid,
|
||||
role_code=row.role_code,
|
||||
status=1,
|
||||
)
|
||||
qs = _club_filter(qs, row.club_id)
|
||||
updated = qs.update(status=0, UpdateTime=timezone.now())
|
||||
# 目标行若已是停用,仍视为成功(幂等)
|
||||
if updated or int(row.status or 0) != 1:
|
||||
return True, None
|
||||
return True, None
|
||||
|
||||
|
||||
def deactivate_all_assignments_for_user(yonghuid):
|
||||
"""账号禁用时:停用该用户全部有效任职。"""
|
||||
yonghuid = (yonghuid or '').strip()
|
||||
if not yonghuid:
|
||||
return 0
|
||||
return AdminAssignment.objects.filter(yonghuid=yonghuid, status=1).update(
|
||||
status=0,
|
||||
UpdateTime=timezone.now(),
|
||||
)
|
||||
if updated:
|
||||
return True, None
|
||||
|
||||
exists = AdminAssignment.objects.filter(pk=pk).exists()
|
||||
if exists:
|
||||
return True, None
|
||||
return False, '任职记录不存在'
|
||||
|
||||
|
||||
def dedupe_active_assignments():
|
||||
|
||||
Reference in New Issue
Block a user