From 9f51a519585c2c060965468b056c9ca054471a9b Mon Sep 17 00:00:00 2001 From: winprn Date: Thu, 25 Apr 2024 10:52:13 +0700 Subject: [PATCH 1/4] fix: add user to Org Admin group when editing org admin --- judge/views/organization.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/judge/views/organization.py b/judge/views/organization.py index 54fb59390..aae26f9be 100644 --- a/judge/views/organization.py +++ b/judge/views/organization.py @@ -420,6 +420,11 @@ def form_valid(self, form): with revisions.create_revision(atomic=True): revisions.set_comment(_('Edited from site')) revisions.set_user(self.request.user) + org = form.save() + all_admins = org.admins.all() + g = Group.objects.get(name=settings.GROUP_PERMISSION_FOR_ORG_ADMIN) + for admin in all_admins: + admin.user.groups.add(g) return super(EditOrganization, self).form_valid(form) From 8883e536dfced875e9b57117f8f4d9ee0597f4ad Mon Sep 17 00:00:00 2001 From: winprn Date: Thu, 2 May 2024 17:33:11 +0700 Subject: [PATCH 2/4] feat: refactor duplicated code --- judge/utils/organization.py | 9 +++++++++ judge/views/organization.py | 12 +++--------- 2 files changed, 12 insertions(+), 9 deletions(-) create mode 100644 judge/utils/organization.py diff --git a/judge/utils/organization.py b/judge/utils/organization.py new file mode 100644 index 000000000..ca77e0f3b --- /dev/null +++ b/judge/utils/organization.py @@ -0,0 +1,9 @@ +from django.conf import settings +from django.contrib.auth.models import Group + +def add_admin_to_group(form): + org = form.save() + all_admins = org.admins.all() + g = Group.objects.get(name=settings.GROUP_PERMISSION_FOR_ORG_ADMIN) + for admin in all_admins: + admin.user.groups.add(g) \ No newline at end of file diff --git a/judge/views/organization.py b/judge/views/organization.py index aae26f9be..c6340d122 100644 --- a/judge/views/organization.py +++ b/judge/views/organization.py @@ -18,6 +18,7 @@ from django.utils.translation import gettext as _, gettext_lazy, ngettext from django.views.generic import CreateView, DetailView, FormView, ListView, UpdateView, View from django.views.generic.detail import SingleObjectMixin, SingleObjectTemplateResponseMixin +from judge.utils.organization import add_admin_to_group from reversion import revisions from judge.forms import OrganizationForm @@ -380,10 +381,7 @@ def form_valid(self, form): # short_name is show in ranking org.short_name = org.slug[:20] org.save() - all_admins = org.admins.all() - g = Group.objects.get(name=settings.GROUP_PERMISSION_FOR_ORG_ADMIN) - for admin in all_admins: - admin.user.groups.add(g) + add_admin_to_group(form) return HttpResponseRedirect(self.get_success_url()) @@ -420,11 +418,7 @@ def form_valid(self, form): with revisions.create_revision(atomic=True): revisions.set_comment(_('Edited from site')) revisions.set_user(self.request.user) - org = form.save() - all_admins = org.admins.all() - g = Group.objects.get(name=settings.GROUP_PERMISSION_FOR_ORG_ADMIN) - for admin in all_admins: - admin.user.groups.add(g) + add_admin_to_group(form) return super(EditOrganization, self).form_valid(form) From fcc7327b544c9dbdd2936c9e259ec6029e35e82a Mon Sep 17 00:00:00 2001 From: winprn Date: Thu, 2 May 2024 17:53:17 +0700 Subject: [PATCH 3/4] feat: only superuser can edit org's admins, fix: lint errors --- judge/forms.py | 7 +++++++ judge/utils/organization.py | 3 ++- judge/views/organization.py | 6 +++++- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/judge/forms.py b/judge/forms.py index bbd769da5..96ccb8183 100755 --- a/judge/forms.py +++ b/judge/forms.py @@ -457,6 +457,13 @@ class Meta: ), }) + def __init__(self, *args, **kwargs): + request = kwargs.pop('request', None) # Pop 'request' from kwargs + super().__init__(*args, **kwargs) + + if request and not request.user.is_superuser: + self.fields.pop('admins') + class SocialAuthMixin: def _has_social_auth(self, key): diff --git a/judge/utils/organization.py b/judge/utils/organization.py index ca77e0f3b..d02b8e7b9 100644 --- a/judge/utils/organization.py +++ b/judge/utils/organization.py @@ -1,9 +1,10 @@ from django.conf import settings from django.contrib.auth.models import Group + def add_admin_to_group(form): org = form.save() all_admins = org.admins.all() g = Group.objects.get(name=settings.GROUP_PERMISSION_FOR_ORG_ADMIN) for admin in all_admins: - admin.user.groups.add(g) \ No newline at end of file + admin.user.groups.add(g) diff --git a/judge/views/organization.py b/judge/views/organization.py index c6340d122..f084fd63b 100644 --- a/judge/views/organization.py +++ b/judge/views/organization.py @@ -4,7 +4,6 @@ from django.conf import settings from django.contrib import messages from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin -from django.contrib.auth.models import Group from django.core.exceptions import ImproperlyConfigured, PermissionDenied from django.db.models import Count, FilteredRelation, Q from django.db.models.expressions import F, Value @@ -421,6 +420,11 @@ def form_valid(self, form): add_admin_to_group(form) return super(EditOrganization, self).form_valid(form) + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + kwargs['request'] = self.request # Pass the request object to the form + return kwargs + class KickUserWidgetView(LoginRequiredMixin, AdminOrganizationMixin, SingleObjectMixin, View): def post(self, request, *args, **kwargs): From 02030bafa261c7fc6cb16854e1022977c11f110d Mon Sep 17 00:00:00 2001 From: winprn Date: Thu, 2 May 2024 18:09:47 +0700 Subject: [PATCH 4/4] fix: lint errors --- judge/views/organization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/judge/views/organization.py b/judge/views/organization.py index f084fd63b..6375d4fb3 100644 --- a/judge/views/organization.py +++ b/judge/views/organization.py @@ -17,7 +17,6 @@ from django.utils.translation import gettext as _, gettext_lazy, ngettext from django.views.generic import CreateView, DetailView, FormView, ListView, UpdateView, View from django.views.generic.detail import SingleObjectMixin, SingleObjectTemplateResponseMixin -from judge.utils.organization import add_admin_to_group from reversion import revisions from judge.forms import OrganizationForm @@ -25,6 +24,7 @@ Problem, Profile from judge.tasks import on_new_problem from judge.utils.infinite_paginator import InfinitePaginationMixin +from judge.utils.organization import add_admin_to_group from judge.utils.ranker import ranker from judge.utils.views import DiggPaginatorMixin, QueryStringSortMixin, TitleMixin, generic_message from judge.views.blog import BlogPostCreate, PostListBase