feat: 新增超级管理员重置用户密码功能
- 用户列表页 /users/:支持按用户名/姓名/邮箱搜索、按状态筛选、分页 - 重置密码页 /users/<id>/password/:仅超级管理员可访问 - 复用 Django 密码校验器(一致性、长度、相似度、常见/纯数字密码) - 管理员重置自身密码时保持登录会话不失效 - settings 补充 AUTH_PASSWORD_VALIDATORS 配置
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
from django import forms
|
||||
from django.contrib.auth.password_validation import password_validators_help_text_html, validate_password
|
||||
from .models import Asset, Category
|
||||
|
||||
|
||||
@@ -33,3 +34,39 @@ class CategoryForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Category
|
||||
fields = ['name', 'description']
|
||||
|
||||
|
||||
class AdminPasswordResetForm(forms.Form):
|
||||
"""管理员重置指定用户密码的表单"""
|
||||
|
||||
new_password1 = forms.CharField(
|
||||
label='新密码',
|
||||
widget=forms.PasswordInput(attrs={'class': 'form-control', 'autocomplete': 'new-password'}),
|
||||
help_text=password_validators_help_text_html(),
|
||||
)
|
||||
new_password2 = forms.CharField(
|
||||
label='确认新密码',
|
||||
widget=forms.PasswordInput(attrs={'class': 'form-control', 'autocomplete': 'new-password'}),
|
||||
)
|
||||
|
||||
def __init__(self, *args, target_user=None, **kwargs):
|
||||
self.target_user = target_user
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def clean(self):
|
||||
cleaned_data = super().clean()
|
||||
password1 = cleaned_data.get('new_password1')
|
||||
password2 = cleaned_data.get('new_password2')
|
||||
|
||||
if password1 and password2 and password1 != password2:
|
||||
raise forms.ValidationError('两次输入的密码不一致')
|
||||
|
||||
if password1 and self.target_user is not None:
|
||||
validate_password(password1, self.target_user)
|
||||
|
||||
return cleaned_data
|
||||
|
||||
def save(self):
|
||||
self.target_user.set_password(self.cleaned_data['new_password1'])
|
||||
self.target_user.save(update_fields=['password'])
|
||||
return self.target_user
|
||||
|
||||
Reference in New Issue
Block a user