Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Validate managers count on update #1169

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions terraso_backend/apps/core/permission_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,16 @@ def validate_managers_count(user, membership, entity):
Returns:
bool: True if the count of managers is valid after the membership change, False otherwise.
"""
is_new = not membership
if is_new:
return True

manager_role = get_manager_role(entity)
is_manager = entity.membership_list.has_role(user, manager_role)

if not is_manager:
return True

managers_count = entity.membership_list.memberships.by_role(manager_role).count()
is_own_membership = user.collaboration_memberships.filter(pk=membership.id).exists()

Expand Down Expand Up @@ -137,13 +145,20 @@ def validate_change_membership(user, entity, obj):
- "user_role": The role the user will have after the change.
- "user_exists": A boolean indicating if the user is already registered.
- "user_email": The email of the user whose membership is being changed.
- "current_membership": The current user membership

Returns:
bool: True if the user is allowed to change the membership, False otherwise.
"""
user_role = obj.get("user_role")
user_exists = obj.get("user_exists")
user_email = obj.get("user_email")
current_membership = obj.get("current_membership")

valid_managers_count = validate_managers_count(user, current_membership, entity)

if not valid_managers_count:
return False

manager_role = get_manager_role(entity)
is_manager = entity.membership_list.has_role(user, manager_role)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ def test_group_membership_add(client_query, groups, users):
group = groups[0]
user = users[0]

group.membership_list.memberships.all().delete()

response = client_query(
"""
mutation addMembership($input: GroupMembershipSaveMutationInput!){
saveGroupMembership(input: $input) {
errors
memberships {
id
userRole
Expand Down Expand Up @@ -432,15 +435,23 @@ def test_group_membership_approve_by_member_fails(client_query, users, groups_cl
)


def test_group_membership_update_role_by_last_manager_fails(
client_query, users, group_manager_memberships, groups
):
manager_membership = group_manager_memberships[0]
def test_group_membership_update_role_by_last_manager_fails(client_query, users, groups):
user = users[0]
group = groups[0]

manager_membership.delete()
group.membership_list.memberships.all().delete()

old_membership = group.membership_list.memberships.filter(user=users[1]).first()
group.membership_list.save_membership(
user_email=user.email,
user_role=group_collaboration_roles.ROLE_MANAGER,
membership_status=CollaborationMembership.APPROVED,
)

group.membership_list.save_membership(
user_email=users[1].email,
user_role=group_collaboration_roles.ROLE_MEMBER,
membership_status=CollaborationMembership.APPROVED,
)

response = client_query(
"""
Expand All @@ -459,7 +470,7 @@ def test_group_membership_update_role_by_last_manager_fails(
""",
variables={
"input": {
"userEmails": [old_membership.user.email],
"userEmails": [user.email],
"userRole": group_collaboration_roles.ROLE_MEMBER,
"groupSlug": group.slug,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def test_landscape_membership_add(client_query, managed_landscapes, users):
landscape = managed_landscapes[0]
user = users[0]

landscape.membership_list.memberships.all().delete()

response = client_query(
"""
mutation addLandscapeMembership($input: LandscapeMembershipSaveMutationInput!){
Expand Down
Loading