Skip to content

Commit

Permalink
✨ add is_crew attribute to users to differ between employee and adm…
Browse files Browse the repository at this point in the history
…in access
  • Loading branch information
krmax44 committed Nov 18, 2024
1 parent 0756cd3 commit 484ca10
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
8 changes: 8 additions & 0 deletions froide/account/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import re
from functools import cached_property
from re import Pattern
from typing import Dict, List, Optional, Tuple, Union

Expand Down Expand Up @@ -243,6 +244,13 @@ def get_dict(self, fields):
def trusted(self) -> bool:
return self.is_trusted or self.is_staff or self.is_superuser

@cached_property
def is_crew(self) -> bool:
if hasattr(settings, "CREW_GROUP") and settings.CREW_GROUP:
return self.groups.filter(pk=settings.CREW_GROUP).exists()
else:
return self.is_staff

@classmethod
def export_csv(cls, queryset, fields=None):
if fields is None:
Expand Down
2 changes: 1 addition & 1 deletion froide/document/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def filter_foirequest(self, qs, name, value):


def get_portal_queryset(request):
if not request.user.is_staff:
if not request.user.is_crew:
return DocumentPortal.objects.filter(public=True)
return DocumentPortal.objects.all()

Expand Down
2 changes: 1 addition & 1 deletion froide/foirequest/views/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def approve_attachment(request, foirequest, attachment_id):
att = get_object_or_404(
FoiAttachment, id=attachment_id, belongs_to__request=foirequest
)
if not att.can_approve and not request.user.is_staff:
if not att.can_approve and not request.user.is_crew:
return render_403(request)

# hard guard against publishing of non publishable requests
Expand Down
8 changes: 4 additions & 4 deletions froide/helper/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def get_read_queryset(
codename = get_permission_codename("view", opts)
if (
token is None
and user.is_staff
and user.is_crew
and user.has_perm("%s.%s" % (opts.app_label, codename))
):
return qs
Expand Down Expand Up @@ -188,7 +188,7 @@ def get_write_queryset(
codename = get_permission_codename("change", opts)
if (
token is None
and user.is_staff
and user.is_crew
and user.has_perm("%s.%s" % (opts.app_label, codename))
):
return qs
Expand Down Expand Up @@ -225,9 +225,9 @@ def get_user_filter(request, teams=None, fk_path=None):
return filter_arg


def require_staff(view_func):
def require_crew(view_func):
def decorator(request, *args, **kwargs):
if not hasattr(request, "user") or not request.user.is_staff:
if not request.user.is_crew:
raise PermissionDenied
return view_func(request, *args, **kwargs)

Expand Down
4 changes: 4 additions & 0 deletions froide/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ def MFA_SITE_TITLE(self):

MANAGERS = ADMINS

# instead of relying on user's `is_staff` attribute, you can also
# specify a user group that should be considered as "crew"
CREW_GROUP = None

INTERNAL_IPS = values.TupleValue(("127.0.0.1",))

# ############## PATHS ###############
Expand Down

0 comments on commit 484ca10

Please sign in to comment.