Skip to content

Commit 8bc722f

Browse files
Implemented permisions in all mutatation
1 parent 3c8871b commit 8bc722f

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

nxtbn/core/admin_mutation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import graphene
33

44
from nxtbn.core import CurrencyTypes
5+
from nxtbn.core.admin_permissions import gql_required_perm
56
from nxtbn.core.admin_types import CurrencyExchangeType
67
from nxtbn.core.models import CurrencyExchange
78
from nxtbn.users import UserRole
@@ -20,6 +21,7 @@ class Arguments:
2021

2122
currency_exchange = graphene.Field(CurrencyExchangeType)
2223

24+
@gql_required_perm(CurrencyExchange, 'add_currencyexchange')
2325
@staticmethod
2426
def mutate(root, info, input):
2527
# Validate base_currency
@@ -47,6 +49,7 @@ class Arguments:
4749

4850
currency_exchange = graphene.Field(CurrencyExchangeType)
4951

52+
@gql_required_perm(CurrencyExchange, 'change_currencyexchange')
5053
@staticmethod
5154
def mutate(root, info, id, input):
5255
try:
@@ -64,6 +67,7 @@ class Arguments:
6467

6568
success = graphene.Boolean() # Indicate whether the operation was successful
6669

70+
@gql_required_perm(CurrencyExchange, 'delete_currencyexchange')
6771
@staticmethod
6872
def mutate(root, info, id):
6973
try:

nxtbn/core/admin_permissions.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def has_required_perm(user, code: str, model_cls=None):
150150
return user.has_perm(perm_code)
151151

152152

153-
def gql_required_perm(code: str): # Used in graphql only
153+
def gql_required_perm(model, code: str): # model argument will be the model class
154154
def decorator(func):
155155
@functools.wraps(func)
156156
def wrapper(self, info, *args, **kwargs):
@@ -169,13 +169,12 @@ def wrapper(self, info, *args, **kwargs):
169169
if user.is_store_admin:
170170
return func(self, info, *args, **kwargs)
171171

172-
173172
if operation == "query":
174173
return func(self, info, *args, **kwargs)
175-
176-
177174

178-
if not user.has_perm(code): # Check if user has the required permission
175+
# Check if the user has permission for the model
176+
perm_code = f"{model._meta.app_label}.{code}" # Constructing the permission name
177+
if not user.has_perm(perm_code): # Check if the user has the required permission for the model
179178
raise GraphQLError("Permission denied") # Block unauthorized access
180179

181180
return func(self, info, *args, **kwargs) # Call the actual resolver

nxtbn/product/admin_mutations.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import graphene
2+
from nxtbn.core.admin_permissions import gql_required_perm
23
from nxtbn.product.admin_types import CategoryTranslationType, CategoryType, CollectionTranslationType, ProductTagTranslationType, ProductTranslationType, ProductVariantTranslationType, SupplierTranslationType
34
from nxtbn.product.models import Category, CategoryTranslation, CollectionTranslation, ProductTagTranslation, ProductTranslation, ProductVariantTranslation, SupplierTranslation
45
from nxtbn.users import UserRole
@@ -18,6 +19,7 @@ class Arguments:
1819

1920
category = graphene.Field(CategoryType)
2021

22+
@gql_required_perm(Category, 'change_category')
2123
def mutate(self, info, id, input):
2224
category = Category.objects.get(id=id)
2325
category.name = input.name
@@ -45,6 +47,7 @@ class Arguments:
4547

4648
product_translation = graphene.Field(ProductTranslationType)
4749

50+
@gql_required_perm(ProductTranslation, 'change_producttranslation')
4851
def mutate(self, info, base_product_id, lang_code, name, summary, description, meta_title, meta_description):
4952
try:
5053
product_translation = ProductTranslation.objects.get(product_id=base_product_id, language_code=lang_code)
@@ -72,6 +75,7 @@ class Arguments:
7275

7376
category_translation = graphene.Field(CategoryTranslationType)
7477

78+
@gql_required_perm(CategoryTranslation, 'change_categorytranslation')
7579
def mutate(self, info, base_category_id, lang_code, name, description, meta_title, meta_description):
7680
try:
7781
category_translation = CategoryTranslation.objects.get(category_id=base_category_id, language_code=lang_code)
@@ -98,6 +102,7 @@ class Arguments:
98102

99103
supplier_translation = graphene.Field(SupplierTranslationType)
100104

105+
@gql_required_perm(SupplierTranslation, 'change_suppliertranslation')
101106
def mutate(self, info, base_supplier_id, lang_code, name, description, meta_title, meta_description):
102107
try:
103108
supplier_translation = SupplierTranslation.objects.get(supplier_id=base_supplier_id, language_code=lang_code)
@@ -121,6 +126,7 @@ class Arguments:
121126

122127
product_variant_translation = graphene.Field(ProductVariantTranslationType)
123128

129+
@gql_required_perm(ProductVariantTranslation, 'change_productvarianttranslation')
124130
def mutate(self, info, base_product_variant_id, lang_code, name, description, meta_title, meta_description):
125131
try:
126132
product_variant_translation = ProductVariantTranslation.objects.get(product_variant_id=base_product_variant_id, language_code=lang_code)
@@ -140,6 +146,7 @@ class Arguments:
140146

141147
product_tag_translation = graphene.Field(ProductTagTranslationType)
142148

149+
@gql_required_perm(ProductTagTranslation, 'change_producttagtranslation')
143150
def mutate(self, info, base_product_tag_id, lang_code, name, description, meta_title, meta_description):
144151
try:
145152
product_tag_translation = ProductTagTranslation.objects.get(product_tag_id=base_product_tag_id, language_code=lang_code)
@@ -162,6 +169,7 @@ class Arguments:
162169

163170
collection_translation = graphene.Field(CollectionTranslationType)
164171

172+
@gql_required_perm(CollectionTranslation, 'change_collectiontranslation')
165173
def mutate(self, info, base_collection_id, lang_code, name, description, meta_title, meta_description):
166174
try:
167175
collection_translation = CollectionTranslation.objects.get(collection_id=base_collection_id, language_code=lang_code)

0 commit comments

Comments
 (0)