From c78a456b6a5919ec996caaa044397c19e33e2e79 Mon Sep 17 00:00:00 2001
From: Shrikrishna Singh <krishnasingh.ss30@gmail.com>
Date: Thu, 12 May 2022 17:22:27 +0530
Subject: [PATCH] Revamp user_can_alter_submissions_external_reviewers method

---
 hypha/apply/funds/forms.py | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/hypha/apply/funds/forms.py b/hypha/apply/funds/forms.py
index 4ed149714..ed2801430 100644
--- a/hypha/apply/funds/forms.py
+++ b/hypha/apply/funds/forms.py
@@ -253,10 +253,12 @@ class UpdateReviewersForm(ApplicationSubmissionModelForm):
 
     def can_alter_external_reviewers(self, instance, user):
         if instance.stage.has_external_review:
+            if user.is_superuser:
+                return True
             if settings.GIVE_STAFF_LEAD_PERMS:
-                return user.is_apply_staff or user.is_superuser
+                return user.is_apply_staff
             else:
-                return user == instance.lead or user.is_superuser
+                return user == instance.lead
         return False
 
     def clean(self):
@@ -355,7 +357,7 @@ class BatchUpdateReviewersForm(forms.Form):
         submissions = self.cleaned_data['submissions']
         if external_reviewers:
             # User needs to be superuser or lead of all selected submissions.
-            if self.user_cant_alter_submissions_external_reviewers(submissions, self.user):
+            if not self.user_can_alter_submissions_external_reviewers(submissions, self.user):
                 self.add_error('external_reviewers', _("Only Lead can change the External Reviewers"))
             # If user is trying to change the external reviewers for submissions that doesn't have workflow with external_review stage.
             elif self.submissions_cant_have_external_reviewers(submissions):
@@ -379,12 +381,14 @@ class BatchUpdateReviewersForm(forms.Form):
                 return True
         return False
 
-    def user_cant_alter_submissions_external_reviewers(self, submissions, user):
-        for submission in submissions:
-            if settings.GIVE_STAFF_LEAD_PERMS:
-                return user != submission.lead and not user.is_superuser
-            else:
-                return not user.is_apply_staff and not user.is_superuser
+    def user_can_alter_submissions_external_reviewers(self, submissions, user):
+        # User needs to be superuser or lead of all selected submissions.
+        if user.is_superuser:
+            return True
+        if settings.GIVE_STAFF_LEAD_PERMS and user.is_apply_staff:
+            return True
+        if submissions.count() == submissions.filter(lead=user).count():
+            return True
         return False
 
     def save(self):
-- 
GitLab