-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadmin.py
More file actions
147 lines (130 loc) · 4.49 KB
/
Copy pathadmin.py
File metadata and controls
147 lines (130 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from constance import config
from django.contrib import admin, messages
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from .emails import (
new_superuser_notification_email,
remove_superuser_notification_email,
send_password_expired_email,
)
from .forms import UserChangeForm, UserCreationForm
from .models import PasswordValidation, SuperuserUpdateLog
User = get_user_model()
class AbstractUserAdmin(UserAdmin):
fieldsets = (
(
None,
{
"fields": (
"email",
"password",
"preferred_language",
"timezone",
"date_joined",
"last_login",
"password_changed_date",
)
},
),
(
_("Permissions"),
{
"fields": (
"is_active",
"is_staff",
"is_superuser",
"groups",
"user_permissions",
),
},
),
(_("Details"), {"fields": (("first_name", "last_name", "phone_number"),)}),
)
add_fieldsets = (
(
None,
{
"classes": ("wide",),
"fields": ("email", "password1", "password2"),
},
),
)
form = UserChangeForm
add_form = UserCreationForm
list_display = (
"id",
"email",
"first_name",
"last_name",
"preferred_language",
"timezone",
"date_joined",
"is_active",
"is_superuser",
"is_password_expired",
)
list_filter = ("date_joined", "is_superuser", "is_active", "preferred_language")
search_fields = ("first_name", "last_name", "email", "phone_number")
ordering = ("id",)
filter_horizontal = ()
actions = ["force_expire_password"]
def get_queryset(self, request):
qs = super().get_queryset(request)
return qs.add_is_password_expired()
def force_expire_password(self, request, queryset):
if not request.user.mfa_methods.filter(is_active=True).exists():
self.message_user(
request,
_("You must be a superuser with MFA enabled to perform this action."),
level=messages.ERROR,
)
return
queryset.update(
# Add extra time so the email doesn't get sent multiple times
password_changed_date=timezone.now()
- timezone.timedelta(days=config.USER_PASSWORD_EXPIRATION_INTERVAL + 7)
)
for user in queryset:
send_password_expired_email(user)
force_expire_password.short_description = "Expire password"
def is_password_expired(self, obj):
return obj.password_expired
def save_model(self, request, obj, form, change):
if change and hasattr(obj, "tracker") and obj.tracker.has_changed("is_superuser"):
SuperuserUpdateLog.objects.create(
assigner=request.user,
assignee=obj,
made_superuser=obj.is_superuser,
)
if obj.is_superuser:
new_superuser_notification_email(obj, request.user)
else:
remove_superuser_notification_email(obj, request.user)
super().save_model(request, obj, form, change)
@admin.register(SuperuserUpdateLog)
class SuperuserUpdateLogAdmin(admin.ModelAdmin):
list_display = ("id", "assigner", "assignee", "made_superuser")
list_display_links = None
list_display = ("assigner", "assignee", "made_superuser", "created")
def has_add_permission(self, request, obj=None):
return False
def changeform_view(self, request, object_id=None, form_url="", extra_context=None):
extra_context = extra_context or {}
extra_context["show_save_and_continue"] = False
extra_context["show_save"] = False
return super(SuperuserUpdateLogAdmin, self).changeform_view(
request, object_id, extra_context=extra_context
)
def has_delete_permission(self, request, obj=None):
if request.user.is_superuser:
return True
return False
@admin.register(PasswordValidation)
class PasswordValidationAdmin(admin.ModelAdmin):
list_display = (
"id",
"name",
"is_active",
)