Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Exclude superuser from restriction in staff mutations

IKarbowiaked96e98609c3
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index b25f126e66..ff453fb680 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -211,9 +211,11 @@ class StaffCreate(ModelMutation):
@classmethod
def clean_permissions(cls, info, cleaned_input, errors):
permissions = cleaned_input.get("permissions")
- out_of_scope_permissions = get_out_of_scope_permissions(
- info.context.user, permissions
- )
+ cleaned_input["user_permissions"] = get_permissions(permissions)
+ user = info.context.user
+ if user.is_superuser:
+ return
+ out_of_scope_permissions = get_out_of_scope_permissions(user, permissions)
if out_of_scope_permissions:
error_msg = "You can't manage permission that you don't have."
code = AccountErrorCode.OUT_OF_SCOPE_PERMISSION.value
@@ -221,8 +223,6 @@ class StaffCreate(ModelMutation):
error = ValidationError(message=error_msg, code=code, params=params)
errors["permissions"].append(error)
- cleaned_input["user_permissions"] = get_permissions(permissions)
-
@classmethod
def clean_groups(cls, info, cleaned_input, errors):
if cleaned_input.get("add_groups"):
@@ -230,8 +230,11 @@ class StaffCreate(ModelMutation):
@classmethod
def can_manage_groups(cls, info, cleaned_input, field, errors):
+ user = info.context.user
+ if user.is_superuser:
+ return
groups = cleaned_input[field]
- user_editable_groups = get_groups_which_user_can_manage(info.context.user)
+ user_editable_groups = get_groups_which_user_can_manage(user)
out_of_scope_groups = set(groups) - set(user_editable_groups)
if out_of_scope_groups:
# add error
@@ -288,8 +291,9 @@ class StaffUpdate(StaffCreate):
@classmethod
def clean_input(cls, info, instance, data):
+ user = info.context.user
# check if requestor can manage this user
- if get_out_of_scope_users(info.context.user, [instance]):
+ if not user.is_superuser and get_out_of_scope_users(user, [instance]):
msg = "You can't manage this user."
code = AccountErrorCode.OUT_OF_SCOPE_USER.value
raise ValidationError({"id": ValidationError(msg, code=code)})
@@ -329,7 +333,9 @@ class StaffUpdate(StaffCreate):
return
if not is_active:
cls.check_if_deactivating_superuser_or_own_account(instance, user, errors)
- cls.check_if_deactivating_left_not_manageable_permissions(instance, errors)
+ cls.check_if_deactivating_left_not_manageable_permissions(
+ instance, user, errors
+ )
@classmethod
def check_if_deactivating_superuser_or_own_account(
@@ -357,7 +363,7 @@ class StaffUpdate(StaffCreate):
@classmethod
def check_if_deactivating_left_not_manageable_permissions(
- cls, user: User, errors: dict
+ cls, user: User, requestor: User, errors: dict
):
"""Check if after deactivating user all permissions will be manageable.
@@ -365,6 +371,8 @@ class StaffUpdate(StaffCreate):
active staff member who can manage it (has both “manage staff” and
this permission).
"""
+ if requestor.is_superuser:
+ return
permissions = get_not_manageable_permissions_when_deactivate_or_remove_users(
[user]
)
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index ba1b2cddeb..125fa376ac 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1590,6 +1590,61 @@ def test_staff_create_out_of_scope_permission_and_group(
_send_set_password_email_mock.assert_not_called()
+@patch("saleor.account.emails._send_set_password_email")
+def test_staff_create_superuser_pass_restrictions(
+ _send_set_password_email_mock,
+ superuser_api_client,
+ media_root,
+ permission_manage_staff,
+ permission_group_manage_users,
+):
+ """Ensure user can create staff even if permissions ang groups are out of scope
+ based on checks.
+ """
+ group = permission_group_manage_users
+ group2 = Group.objects.create(name="second group")
+ group2.permissions.add(permission_manage_staff)
+ email = "[email protected]"
+ variables = {
+ "email": email,
+ "permissions": [
+ PermissionEnum.MANAGE_PRODUCTS.name,
+ PermissionEnum.MANAGE_STAFF.name,
+ ],
+ "redirect_url": "https://www.example.com",
+ "add_groups": [
+ graphene.Node.to_global_id("Group", gr.pk) for gr in [group, group2]
+ ],
+ }
+
+ response = superuser_api_client.post_graphql(STAFF_CREATE_MUTATION, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["staffCreate"]
+
+ assert data["staffErrors"] == []
+ assert data["user"]["email"] == email
+ assert data["user"]["isStaff"]
+ assert data["user"]["isActive"]
+ assert re.match(
+ r"http://testserver/media/user-avatars/avatar\d+.*",
+ data["user"]["avatar"]["url"],
+ )
+ permissions = data["user"]["userPermissions"]
+ assert permissions[0]["code"] == "MANAGE_PRODUCTS"
+
+ # deprecated, to remove in #5389
+ permissions = data["user"]["permissions"]
+ assert permissions[0]["code"] == "MANAGE_PRODUCTS"
+
+ staff_user = User.objects.get(email=email)
+
+ assert staff_user.is_staff
+
+ _send_set_password_email_mock.assert_called_once_with(
+ staff_user.email, ANY, "dashboard/staff/set_password"
+ )
+
+
@patch("saleor.account.emails._send_set_user_password_email_with_url.delay")
def test_staff_create_send_password_with_url(
_send_set_user_password_email_with_url_mock,
@@ -1694,6 +1749,7 @@ STAFF_UPDATE_MUTATIONS = """
name
}
isActive
+ email
}
}
}
@@ -1782,14 +1838,22 @@ def test_staff_update_groups_and_permissions(
def test_staff_update_out_of_scope_user(
- staff_api_client, permission_manage_staff, permission_manage_orders, media_root
+ staff_api_client,
+ superuser_api_client,
+ permission_manage_staff,
+ permission_manage_orders,
+ media_root,
):
+ """Ensure that staff user cannot and superuser can update user with wider
+ scope of permission.
+ """
query = STAFF_UPDATE_MUTATIONS
staff_user = User.objects.create(email="[email protected]", is_staff=True)
staff_user.user_permissions.add(permission_manage_orders)
id = graphene.Node.to_global_id("User", staff_user.id)
variables = {"id": id, "input": {"permissions": [], "isActive": False}}
+ # for staff user
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_staff]
)
@@ -1800,10 +1864,24 @@ def test_staff_update_out_of_scope_user(
assert data["staffErrors"][0]["field"] == "id"
assert data["staffErrors"][0]["code"] == AccountErrorCode.OUT_OF_SCOPE_USER.name
+ # for superuser
+ response = superuser_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["staffUpdate"]
+ assert data["user"]["email"] == staff_user.email
+ assert data["user"]["isActive"] is False
+ assert not data["staffErrors"]
+
def test_staff_update_out_of_scope_permissions(
- staff_api_client, permission_manage_staff, media_root, permission_manage_orders
+ staff_api_client,
+ superuser_api_client,
+ permission_manage_staff,
+ media_root,
+ permission_manage_orders,
+ permission_manage_products,
):
+ """Ensure that user cannot and superuser can add permissions which doesn't have."""
query = STAFF_UPDATE_MUTATIONS
staff_user = User.objects.create(email="[email protected]", is_staff=True)
staff_api_client.user.user_permissions.add(permission_manage_orders)
@@ -1819,6 +1897,7 @@ def test_staff_update_out_of_scope_permissions(
},
}
+ # for staff user
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_staff]
)
@@ -1832,15 +1911,31 @@ def test_staff_update_out_of_scope_permissions(
assert errors[0]["permissions"] == [PermissionEnum.MANAGE_PRODUCTS.name]
assert errors[0]["groups"] is None
+ # for superuser
+ response = superuser_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["staffUpdate"]
+ errors = data["staffErrors"]
+ assert not errors
+ assert data["user"]["email"] == staff_user.email
+ assert {perm["code"].lower() for perm in data["user"]["userPermissions"]} == {
+ permission_manage_orders.codename,
+ permission_manage_products.codename,
+ }
+
def test_staff_update_out_of_scope_groups(
staff_api_client,
+ superuser_api_client,
permission_manage_staff,
media_root,
permission_manage_users,
permission_manage_orders,
permission_manage_products,
):
+ """Ensure that staff user cannot and superuser can add to groups which permission
+ scope is wider than user's scope.
+ """
query = STAFF_UPDATE_MUTATIONS
groups = Group.objects.bulk_create(
@@ -1871,6 +1966,7 @@ def test_staff_update_out_of_scope_groups(
},
}
+ # for staff user
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_staff]
)
@@ -1898,6 +1994,18 @@ def test_staff_update_out_of_scope_groups(
error.pop("message")
assert error in expected_errors
+ # for superuser
+ response = superuser_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["staffUpdate"]
+ errors = data["staffErrors"]
+ assert not errors
+ assert data["user"]["email"] == staff_user.email
+ assert {group["name"] for group in data["user"]["permissionGroups"]} == {
+ group1.name,
+ group2.name,
+ }
+
def test_staff_update_cannot_add_and_remove(
staff_api_client,
@@ -1993,11 +2101,16 @@ def test_staff_update_doesnt_change_existing_avatar(
def test_staff_update_deactivate_with_manage_staff_left_not_manageable_perms(
staff_api_client,
+ superuser_api_client,
staff_users,
permission_manage_users,
permission_manage_staff,
permission_manage_orders,
+ media_root,
):
+ """Ensure that staff user can't and superuser can deactivate user where some
+ permissions will be not manageable.
+ """
query = STAFF_UPDATE_MUTATIONS
groups = Group.objects.bulk_create(
[
@@ -2021,6 +2134,8 @@ def test_staff_update_deactivate_with_manage_staff_left_not_manageable_perms(
id = graphene.Node.to_global_id("User", staff_user1.id)
variables = {"id": id, "input": {"isActive": False}}
+
+ # for staff user
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_staff]
)
@@ -2035,6 +2150,18 @@ def test_staff_update_deactivate_with_manage_staff_left_not_manageable_perms(
assert len(errors[0]["permissions"]) == 1
assert errors[0]["permissions"][0] == AccountPermissions.MANAGE_USERS.name
+ # for superuser
+ response = superuser_api_client.post_graphql(query, variables)
+ content = get_graphql_content(response)
+ data = content["data"]["staffUpdate"]
+ errors = data["staffErrors"]
+
+ staff_user1.refresh_from_db()
+ assert data["user"]["email"] == staff_user1.email
+ assert data["user"]["isActive"] is False
+ assert not errors
+ assert not staff_user1.is_active
+
def test_staff_update_deactivate_with_manage_staff_all_perms_manageable(
staff_api_client,