diff --git a/swh/web/add_forge_now/views.py b/swh/web/add_forge_now/views.py --- a/swh/web/add_forge_now/views.py +++ b/swh/web/add_forge_now/views.py @@ -17,7 +17,7 @@ AddForgeNowRequestPublicSerializer, AddForgeNowRequestSerializer, ) -from swh.web.auth.utils import ADD_FORGE_MODERATOR_PERMISSION +from swh.web.common.utils import has_add_forge_now_permission def add_forge_request_list_datatables(request: HttpRequest) -> HttpResponse: @@ -66,7 +66,7 @@ paginator = Paginator(add_forge_requests, per_page) page = paginator.page(page_num) - if request.user.has_perm(ADD_FORGE_MODERATOR_PERMISSION): + if has_add_forge_now_permission(request.user): requests = AddForgeNowRequestSerializer(page.object_list, many=True).data else: requests = AddForgeNowRequestPublicSerializer(page.object_list, many=True).data diff --git a/swh/web/admin/add_forge_now.py b/swh/web/admin/add_forge_now.py --- a/swh/web/admin/add_forge_now.py +++ b/swh/web/admin/add_forge_now.py @@ -8,17 +8,13 @@ from django.shortcuts import render from swh.web.admin.adminurls import admin_route -from swh.web.auth.utils import ADD_FORGE_MODERATOR_PERMISSION - - -def _can_access_moderation(user): - return user.is_staff or user.has_perm(ADD_FORGE_MODERATOR_PERMISSION) +from swh.web.common.utils import has_add_forge_now_permission @admin_route( r"add-forge/requests/", view_name="add-forge-now-requests-moderation", ) -@user_passes_test(_can_access_moderation, login_url=settings.LOGIN_URL) +@user_passes_test(has_add_forge_now_permission, login_url=settings.LOGIN_URL) def add_forge_now_requests_moderation_dashboard(request): """Moderation dashboard to allow listing current requests. @@ -34,7 +30,7 @@ r"add-forge/request/(?P(\d)+)/", view_name="add-forge-now-request-dashboard", ) -@user_passes_test(_can_access_moderation, login_url=settings.LOGIN_URL) +@user_passes_test(has_add_forge_now_permission, login_url=settings.LOGIN_URL) def add_forge_now_request_dashboard(request, request_id): """Moderation dashboard to allow listing current requests. diff --git a/swh/web/api/views/add_forge_now.py b/swh/web/api/views/add_forge_now.py --- a/swh/web/api/views/add_forge_now.py +++ b/swh/web/api/views/add_forge_now.py @@ -25,7 +25,7 @@ from swh.web.api.apiurls import api_route from swh.web.auth.utils import ADD_FORGE_MODERATOR_PERMISSION from swh.web.common.exc import BadInputExc -from swh.web.common.utils import reverse +from swh.web.common.utils import has_add_forge_now_permission, reverse def _block_while_testing(): @@ -194,7 +194,7 @@ "You must be authenticated to update a new add-forge request" ) - if not request.user.has_perm(ADD_FORGE_MODERATOR_PERMISSION): + if not has_add_forge_now_permission(request.user): return HttpResponseForbidden("You are not a moderator") add_forge_request = ( diff --git a/swh/web/common/utils.py b/swh/web/common/utils.py --- a/swh/web/common/utils.py +++ b/swh/web/common/utils.py @@ -521,3 +521,13 @@ if elt is not None: return elt.attrib["url"] return None + + +def has_add_forge_now_permission(user) -> bool: + """Is a user considered an add-forge-now moderator? + + Returns + True if a user is staff or has add forge now moderator permission + + """ + return user.is_staff or user.has_perm(ADD_FORGE_MODERATOR_PERMISSION)