mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-04 15:15:42 +00:00 
			
		
		
		
	refactor 'all' role
This commit is contained in:
		@@ -49,32 +49,38 @@ class UserMixin:
 | 
			
		||||
 | 
			
		||||
        self.user.save()
 | 
			
		||||
 | 
			
		||||
        for role in self.roles:
 | 
			
		||||
            self.assignRole(role, self.roles == ['all'])
 | 
			
		||||
        # Assign all roles if set
 | 
			
		||||
        if self.roles == 'all':
 | 
			
		||||
            self.assignRole()
 | 
			
		||||
        # else filter the roles
 | 
			
		||||
        else:
 | 
			
		||||
            for role in self.roles:
 | 
			
		||||
                self.assignRole(role)
 | 
			
		||||
 | 
			
		||||
        if self.auto_login:
 | 
			
		||||
            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
 | 
			
		||||
        """
 | 
			
		||||
 | 
			
		||||
        # role is of the format 'rule.permission' e.g. 'part.add'
 | 
			
		||||
 | 
			
		||||
        rule, perm = role.split('.')
 | 
			
		||||
        if not assign_all:
 | 
			
		||||
            rule, perm = role.split('.')
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
                elif perm == 'change' or assign_all:
 | 
			
		||||
                elif assign_all or perm == 'change':
 | 
			
		||||
                    ruleset.can_change = True
 | 
			
		||||
                elif perm == 'delete' or assign_all:
 | 
			
		||||
                elif assign_all or perm == 'delete':
 | 
			
		||||
                    ruleset.can_delete = True
 | 
			
		||||
                elif perm == 'add' or assign_all:
 | 
			
		||||
                elif assign_all or perm == 'add':
 | 
			
		||||
                    ruleset.can_add = True
 | 
			
		||||
 | 
			
		||||
                ruleset.save()
 | 
			
		||||
 
 | 
			
		||||
@@ -20,7 +20,7 @@ class HTMLAPITests(InvenTreeTestCase):
 | 
			
		||||
    which raised an AssertionError when using the HTML API interface,
 | 
			
		||||
    while the regular JSON interface continued to work as expected.
 | 
			
		||||
    """
 | 
			
		||||
    roles = ['all']
 | 
			
		||||
    roles = 'all'
 | 
			
		||||
 | 
			
		||||
    def test_part_api(self):
 | 
			
		||||
        url = reverse('api-part-list')
 | 
			
		||||
 
 | 
			
		||||
@@ -16,9 +16,7 @@ class CompanyViewTestBase(InvenTreeTestCase):
 | 
			
		||||
        'supplier_part',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    roles = [
 | 
			
		||||
        'all',
 | 
			
		||||
    ]
 | 
			
		||||
    roles = 'all'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class CompanyViewTest(CompanyViewTestBase):
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@ class BomExportTest(InvenTreeTestCase):
 | 
			
		||||
        'bom',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    roles = ['all']
 | 
			
		||||
    roles = 'all'
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        super().setUp()
 | 
			
		||||
 
 | 
			
		||||
@@ -17,7 +17,7 @@ class PartViewTestCase(TestCase):
 | 
			
		||||
        'supplier_part',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    roles = ['all']
 | 
			
		||||
    roles = 'all'
 | 
			
		||||
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        super().setUp()
 | 
			
		||||
 
 | 
			
		||||
@@ -260,7 +260,7 @@ class PanelMixinTests(TestCase):
 | 
			
		||||
        'stock',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    roles = ['all']
 | 
			
		||||
    roles = 'all'
 | 
			
		||||
 | 
			
		||||
    def test_installed(self):
 | 
			
		||||
        """Test that the sample panel plugin is installed"""
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@ class StockViewTestCase(InvenTreeTestCase):
 | 
			
		||||
        'stock',
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    roles = ['all']
 | 
			
		||||
    roles = 'all'
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class StockListTest(StockViewTestCase):
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user