2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 20:16:44 +00:00

refactor 'all' role

This commit is contained in:
Matthias Mair 2022-05-20 17:43:51 +02:00
parent 9f922f06ff
commit a148dbf303
7 changed files with 21 additions and 17 deletions

View File

@ -49,32 +49,38 @@ class UserMixin:
self.user.save() self.user.save()
# Assign all roles if set
if self.roles == 'all':
self.assignRole()
# else filter the roles
else:
for role in self.roles: for role in self.roles:
self.assignRole(role, self.roles == ['all']) self.assignRole(role)
if self.auto_login: if self.auto_login:
self.client.login(username=self.username, password=self.password) self.client.login(username=self.username, password=self.password)
def assignRole(self, role, assign_all: bool = False): def assignRole(self, role = None, assign_all: bool = False):
""" """
Set the user roles for the registered user Set the user roles for the registered user
""" """
# role is of the format 'rule.permission' e.g. 'part.add' # role is of the format 'rule.permission' e.g. 'part.add'
if not assign_all:
rule, perm = role.split('.') rule, perm = role.split('.')
for ruleset in self.group.rule_sets.all(): for ruleset in self.group.rule_sets.all():
if ruleset.name == rule or assign_all: if assign_all or ruleset.name == rule:
if perm == 'view' or assign_all: if assign_all or perm == 'view':
ruleset.can_view = True ruleset.can_view = True
elif perm == 'change' or assign_all: elif assign_all or perm == 'change':
ruleset.can_change = True ruleset.can_change = True
elif perm == 'delete' or assign_all: elif assign_all or perm == 'delete':
ruleset.can_delete = True ruleset.can_delete = True
elif perm == 'add' or assign_all: elif assign_all or perm == 'add':
ruleset.can_add = True ruleset.can_add = True
ruleset.save() ruleset.save()

View File

@ -20,7 +20,7 @@ class HTMLAPITests(InvenTreeTestCase):
which raised an AssertionError when using the HTML API interface, which raised an AssertionError when using the HTML API interface,
while the regular JSON interface continued to work as expected. while the regular JSON interface continued to work as expected.
""" """
roles = ['all'] roles = 'all'
def test_part_api(self): def test_part_api(self):
url = reverse('api-part-list') url = reverse('api-part-list')

View File

@ -16,9 +16,7 @@ class CompanyViewTestBase(InvenTreeTestCase):
'supplier_part', 'supplier_part',
] ]
roles = [ roles = 'all'
'all',
]
class CompanyViewTest(CompanyViewTestBase): class CompanyViewTest(CompanyViewTestBase):

View File

@ -18,7 +18,7 @@ class BomExportTest(InvenTreeTestCase):
'bom', 'bom',
] ]
roles = ['all'] roles = 'all'
def setUp(self): def setUp(self):
super().setUp() super().setUp()

View File

@ -17,7 +17,7 @@ class PartViewTestCase(TestCase):
'supplier_part', 'supplier_part',
] ]
roles = ['all'] roles = 'all'
def setUp(self): def setUp(self):
super().setUp() super().setUp()

View File

@ -260,7 +260,7 @@ class PanelMixinTests(TestCase):
'stock', 'stock',
] ]
roles = ['all'] roles = 'all'
def test_installed(self): def test_installed(self):
"""Test that the sample panel plugin is installed""" """Test that the sample panel plugin is installed"""

View File

@ -18,7 +18,7 @@ class StockViewTestCase(InvenTreeTestCase):
'stock', 'stock',
] ]
roles = ['all'] roles = 'all'
class StockListTest(StockViewTestCase): class StockListTest(StockViewTestCase):