Example #1
0
    def setUp(self):
        """Set up the role viewset tests."""
        super().setUp()
        request = self.request_context['request']
        user = User(username=self.user_data['username'],
                    tenant=self.tenant)
        user.save()
        request.user = user

        sys_role_config = {
            'name': 'system_role',
            'system': True
        }

        def_role_config = {
            'name': 'default_role',
            'platform_default': True
        }

        with tenant_context(self.tenant):
            self.principal = Principal(username=self.user_data['username'])
            self.principal.save()
            self.group = Group(name='groupA')
            self.group.save()
            self.group.principals.add(self.principal)
            self.group.save()

            self.sysRole = Role(**sys_role_config)
            self.sysRole.save()

            self.defRole = Role(**def_role_config)
            self.defRole.save()
Example #2
0
    def setUp(self):
        """Set up the role viewset tests."""
        super().setUp()
        request = self.request_context['request']
        user = User()
        user.username = self.user_data['username']
        user.account = self.customer_data['account_id']
        request.user = user

        sys_role_config = {'name': 'system_role', 'system': True}

        def_role_config = {'name': 'default_role', 'platform_default': True}

        self.display_fields = {
            'applications', 'description', 'uuid', 'name', 'system', 'created',
            'policyCount', 'accessCount', 'modified', 'platform_default'
        }

        with tenant_context(self.tenant):
            self.principal = Principal(username=self.user_data['username'])
            self.principal.save()
            self.policy = Policy.objects.create(name='policyA')
            self.group = Group(name='groupA', description='groupA description')
            self.group.save()
            self.group.principals.add(self.principal)
            self.group.policies.add(self.policy)
            self.group.save()

            self.sysRole = Role(**sys_role_config)
            self.sysRole.save()

            self.defRole = Role(**def_role_config)
            self.defRole.save()
            self.defRole.save()

            self.policy.roles.add(self.defRole, self.sysRole)
            self.policy.save()

            self.access = Access.objects.create(permission='app:*:*',
                                                role=self.defRole)
Example #3
0
def add_role(request):
    username = request.session.get('username', False)
    if username is False:
        return HttpResponseRedirect('login')
    if request.is_ajax() and request.method == 'POST':
        role_name = request.POST.get('role_name')
        if role_name is None:
            result = {'status': '0', 'info': '角色名不能为空!'}
        else:
            try:
                Role.objects.get(role_name=role_name)
            except ObjectDoesNotExist:
                r = Role(role_name=role_name)
                r.create_date = datetime.datetime.now()
                r.save()
            result = {'status': '1', 'info': '新角色创建成功!'}
        return HttpResponse(json.dumps(result),
                            content_type='application/json')
    return render(request, 'role/add.html')
Example #4
0
    def setUp(self):
        """Set up the role viewset tests."""
        super().setUp()
        request = self.request_context["request"]
        user = User()
        user.username = self.user_data["username"]
        user.account = self.customer_data["account_id"]
        request.user = user

        sys_role_config = {
            "name": "system_role",
            "display_name": "system_display",
            "system": True
        }

        def_role_config = {
            "name": "default_role",
            "display_name": "default_display",
            "platform_default": True
        }

        self.display_fields = {
            "applications",
            "description",
            "uuid",
            "name",
            "display_name",
            "system",
            "created",
            "policyCount",
            "accessCount",
            "modified",
            "platform_default",
        }

        with tenant_context(self.tenant):
            self.principal = Principal(username=self.user_data["username"])
            self.principal.save()
            self.policy = Policy.objects.create(name="policyA")
            self.group = Group(name="groupA", description="groupA description")
            self.group.save()
            self.group.principals.add(self.principal)
            self.group.policies.add(self.policy)
            self.group.save()

            self.sysRole = Role(**sys_role_config)
            self.sysRole.save()

            self.defRole = Role(**def_role_config)
            self.defRole.save()
            self.defRole.save()

            self.policy.roles.add(self.defRole, self.sysRole)
            self.policy.save()

            self.permission = Permission.objects.create(permission="app:*:*")
            self.permission2 = Permission.objects.create(permission="app2:*:*")
            self.access = Access.objects.create(permission=self.permission,
                                                role=self.defRole)
            self.access2 = Access.objects.create(permission=self.permission2,
                                                 role=self.defRole)

            self.access3 = Access.objects.create(permission=self.permission2,
                                                 role=self.sysRole)
            Permission.objects.create(permission="cost-management:*:*")

        # Create permission in public schema
        with tenant_context(Tenant.objects.get(schema_name="public")):
            Permission.objects.create(permission="cost-management:*:*")
            Permission.objects.create(permission="app:*:*")
            Permission.objects.create(permission="app2:*:*")