Skip to content

Commit 4f8b2c2

Browse files
authored
validate only on permission change (#11336)
1 parent ee90e09 commit 4f8b2c2

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

api/preprints/serializers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
)
5353
from osf.utils import permissions as osf_permissions
5454
from osf.utils.workflows import DefaultStates
55+
from osf.models.contributor import get_user_permission
5556

5657

5758
class PrimaryFileRelationshipField(RelationshipField):
@@ -653,6 +654,7 @@ def validate_permission(self, value):
653654
user # if user is None then probably we're trying to make bulk update and this validation is not relevant
654655
and preprint.machine_state == DefaultStates.INITIAL.value
655656
and preprint.creator_id == user.id
657+
and get_user_permission(user, preprint) != value
656658
):
657659
raise ValidationError(
658660
'You cannot change your permission setting at this time. '

osf/models/contributor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,15 @@ def get_contributor_permission(contributor, resource):
103103
"""
104104
Returns a contributor's permissions - perms through contributorship only. No permissions through osf group membership.
105105
"""
106+
return get_user_permission(contributor.user, resource)
107+
108+
109+
def get_user_permission(user, resource):
106110
read = resource.format_group(permissions.READ)
107111
write = resource.format_group(permissions.WRITE)
108112
admin = resource.format_group(permissions.ADMIN)
109113
# Checking for django group membership allows you to also get the intended permissions of unregistered contributors
110-
user_groups = contributor.user.groups.filter(name__in=[read, write, admin]).values_list('name', flat=True)
114+
user_groups = user.groups.filter(name__in=[read, write, admin]).values_list('name', flat=True)
111115
if admin in user_groups:
112116
return permissions.ADMIN
113117
elif write in user_groups:

0 commit comments

Comments
 (0)