From 115e94cb09b8ad8ee472afc3f5a5cf3acc455fa1 Mon Sep 17 00:00:00 2001 From: George Helman Date: Wed, 31 May 2023 00:14:11 -0400 Subject: [PATCH] Make new permission group for clinic leaders so they don't have to be superusers --- dear_petition/petition/api/serializers.py | 5 +++ src/components/pages/PageBase.jsx | 54 +++++++++++++++-------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/dear_petition/petition/api/serializers.py b/dear_petition/petition/api/serializers.py index 63ae7d8b..2d659d6e 100644 --- a/dear_petition/petition/api/serializers.py +++ b/dear_petition/petition/api/serializers.py @@ -23,6 +23,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer): admin_url = serializers.SerializerMethodField() + has_user_create_permission = serializers.SerializerMethodField() is_admin = serializers.BooleanField(source="is_staff", default=False) class Meta: @@ -35,6 +36,7 @@ class Meta: "is_staff", "admin_url", "last_login", + "has_user_create_permission" ] extra_kwargs = {"email": {"required": True, "allow_blank": False}} @@ -43,6 +45,9 @@ def get_admin_url(self, user_obj): if user_obj and user_obj.is_staff: url = reverse("admin:index") return url + + def get_has_user_create_permission(self, user_obj): + return user_obj.has_perm("petition.add_user") def create(self, validated_data): random_pw = User.objects.make_random_password() diff --git a/src/components/pages/PageBase.jsx b/src/components/pages/PageBase.jsx index 11aec364..5f0266ea 100644 --- a/src/components/pages/PageBase.jsx +++ b/src/components/pages/PageBase.jsx @@ -46,6 +46,39 @@ function PageBase({ children, className, ...props }) { const dispatch = useDispatch(); const [logout] = useLogoutMutation(); + const calculateManageDropDownMenuItems = () => { + let manageDropDownItems = []; + + if (user?.is_admin) { + manageDropDownItems = [ + ...manageDropDownItems, + + Agencies + , + + +
+ Admin + +
+
+
, + ]; + } + if (user?.has_user_create_permission) { + manageDropDownItems = [ + ...manageDropDownItems, + + Users + , + ]; + } + + return manageDropDownItems; + }; + + const manageDropDownMenuItems = calculateManageDropDownMenuItems(); + return ( @@ -64,25 +97,8 @@ function PageBase({ children, className, ...props }) { Help - {user?.is_admin ? ( - - Agencies - , - - Users - , - - -
- Admin - -
-
-
, - ]} - > + {manageDropDownMenuItems ? ( +
Manage