Skip to content

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

Allow apps to query data protected by MANAGE_STAFF permission (#10103)

SzymJ4eb93d3f5c62
diff --git a/saleor/core/permissions/__init__.py b/saleor/core/permissions/__init__.py
index 3d72bce1d6..1becc0c85d 100644
--- a/saleor/core/permissions/__init__.py
+++ b/saleor/core/permissions/__init__.py
@@ -90,18 +90,12 @@ def one_of_permissions_or_auth_filter_required(context, permissions):
# TODO: move this function from graphql to core
from saleor.graphql.utils import get_user_or_app_from_context
- is_app = bool(getattr(context, "app", None))
requestor = get_user_or_app_from_context(context)
if permissions:
perm_checks_results = []
for permission in permissions:
- if is_app and permission == AccountPermissions.MANAGE_STAFF:
- # `MANAGE_STAFF` permission for apps is not supported, as apps using it
- # could create a staff user with full access.
- perm_checks_results.append(False)
- else:
- perm_checks_results.append(requestor.has_perm(permission))
+ perm_checks_results.append(requestor.has_perm(permission))
granted_by_permissions = any(perm_checks_results)
if authorization_filters:
diff --git a/saleor/graphql/account/bulk_mutations.py b/saleor/graphql/account/bulk_mutations.py
index eedba450e4..a5ae7037e0 100644
--- a/saleor/graphql/account/bulk_mutations.py
+++ b/saleor/graphql/account/bulk_mutations.py
@@ -47,7 +47,9 @@ class CustomerBulkDelete(CustomerDeleteMixin, UserBulkDelete):
class StaffBulkDelete(StaffDeleteMixin, UserBulkDelete):
class Meta:
- description = "Deletes staff users."
+ description = (
+ "Deletes staff users. Apps are not allowed to perform this mutation."
+ )
model = models.User
object_type = User
permissions = (AccountPermissions.MANAGE_STAFF,)
@@ -70,6 +72,7 @@ class StaffBulkDelete(StaffDeleteMixin, UserBulkDelete):
@classmethod
def clean_instances(cls, info, users):
errors = defaultdict(list)
+
requestor = info.context.user
cls.check_if_users_can_be_deleted(info, users, "ids", errors)
cls.check_if_requestor_can_manage_users(requestor, users, "ids", errors)
diff --git a/saleor/graphql/account/mutations/permission_group.py b/saleor/graphql/account/mutations/permission_group.py
index 60fd9a9024..15072d6674 100644
--- a/saleor/graphql/account/mutations/permission_group.py
+++ b/saleor/graphql/account/mutations/permission_group.py
@@ -6,6 +6,7 @@ from django.contrib.auth import models as auth_models
from django.core.exceptions import ValidationError
from ....account.error_codes import PermissionGroupErrorCode
+from ....core.exceptions import PermissionDenied
from ....core.permissions import AccountPermissions, get_permissions
from ....core.tracing import traced_atomic_transaction
from ...account.utils import (
@@ -50,7 +51,10 @@ class PermissionGroupCreate(ModelMutation):
)
class Meta:
- description = "Create new permission group."
+ description = (
+ "Create new permission group. "
+ "Apps are not allowed to perform this mutation."
+ )
model = auth_models.Group
object_type = Group
permissions = (AccountPermissions.MANAGE_STAFF,)
@@ -69,12 +73,7 @@ class PermissionGroupCreate(ModelMutation):
instance.user_set.add(*users)
@classmethod
- def clean_input(
- cls,
- info,
- instance,
- data,
- ):
+ def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
requestor = info.context.user
@@ -104,6 +103,14 @@ class PermissionGroupCreate(ModelMutation):
requestor, errors, field, permission_items
)
+ @classmethod
+ def check_permissions(cls, context, permissions=None):
+ if context.app:
+ raise PermissionDenied(
+ message="Apps are not allowed to perform this mutation."
+ )
+ return super().check_permissions(context, permissions)
+
@classmethod
def ensure_can_manage_permissions(
cls,
@@ -190,7 +197,9 @@ class PermissionGroupUpdate(PermissionGroupCreate):
)
class Meta:
- description = "Update permission group."
+ description = (
+ "Update permission group. Apps are not allowed to perform this mutation."
+ )
model = auth_models.Group
object_type = Group
permissions = (AccountPermissions.MANAGE_STAFF,)
@@ -412,7 +421,9 @@ class PermissionGroupDelete(ModelDeleteMutation):
id = graphene.ID(description="ID of the group to delete.", required=True)
class Meta:
- description = "Delete permission group."
+ description = (
+ "Delete permission group. Apps are not allowed to perform this mutation."
+ )
model = auth_models.Group
object_type = Group
permissions = (AccountPermissions.MANAGE_STAFF,)
@@ -431,6 +442,14 @@ class PermissionGroupDelete(ModelDeleteMutation):
cls.check_if_group_can_be_removed(requestor, instance)
+ @classmethod
+ def check_permissions(cls, context, permissions=None):
+ if context.app:
+ raise PermissionDenied(
+ message="Apps are not allowed to perform this mutation."
+ )
+ return super().check_permissions(context, permissions)
+
@classmethod
def check_if_group_can_be_removed(cls, requestor, group):
cls.ensure_deleting_not_left_not_manageable_permissions(group)
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index a45e4d3d12..3d64f7c6cf 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -16,6 +16,7 @@ from ....account.utils import (
remove_the_oldest_user_address_if_address_limit_is_reached,
)
from ....checkout import AddressType
+from ....core.exceptions import PermissionDenied
from ....core.permissions import AccountPermissions, AuthorizationFilters
from ....core.tracing import traced_atomic_transaction
from ....core.utils.url import validate_storefront_url
@@ -186,7 +187,10 @@ class StaffCreate(ModelMutation):
)
class Meta:
- description = "Creates a new staff user."
+ description = (
+ "Creates a new staff user. "
+ "Apps are not allowed to perform this mutation."
+ )
exclude = ["password"]
model = models.User
object_type = User
@@ -194,6 +198,14 @@ class StaffCreate(ModelMutation):
error_type_class = StaffError
error_type_field = "staff_errors"
+ @classmethod
+ def check_permissions(cls, context, permissions=None):
+ if context.app:
+ raise PermissionDenied(
+ message="Apps are not allowed to perform this mutation."
+ )
+ return super().check_permissions(context, permissions)
+
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
@@ -319,7 +331,10 @@ class StaffUpdate(StaffCreate):
)
class Meta:
- description = "Updates an existing staff user."
+ description = (
+ "Updates an existing staff user. "
+ "Apps are not allowed to perform this mutation."
+ )
exclude = ["password"]
model = models.User
object_type = User
@@ -455,7 +470,9 @@ class StaffUpdate(StaffCreate):
class StaffDelete(StaffDeleteMixin, UserDelete):
class Meta:
- description = "Deletes a staff user."
+ description = (
+ "Deletes a staff user. Apps are not allowed to perform this mutation."
+ )
model = models.User
object_type = User
permissions = (AccountPermissions.MANAGE_STAFF,)
diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.py
index c983974705..4b57c4620b 100644
--- a/saleor/graphql/account/tests/test_account.py
+++ b/saleor/graphql/account/tests/test_account.py
@@ -409,36 +409,6 @@ def test_query_customer_user_app(
assert data["email"] == user.email
-def test_query_customer_user_app_no_permission(
- app_api_client,
- customer_user,
- address,
- permission_manage_users,
- permission_manage_staff,
- media_root,
- app,
-):
- user = customer_user
- user.default_shipping_address.country = "US"
- user.default_shipping_address.save()
- user.addresses.add(address.get_copy())
-
- avatar_mock = MagicMock(spec=File)
- avatar_mock.name = "image.jpg"
- user.avatar = avatar_mock
- user.save()
-
- Group.objects.create(name="empty group")
-
- query = FULL_USER_QUERY
- ID = graphene.Node.to_global_id("User", customer_user.id)
- variables = {"id": ID}
- app.permissions.add(permission_manage_staff)
- response = app_api_client.post_graphql(query, variables)
-
- assert_no_permission(response)
-
-
def test_query_customer_user_with_orders_by_app_no_manage_orders_perm(
app_api_client,
customer_user,
@@ -3586,14 +3556,14 @@ def test_user_delete_errors(staff_user, admin_user):
def test_staff_delete_errors(staff_user, customer_user, admin_user):
- info = Mock(context=Mock(user=staff_user))
+ info = Mock(context=Mock(user=staff_user, app=None))
with pytest.raises(ValidationError) as e:
StaffDelete.clean_instance(info, customer_user)
msg = "Cannot delete a non-staff users."
assert e.value.error_dict["id"][0].message == msg
# should not raise any errors
- info = Mock(context=Mock(user=admin_user))
+ info = Mock(context=Mock(user=admin_user, app=None))
StaffDelete.clean_instance(info, staff_user)
@@ -5553,27 +5523,6 @@ def test_query_staff_members_with_filter_by_ids(
assert len(users) == 1
-def test_query_staff_members_app_no_permission(
- query_staff_users_with_filter,
- app_api_client,
- permission_manage_staff,
-):
-
- User.objects.bulk_create(
- [
- User(email="[email protected]", is_staff=True, is_active=False),
- User(email="[email protected]", is_staff=True, is_active=True),
- ]
- )
-
- variables = {"filter": {"status": "DEACTIVATED"}}
- response = app_api_client.post_graphql(
- query_staff_users_with_filter, variables, permissions=[permission_manage_staff]
- )
-
- assert_no_permission(response)
-
-
@pytest.mark.parametrize(
"staff_member_filter, count",
[
diff --git a/saleor/graphql/account/tests/test_account_permission_group.py b/saleor/graphql/account/tests/test_account_permission_group.py
index 9ac97726a0..cd4082f53f 100644
--- a/saleor/graphql/account/tests/test_account_permission_group.py
+++ b/saleor/graphql/account/tests/test_account_permission_group.py
@@ -2200,24 +2200,6 @@ def test_permission_groups_query_with_filter_by_ids(
assert len(data) == 1
-def test_permission_groups_app_no_permission(
- permission_group_manage_users,
- permission_manage_staff,
- app_api_client,
- app,
-):
- app.permissions.add(permission_manage_staff)
- query = QUERY_PERMISSION_GROUP_WITH_FILTER
- Group.objects.bulk_create(
- [Group(name="Manage product."), Group(name="Remove product.")]
- )
- variables = {"filter": {"search": "Manage user groups"}}
-
- response = app_api_client.post_graphql(query, variables)
-
- assert_no_permission(response)
-
-
def test_permission_groups_no_permission_to_perform(
permission_group_manage_users,
permission_manage_staff,
@@ -2332,24 +2314,6 @@ def test_permission_group_query(
assert data["userCanManage"] is True
-def test_permission_group_query_app_no_permission(
- permission_group_manage_users,
- staff_user,
- permission_manage_staff,
- permission_manage_users,
- app_api_client,
- app,
-):
- app.permissions.add(permission_manage_staff, permission_manage_users)
- group = permission_group_manage_users
- query = QUERY_PERMISSION_GROUP
- variables = {"id": graphene.Node.to_global_id("Group", group.id)}
-
- response = app_api_client.post_graphql(query, variables)
-
- assert_no_permission(response)
-
-
def test_permission_group_query_user_cannot_manage(
permission_group_manage_users,
staff_user,
diff --git a/saleor/graphql/account/utils.py b/saleor/graphql/account/utils.py
index f9623ccb92..08a667fd44 100644
--- a/saleor/graphql/account/utils.py
+++ b/saleor/graphql/account/utils.py
@@ -82,10 +82,20 @@ class StaffDeleteMixin(UserDeleteMixin):
class Meta:
abstract = True
+ @classmethod
+ def check_permissions(cls, context, permissions=None):
+ if context.app:
+ raise PermissionDenied(
+ message="Apps are not allowed to perform this mutation."
+ )
+ return super().check_permissions(context, permissions)
+
@classmethod
def clean_instance(cls, info, instance):
errors = defaultdict(list)
+
requestor = info.context.user
+
cls.check_if_users_can_be_deleted(info, [instance], "id", errors)
cls.check_if_requestor_can_manage_users(requestor, [instance], "id", errors)
cls.check_if_removing_left_not_manageable_permissions(
diff --git a/saleor/graphql/meta/mutations.py b/saleor/graphql/meta/mutations.py
index 5a048d894e..96e28b5770 100644
--- a/saleor/graphql/meta/mutations.py
+++ b/saleor/graphql/meta/mutations.py
@@ -21,7 +21,11 @@ from ..core.types import MetadataError, NonNullList
from ..core.utils import from_global_id_or_error
from ..payment.utils import metadata_contains_empty_key
from .extra_methods import MODEL_EXTRA_METHODS, MODEL_EXTRA_PREFETCH
-from .permissions import PRIVATE_META_PERMISSION_MAP, PUBLIC_META_PERMISSION_MAP
+from .permissions import (
+ PRIVATE_META_PERMISSION_MAP,
+ PUBLIC_META_PERMISSION_MAP,
+ AccountPermissions,
+)
from .types import ObjectWithMetadata
logger = logging.getLogger(__name__)
@@ -172,6 +176,15 @@ class BaseMetadataMutation(BaseMutation):
return graphene_type._meta.model
+ @classmethod
+ def check_permissions(cls, context, permissions=None):
+ is_app = bool(getattr(context, "app", None))
+ if is_app and permissions and AccountPermissions.MANAGE_STAFF in permissions:
+ raise PermissionDenied(
+ message="Apps are not allowed to perform this mutation."
+ )
+ return super().check_permissions(context, permissions)
+
@classmethod
def mutate(cls, root, info, **data):
try:
diff --git a/saleor/graphql/meta/tests/test_meta_queries.py b/saleor/graphql/meta/tests/test_meta_queries.py
index 7099dc2d52..2eb8bc549f 100644
--- a/saleor/graphql/meta/tests/test_meta_queries.py
+++ b/saleor/graphql/meta/tests/test_meta_queries.py
@@ -174,23 +174,6 @@ def test_query_public_meta_for_staff_as_other_staff(
assert metadata["value"] == PUBLIC_VALUE
-def test_query_public_meta_for_staff_as_app(
- app_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_user.store_value_in_metadata({PUBLIC_KEY: PUBLIC_VALUE})
- admin_user.save(update_fields=["metadata"])
- variables = {"id": graphene.Node.to_global_id("User", admin_user.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_USER_PUBLIC_META, variables, [permission_manage_staff]
- )
-
- # then
- assert_no_permission(response)
-
-
QUERY_CHECKOUT_PUBLIC_META = """
query checkoutMeta($token: UUID!){
checkout(token: $token){
@@ -1967,23 +1950,6 @@ def test_query_private_meta_for_staff_as_other_staff(
assert metadata["value"] == PRIVATE_VALUE
-def test_query_private_meta_for_staff_as_app(
- app_api_client, permission_manage_staff, admin_user
-):
- # given
- admin_user.store_value_in_private_metadata({PRIVATE_KEY: PRIVATE_VALUE})
- admin_user.save(update_fields=["private_metadata"])
- variables = {"id": graphene.Node.to_global_id("User", admin_user.pk)}
-
- # when
- response = app_api_client.post_graphql(
- QUERY_USER_PRIVATE_META, variables, [permission_manage_staff]
- )
-
- # then
- assert_no_permission(response)
-
-
QUERY_CHECKOUT_PRIVATE_META = """
query checkoutMeta($token: UUID!){
checkout(token: $token){
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index acc6f0a413..8438e91382 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -13920,7 +13920,7 @@ type Mutation {
): CustomerBulkDelete
"""
- Creates a new staff user.
+ Creates a new staff user. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -13930,7 +13930,7 @@ type Mutation {
): StaffCreate
"""
- Updates an existing staff user.
+ Updates an existing staff user. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -13943,7 +13943,7 @@ type Mutation {
): StaffUpdate
"""
- Deletes a staff user.
+ Deletes a staff user. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -13953,7 +13953,7 @@ type Mutation {
): StaffDelete
"""
- Deletes staff users.
+ Deletes staff users. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -13993,7 +13993,7 @@ type Mutation {
): UserBulkSetActive
"""
- Create new permission group.
+ Create new permission group. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -14003,7 +14003,7 @@ type Mutation {
): PermissionGroupCreate
"""
- Update permission group.
+ Update permission group. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -14016,7 +14016,7 @@ type Mutation {
): PermissionGroupUpdate
"""
- Delete permission group.
+ Delete permission group. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -20704,7 +20704,7 @@ type CustomerBulkDelete {
}
"""
-Creates a new staff user.
+Creates a new staff user. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -20765,7 +20765,7 @@ input StaffCreateInput {
}
"""
-Updates an existing staff user.
+Updates an existing staff user. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -20799,7 +20799,7 @@ input StaffUpdateInput {
}
"""
-Deletes a staff user.
+Deletes a staff user. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -20810,7 +20810,7 @@ type StaffDelete {
}
"""
-Deletes staff users.
+Deletes staff users. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -20858,7 +20858,7 @@ type UserBulkSetActive {
}
"""
-Create new permission group.
+Create new permission group. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -20911,7 +20911,7 @@ input PermissionGroupCreateInput {
}
"""
-Update permission group.
+Update permission group. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""
@@ -20939,7 +20939,7 @@ input PermissionGroupUpdateInput {
}
"""
-Delete permission group.
+Delete permission group. Apps are not allowed to perform this mutation.
Requires one of the following permissions: MANAGE_STAFF.
"""