Skip to content

Commit

Permalink
Drop graphql_jwt.PermissionDenied
Browse files Browse the repository at this point in the history
  • Loading branch information
Maciej Korycinski committed Jun 4, 2020
1 parent 865755e commit 6216fc0
Show file tree
Hide file tree
Showing 18 changed files with 44 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions saleor/core/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ def __init__(self, context=None):
super().__init__("Can't add unpublished product.")
self.context = context
self.code = CheckoutErrorCode.PRODUCT_NOT_PUBLISHED


class PermissionDenied(Exception):
def __init__(self, message=None):
default_message = "You do not have permission to perform this action"
if message is None:
message = default_message
super().__init__(message)
2 changes: 1 addition & 1 deletion saleor/graphql/account/mutations/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from django.contrib.auth.tokens import default_token_generator
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import transaction
from graphql_jwt.exceptions import PermissionDenied

from ....account import events as account_events, models
from ....account.emails import (
send_set_password_email_with_url,
send_user_password_reset_email_with_url,
)
from ....account.error_codes import AccountErrorCode
from ....core.exceptions import PermissionDenied
from ....core.permissions import AccountPermissions
from ....core.utils.url import validate_storefront_url
from ....order.utils import match_orders_with_new_user
Expand Down
4 changes: 2 additions & 2 deletions saleor/graphql/account/mutations/staff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import graphene
from django.core.exceptions import ValidationError
from django.db import transaction
from graphql_jwt.decorators import staff_member_required
from graphql_jwt.exceptions import PermissionDenied

from ....account import events as account_events, models, utils
from ....account.emails import send_set_password_email_with_url
from ....account.error_codes import AccountErrorCode
from ....account.thumbnails import create_user_avatar_thumbnails
from ....account.utils import remove_staff_member
from ....checkout import AddressType
from ....core.exceptions import PermissionDenied
from ....core.permissions import AccountPermissions
from ....core.utils.url import validate_storefront_url
from ...account.enums import AddressTypeEnum
Expand All @@ -21,6 +20,7 @@
from ...core.types import Upload
from ...core.types.common import AccountError, StaffError
from ...core.utils import get_duplicates_ids, validate_image_file
from ...decorators import staff_member_required
from ...meta.deprecated.mutations import ClearMetaBaseMutation, UpdateMetaBaseMutation
from ..utils import (
CustomerDeleteMixin,
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/account/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

import graphene
from django.contrib.auth import models as auth_models
from graphql_jwt.exceptions import PermissionDenied
from i18naddress import get_validation_rules

from ...account import models
from ...core.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions
from ...payment import gateway
from ...payment.utils import fetch_customer_id
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/account/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from django.contrib.auth import get_user_model, models as auth_models
from graphene import relay
from graphene_federation import key
from graphql_jwt.exceptions import PermissionDenied

from ...account import models
from ...checkout.utils import get_user_checkout
from ...core.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions, OrderPermissions
from ...order import models as order_models
from ..checkout.types import Checkout
Expand Down
3 changes: 1 addition & 2 deletions saleor/graphql/checkout/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import transaction
from django.db.models import Prefetch
from graphql_jwt.exceptions import PermissionDenied

from ...account.error_codes import AccountErrorCode
from ...checkout import models
Expand All @@ -24,7 +23,7 @@
remove_promo_code_from_checkout,
)
from ...core import analytics
from ...core.exceptions import InsufficientStock, ProductNotPublished
from ...core.exceptions import InsufficientStock, PermissionDenied, ProductNotPublished
from ...core.permissions import OrderPermissions
from ...core.taxes import TaxError
from ...core.utils.url import validate_storefront_url
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/checkout/types.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import graphene
from graphql_jwt.exceptions import PermissionDenied
from promise import Promise

from ...checkout import calculations, models
from ...checkout.utils import get_valid_shipping_methods_for_checkout
from ...core.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions, CheckoutPermissions
from ...core.taxes import display_gross_prices, zero_taxed_money
from ...plugins.manager import get_plugins_manager
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/core/mutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
from graphene.types.mutation import MutationOptions
from graphene_django.registry import get_global_registry
from graphql.error import GraphQLError
from graphql_jwt.exceptions import PermissionDenied

from ...core.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions
from ..utils import get_nodes
from .types import Error, Upload
Expand Down
22 changes: 19 additions & 3 deletions saleor/graphql/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
from functools import wraps
from typing import Iterable, Union

from graphql_jwt import exceptions
from graphql_jwt.decorators import context
from graphql.execution.base import ResolveInfo

from ..core.exceptions import PermissionDenied
from ..core.permissions import AccountPermissions


def context(f):
def decorator(func):
def wrapper(*args, **kwargs):
info = next(arg for arg in args if isinstance(arg, ResolveInfo))
return func(info.context, *args, **kwargs)

return wrapper

return decorator


def account_passes_test(test_func):
"""Determine if user/app has permission to access to content."""

Expand All @@ -17,7 +28,7 @@ def decorator(f):
def wrapper(context, *args, **kwargs):
if test_func(context):
return f(*args, **kwargs)
raise exceptions.PermissionDenied()
raise PermissionDenied()

return wrapper

Expand Down Expand Up @@ -56,3 +67,8 @@ def check_perms(context):
return False

return account_passes_test(check_perms)


staff_member_required = account_passes_test(
lambda context: context.user.is_active and context.user.is_staff
)
2 changes: 1 addition & 1 deletion saleor/graphql/giftcard/types.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import graphene
from graphql_jwt.exceptions import PermissionDenied

from ...core.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions, GiftcardPermissions
from ...giftcard import models
from ..core.connection import CountableDjangoObjectType
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/meta/mutations.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import graphene
from django.core.exceptions import ValidationError
from graphql_jwt.exceptions import PermissionDenied

from ...core import models
from ...core.error_codes import MetadataErrorCode
from ...core.exceptions import PermissionDenied
from ..core.mutations import BaseMutation
from ..core.types.common import MetadataError
from .permissions import PRIVATE_META_PERMISSION_MAP, PUBLIC_META_PERMISSION_MAP
Expand Down
3 changes: 1 addition & 2 deletions saleor/graphql/meta/permissions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import Any, List

from graphql_jwt.exceptions import PermissionDenied

from ...account import models as account_models
from ...core.exceptions import PermissionDenied
from ...core.permissions import (
AccountPermissions,
AppPermission,
Expand Down
3 changes: 1 addition & 2 deletions saleor/graphql/meta/resolvers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from operator import itemgetter

from graphql_jwt.exceptions import PermissionDenied

from ...account import models as account_models
from ...app import models as app_models
from ...checkout import models as checkout_models
from ...core.exceptions import PermissionDenied
from ...core.models import ModelWithMetadata
from ...order import models as order_models
from ...product import models as product_models
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/order/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import graphene
from django.core.exceptions import ValidationError
from graphene import relay
from graphql_jwt.exceptions import PermissionDenied

from ...core.exceptions import PermissionDenied
from ...core.permissions import AccountPermissions, OrderPermissions
from ...core.taxes import display_gross_prices
from ...order import OrderStatus, models
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/product/mutations/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from django.db.models import Q, QuerySet
from django.template.defaultfilters import slugify
from graphene.types import InputObjectType
from graphql_jwt.exceptions import PermissionDenied
from graphql_relay import from_global_id

from ....core.exceptions import PermissionDenied
from ....core.permissions import ProductPermissions
from ....product import models
from ....product.error_codes import ProductErrorCode
Expand Down
3 changes: 1 addition & 2 deletions saleor/graphql/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
format_error as format_graphql_error,
)
from graphql.execution import ExecutionResult
from graphql_jwt.exceptions import PermissionDenied
from jwt.exceptions import PyJWTError

from ..core.exceptions import ReadOnlyException
from ..core.exceptions import PermissionDenied, ReadOnlyException
from ..core.utils import is_valid_ipv4, is_valid_ipv6

API_PATH = SimpleLazyObject(lambda: reverse("api"))
Expand Down
2 changes: 1 addition & 1 deletion saleor/graphql/webhook/resolvers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import graphene
from graphql_jwt.exceptions import PermissionDenied

from ...core.exceptions import PermissionDenied
from ...core.permissions import WebhookPermissions
from ...webhook import models, payloads
from ...webhook.event_types import WebhookEventType
Expand Down

0 comments on commit 6216fc0

Please sign in to comment.