2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-11-15 08:24:53 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2022-11-15 09:49:28 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-12-02 05:15:03 +00:00
|
|
|
from rest_framework.request import Request
|
|
|
|
from rest_framework.response import Response
|
2020-09-27 08:02:44 +00:00
|
|
|
|
2022-12-02 05:15:03 +00:00
|
|
|
from assets.api.asset.asset import AssetFilterSet
|
|
|
|
from assets.api.mixin import SerializeToTreeNodeMixin
|
|
|
|
from common.exceptions import JMSObjectDoesNotExist
|
2021-02-05 05:29:29 +00:00
|
|
|
from common.http import is_true
|
2022-11-15 09:49:28 +00:00
|
|
|
from common.utils import is_uuid
|
2022-12-02 05:15:03 +00:00
|
|
|
from perms import serializers
|
2022-08-23 02:23:48 +00:00
|
|
|
from perms.utils.user_permission import UserGrantedTreeRefreshController
|
2022-11-15 08:24:53 +00:00
|
|
|
from rbac.permissions import RBACPermission
|
2022-11-11 07:04:31 +00:00
|
|
|
from users.models import User
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
|
2022-09-13 07:41:39 +00:00
|
|
|
class RebuildTreeMixin:
|
2021-02-05 05:29:29 +00:00
|
|
|
user: User
|
2020-08-16 15:08:58 +00:00
|
|
|
|
2021-02-25 06:45:21 +00:00
|
|
|
def get(self, request: Request, *args, **kwargs):
|
2021-02-05 05:29:29 +00:00
|
|
|
force = is_true(request.query_params.get('rebuild_tree'))
|
|
|
|
controller = UserGrantedTreeRefreshController(self.user)
|
|
|
|
controller.refresh_if_need(force)
|
|
|
|
return super().get(request, *args, **kwargs)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2019-12-25 02:29:58 +00:00
|
|
|
|
2022-11-15 08:24:53 +00:00
|
|
|
class SelfOrPKUserMixin:
|
|
|
|
kwargs: dict
|
|
|
|
request: Request
|
|
|
|
permission_classes = (RBACPermission,)
|
|
|
|
|
2022-11-15 09:49:28 +00:00
|
|
|
def get_rbac_perms(self):
|
|
|
|
if self.request_user_is_self():
|
|
|
|
return self.self_rbac_perms
|
|
|
|
else:
|
|
|
|
return self.admin_rbac_perms
|
|
|
|
|
2022-11-15 08:24:53 +00:00
|
|
|
@property
|
|
|
|
def self_rbac_perms(self):
|
|
|
|
return (
|
|
|
|
('list', 'perms.view_myassets'),
|
|
|
|
('retrieve', 'perms.view_myassets'),
|
|
|
|
('get_tree', 'perms.view_myassets'),
|
|
|
|
('GET', 'perms.view_myassets'),
|
2022-12-01 04:02:20 +00:00
|
|
|
('OPTIONS', 'perms.view_myassets'),
|
2022-11-15 08:24:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def admin_rbac_perms(self):
|
|
|
|
return (
|
|
|
|
('list', 'perms.view_userassets'),
|
|
|
|
('retrieve', 'perms.view_userassets'),
|
|
|
|
('get_tree', 'perms.view_userassets'),
|
|
|
|
('GET', 'perms.view_userassets'),
|
2022-12-01 04:02:20 +00:00
|
|
|
('OPTIONS', 'perms.view_userassets'),
|
2022-11-15 08:24:53 +00:00
|
|
|
)
|
|
|
|
|
2022-11-15 09:49:28 +00:00
|
|
|
@property
|
|
|
|
def user(self):
|
2022-11-15 08:24:53 +00:00
|
|
|
if self.request_user_is_self():
|
2022-11-15 09:49:28 +00:00
|
|
|
user = self.request.user
|
|
|
|
elif is_uuid(self.kwargs.get('user')):
|
|
|
|
user = get_object_or_404(User, pk=self.kwargs.get('user'))
|
2022-12-01 04:02:20 +00:00
|
|
|
elif hasattr(self, 'swagger_fake_view'):
|
|
|
|
user = self.request.user
|
2022-11-15 08:24:53 +00:00
|
|
|
else:
|
2022-11-15 09:49:28 +00:00
|
|
|
raise JMSObjectDoesNotExist(object_name=_('User'))
|
|
|
|
return user
|
2022-11-15 08:24:53 +00:00
|
|
|
|
|
|
|
def request_user_is_self(self):
|
|
|
|
return self.kwargs.get('user') in ['my', 'self']
|
2022-12-02 05:15:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PermedAssetSerializerMixin:
|
|
|
|
serializer_class = serializers.AssetGrantedSerializer
|
|
|
|
filterset_class = AssetFilterSet
|
|
|
|
search_fields = ['name', 'address', 'comment']
|
|
|
|
ordering_fields = ("name", "address")
|
|
|
|
ordering = ('name',)
|
|
|
|
|
|
|
|
|
|
|
|
class AssetsTreeFormatMixin(SerializeToTreeNodeMixin):
|
|
|
|
"""
|
|
|
|
将 资产 序列化成树的结构返回
|
|
|
|
"""
|
|
|
|
filter_queryset: callable
|
|
|
|
get_queryset: callable
|
|
|
|
|
|
|
|
filterset_fields = ['name', 'address', 'id', 'comment']
|
|
|
|
search_fields = ['name', 'address', 'comment']
|
|
|
|
|
|
|
|
def list(self, request: Request, *args, **kwargs):
|
|
|
|
queryset = self.filter_queryset(self.get_queryset())
|
|
|
|
|
|
|
|
if request.query_params.get('search'):
|
|
|
|
# 如果用户搜索的条件不精准,会导致返回大量的无意义数据。
|
|
|
|
# 这里限制一下返回数据的最大条数
|
|
|
|
queryset = queryset[:999]
|
|
|
|
queryset = sorted(queryset, key=lambda asset: asset.name)
|
|
|
|
data = self.serialize_assets(queryset, None)
|
|
|
|
return Response(data=data)
|