Rajoute une page listant les admins et des icônes pour voir plus facilement les permissions
This commit is contained in:
parent
77e085458c
commit
f65c3a991a
8 changed files with 214 additions and 73 deletions
|
@ -14,4 +14,5 @@ urlpatterns = [
|
|||
"permissions", views.PermissionManagementView.as_view(), name="auth.permissions"
|
||||
),
|
||||
path("accounts", views.AccountListView.as_view(), name="auth.accounts"),
|
||||
path("admins", views.AdminAccountsView.as_view(), name="auth.admins"),
|
||||
]
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth import views as auth_views
|
||||
from django.contrib.auth.hashers import make_password
|
||||
from django.contrib.auth.mixins import UserPassesTestMixin
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
|
||||
from django.contrib.auth.models import Permission
|
||||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.urls import reverse, reverse_lazy
|
||||
|
@ -13,6 +13,8 @@ from .utils import generate_password
|
|||
|
||||
User = get_user_model()
|
||||
|
||||
election_perm = Permission.objects.get(codename="election_admin")
|
||||
faq_perm = Permission.objects.get(codename="faq_admin")
|
||||
|
||||
# #############################################################################
|
||||
# Mixin to restrict access to staff members
|
||||
|
@ -89,6 +91,12 @@ class AccountListView(StaffMemberMixin, ListView):
|
|||
|
||||
ctx["cas_users"] = qs.filter(username__startswith="cas__")
|
||||
ctx["pwd_users"] = qs.filter(username__startswith="pwd__")
|
||||
ctx["e_manager"] = User.objects.with_perm(
|
||||
election_perm, backend="shared.auth.backends.PwdBackend"
|
||||
)
|
||||
ctx["f_manager"] = User.objects.with_perm(
|
||||
faq_perm, backend="shared.auth.backends.PwdBackend"
|
||||
)
|
||||
|
||||
return ctx
|
||||
|
||||
|
@ -133,18 +141,37 @@ class PermissionManagementView(StaffMemberMixin, SuccessMessageMixin, FormView):
|
|||
user.is_staff = form.cleaned_data["full_admin"]
|
||||
|
||||
# Election admin
|
||||
perm_election = Permission.objects.get(codename="election_admin")
|
||||
if form.cleaned_data["election_admin"]:
|
||||
perm_election.user_set.add(user)
|
||||
election_perm.user_set.add(user)
|
||||
else:
|
||||
perm_election.user_set.remove(user)
|
||||
election_perm.user_set.remove(user)
|
||||
|
||||
# FAQ admin
|
||||
perm_faq = Permission.objects.get(codename="faq_admin")
|
||||
if form.cleaned_data["faq_admin"]:
|
||||
perm_faq.user_set.add(user)
|
||||
faq_perm.user_set.add(user)
|
||||
else:
|
||||
perm_faq.user_set.remove(user)
|
||||
faq_perm.user_set.remove(user)
|
||||
|
||||
user.save()
|
||||
return super().form_valid(form)
|
||||
|
||||
|
||||
# #############################################################################
|
||||
# List of special accounts
|
||||
# #############################################################################
|
||||
|
||||
|
||||
class AdminAccountsView(LoginRequiredMixin, TemplateView):
|
||||
template_name = "auth/admin-accounts.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
ctx = super().get_context_data(**kwargs)
|
||||
|
||||
ctx["admin"] = User.objects.filter(is_staff=True)
|
||||
ctx["e_manager"] = User.objects.with_perm(
|
||||
election_perm, backend="shared.auth.backends.PwdBackend"
|
||||
)
|
||||
ctx["f_manager"] = User.objects.with_perm(
|
||||
faq_perm, backend="shared.auth.backends.PwdBackend"
|
||||
)
|
||||
return ctx
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue