2023-02-07 08:21:26 +00:00
|
|
|
from django.db.models import Q, Count
|
2022-02-17 12:13:31 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from rest_framework.decorators import action
|
2023-02-03 03:14:24 +00:00
|
|
|
from rest_framework.exceptions import PermissionDenied
|
2022-02-17 12:13:31 +00:00
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.api import JMSModelViewSet
|
2023-02-07 08:21:26 +00:00
|
|
|
from orgs.utils import current_org
|
2023-02-03 03:14:24 +00:00
|
|
|
from .permission import PermissionViewSet
|
2022-04-18 06:52:41 +00:00
|
|
|
from ..filters import RoleFilter
|
2023-02-07 08:21:26 +00:00
|
|
|
from ..models import Role, SystemRole, OrgRole, RoleBinding
|
2023-02-03 03:14:24 +00:00
|
|
|
from ..serializers import RoleSerializer, RoleUserSerializer
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2022-03-04 09:43:57 +00:00
|
|
|
'RoleViewSet', 'SystemRoleViewSet', 'OrgRoleViewSet',
|
|
|
|
'SystemRolePermissionsViewSet', 'OrgRolePermissionsViewSet',
|
2022-02-17 12:13:31 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-08-04 02:44:11 +00:00
|
|
|
class RoleViewSet(JMSModelViewSet):
|
2022-02-17 12:13:31 +00:00
|
|
|
queryset = Role.objects.all()
|
|
|
|
serializer_classes = {
|
|
|
|
'default': RoleSerializer,
|
|
|
|
'users': RoleUserSerializer,
|
|
|
|
}
|
2023-02-23 11:38:40 +00:00
|
|
|
ordering = ('-builtin', 'name')
|
2022-04-18 06:52:41 +00:00
|
|
|
filterset_class = RoleFilter
|
|
|
|
search_fields = ('name', 'scope', 'builtin')
|
2022-02-17 12:13:31 +00:00
|
|
|
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)
|
2023-02-07 08:21:26 +00:00
|
|
|
|
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)
|
|
|
|
|
2022-06-06 07:51:17 +00:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
super(RoleViewSet, self).perform_create(serializer)
|
|
|
|
self.set_permissions_if_need(serializer.instance)
|
|
|
|
|
|
|
|
def set_permissions_if_need(self, instance):
|
|
|
|
if not isinstance(instance, Role):
|
|
|
|
return
|
|
|
|
clone_from = self.request.query_params.get('clone_from')
|
|
|
|
if not clone_from:
|
|
|
|
return
|
|
|
|
clone = Role.objects.filter(id=clone_from).first()
|
|
|
|
if not clone:
|
|
|
|
return
|
2023-02-03 03:14:24 +00:00
|
|
|
instance.permissions.set(clone.get_permissions())
|
2022-06-06 07:51:17 +00:00
|
|
|
|
2023-02-23 11:38:40 +00:00
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
queryset = queryset.order_by(*self.ordering)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def set_users_amount(self, queryset):
|
2023-02-07 08:21:26 +00:00
|
|
|
"""设置角色的用户绑定数量,以减少查询"""
|
2023-02-22 12:30:43 +00:00
|
|
|
ids = [role.id for role in queryset]
|
2023-02-23 11:38:40 +00:00
|
|
|
queryset = Role.objects.filter(id__in=ids).order_by(*self.ordering)
|
2023-02-07 08:21:26 +00:00
|
|
|
org_id = current_org.id
|
|
|
|
q = Q(role__scope=Role.Scope.system) | Q(role__scope=Role.Scope.org, org_id=org_id)
|
|
|
|
role_bindings = RoleBinding.objects.filter(q).values_list('role_id').annotate(user_count=Count('user_id'))
|
|
|
|
role_user_amount_mapper = {role_id: user_count for role_id, user_count in role_bindings}
|
|
|
|
queryset = queryset.annotate(permissions_amount=Count('permissions'))
|
|
|
|
queryset = list(queryset)
|
|
|
|
for role in queryset:
|
|
|
|
role.users_amount = role_user_amount_mapper.get(role.id, 0)
|
|
|
|
return queryset
|
|
|
|
|
2023-02-22 12:30:43 +00:00
|
|
|
def get_serializer(self, *args, **kwargs):
|
2023-02-22 14:20:11 +00:00
|
|
|
if len(args) == 1 and kwargs.get('many', False):
|
2023-02-22 12:30:43 +00:00
|
|
|
queryset = self.set_users_amount(args[0])
|
|
|
|
args = (queryset,)
|
|
|
|
return super().get_serializer(*args, **kwargs)
|
2023-02-07 08:21:26 +00:00
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
@action(methods=['GET'], detail=True)
|
|
|
|
def users(self, *args, **kwargs):
|
|
|
|
role = self.get_object()
|
|
|
|
queryset = role.users
|
2022-05-07 02:40:12 +00:00
|
|
|
return self.get_paginated_response_from_queryset(queryset)
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SystemRoleViewSet(RoleViewSet):
|
2023-02-14 06:17:12 +00:00
|
|
|
perm_model = SystemRole
|
|
|
|
|
2023-02-07 08:21:26 +00:00
|
|
|
def get_queryset(self):
|
2023-02-23 11:38:40 +00:00
|
|
|
qs = super().get_queryset().filter(scope='system')
|
|
|
|
return qs
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrgRoleViewSet(RoleViewSet):
|
2023-02-14 06:17:12 +00:00
|
|
|
perm_model = OrgRole
|
|
|
|
|
2023-02-07 08:21:26 +00:00
|
|
|
def get_queryset(self):
|
2023-02-23 11:38:40 +00:00
|
|
|
qs = super().get_queryset().filter(scope='org')
|
|
|
|
return qs
|
2022-02-17 12:13:31 +00:00
|
|
|
|
|
|
|
|
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 = (
|
2022-04-20 11:58:39 +00:00
|
|
|
('get_tree', 'rbac.view_permission'),
|
2022-03-04 09:43:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Sub view set
|
|
|
|
class OrgRolePermissionsViewSet(BaseRolePermissionsViewSet):
|
|
|
|
role_pk = 'org_role_pk'
|
|
|
|
model = OrgRole
|
|
|
|
rbac_perms = (
|
2022-04-20 11:58:39 +00:00
|
|
|
('get_tree', 'rbac.view_permission'),
|
2022-03-04 09:43:57 +00:00
|
|
|
)
|