2022-02-17 12:13:31 +00:00
|
|
|
from django.db.models import Count
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from rest_framework.exceptions import PermissionDenied
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
|
|
|
|
from common.drf.api import JMSModelViewSet
|
|
|
|
from ..serializers import RoleSerializer, RoleUserSerializer
|
|
|
|
from ..models import Role, SystemRole, OrgRole
|
|
|
|
from .permission import PermissionViewSet
|
|
|
|
|
|
|
|
__all__ = [
|
2022-03-04 09:43:57 +00:00
|
|
|
'RoleViewSet', 'SystemRoleViewSet', 'OrgRoleViewSet',
|
|
|
|
'SystemRolePermissionsViewSet', 'OrgRolePermissionsViewSet',
|
2022-02-17 12:13:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class RoleViewSet(JMSModelViewSet):
|
|
|
|
queryset = Role.objects.all()
|
|
|
|
serializer_classes = {
|
|
|
|
'default': RoleSerializer,
|
|
|
|
'users': RoleUserSerializer,
|
|
|
|
}
|
|
|
|
filterset_fields = ['name', 'scope', 'builtin']
|
|
|
|
search_fields = filterset_fields
|
|
|
|
rbac_perms = {
|
|
|
|
'users': 'rbac.view_rolebinding'
|
|
|
|
}
|
|
|
|
|
|
|
|
def perform_destroy(self, instance):
|
2022-03-08 12:00:19 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2022-02-17 12:13:31 +00:00
|
|
|
if instance.builtin:
|
|
|
|
error = _("Internal role, can't be destroy")
|
|
|
|
raise PermissionDenied(error)
|
2022-03-08 12:00:19 +00:00
|
|
|
with tmp_to_root_org():
|
|
|
|
if instance.users.count() >= 1:
|
|
|
|
error = _("The role has been bound to users, can't be destroy")
|
|
|
|
raise PermissionDenied(error)
|
2022-02-17 12:13:31 +00:00
|
|
|
return super().perform_destroy(instance)
|
|
|
|
|
|
|
|
def perform_update(self, serializer):
|
|
|
|
instance = serializer.instance
|
|
|
|
if instance.builtin:
|
|
|
|
error = _("Internal role, can't be update")
|
|
|
|
raise PermissionDenied(error)
|
|
|
|
return super().perform_update(serializer)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-03-04 09:43:57 +00:00
|
|
|
queryset = super().get_queryset() \
|
2022-02-17 12:13:31 +00:00
|
|
|
.annotate(permissions_amount=Count('permissions'))
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
@action(methods=['GET'], detail=True)
|
|
|
|
def users(self, *args, **kwargs):
|
|
|
|
role = self.get_object()
|
|
|
|
queryset = role.users
|
|
|
|
return self.get_paginated_response_with_query_set(queryset)
|
|
|
|
|
|
|
|
|
|
|
|
class SystemRoleViewSet(RoleViewSet):
|
|
|
|
queryset = SystemRole.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class OrgRoleViewSet(RoleViewSet):
|
|
|
|
queryset = OrgRole.objects.all()
|
|
|
|
|
|
|
|
|
2022-03-04 09:43:57 +00:00
|
|
|
class BaseRolePermissionsViewSet(PermissionViewSet):
|
|
|
|
model = None
|
|
|
|
role_pk = None
|
2022-02-21 10:51:11 +00:00
|
|
|
filterset_fields = []
|
2022-02-17 12:13:31 +00:00
|
|
|
http_method_names = ['get', 'option']
|
|
|
|
check_disabled = False
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-03-04 09:43:57 +00:00
|
|
|
role_id = self.kwargs.get(self.role_pk)
|
2022-02-21 10:51:11 +00:00
|
|
|
if not role_id:
|
2022-03-04 09:43:57 +00:00
|
|
|
return self.model.objects.none()
|
2022-02-21 10:51:11 +00:00
|
|
|
|
2022-03-04 09:43:57 +00:00
|
|
|
role = self.model.objects.get(id=role_id)
|
2022-02-17 12:13:31 +00:00
|
|
|
self.scope = role.scope
|
|
|
|
self.check_disabled = role.builtin
|
2022-03-04 09:43:57 +00:00
|
|
|
queryset = role.get_permissions() \
|
2022-02-17 12:13:31 +00:00
|
|
|
.prefetch_related('content_type')
|
|
|
|
return queryset
|
2022-03-04 09:43:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Sub view set
|
|
|
|
class SystemRolePermissionsViewSet(BaseRolePermissionsViewSet):
|
|
|
|
role_pk = 'system_role_pk'
|
|
|
|
model = SystemRole
|
|
|
|
rbac_perms = (
|
|
|
|
('get_tree', 'rbac.view_systemrole'),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Sub view set
|
|
|
|
class OrgRolePermissionsViewSet(BaseRolePermissionsViewSet):
|
|
|
|
role_pk = 'org_role_pk'
|
|
|
|
model = OrgRole
|
|
|
|
rbac_perms = (
|
|
|
|
('get_tree', 'rbac.view_orgrole'),
|
|
|
|
)
|
|
|
|
|