feat(accounts): 实现用户资料与认证页面
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
"""用户资料服务。"""
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.db import transaction
|
||||
|
||||
from .models import UserProfile
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def get_or_create_profile(user) -> UserProfile:
|
||||
"""显式建立用户资料,避免依赖难以追踪的模型信号。"""
|
||||
|
||||
profile, _ = UserProfile.objects.get_or_create(user=user)
|
||||
return profile
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def deactivate_user(actor, target_user_id: int):
|
||||
"""仅允许管理员停用账户;保留关联审计记录。"""
|
||||
|
||||
if not actor.is_staff:
|
||||
raise PermissionError("只有管理员可以停用用户。")
|
||||
user = get_user_model().objects.select_for_update().get(pk=target_user_id)
|
||||
user.is_active = False
|
||||
user.save(update_fields=("is_active",))
|
||||
return user
|
||||
Reference in New Issue
Block a user