2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from rest_framework.generics import (
|
2022-10-27 07:47:05 +00:00
|
|
|
ListAPIView, get_object_or_404
|
2022-08-23 02:26:43 +00:00
|
|
|
)
|
2022-02-17 12:13:31 +00:00
|
|
|
from common.permissions import IsValidUser
|
2020-08-16 15:08:58 +00:00
|
|
|
from common.utils import get_logger, lazyproperty
|
|
|
|
|
2022-09-23 07:59:37 +00:00
|
|
|
from perms.hands import User, Asset, Account
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms import serializers
|
2022-10-27 07:47:05 +00:00
|
|
|
from perms.models import Action
|
2022-10-18 08:42:32 +00:00
|
|
|
from perms.utils import PermAccountUtil
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
__all__ = [
|
2022-09-29 08:18:12 +00:00
|
|
|
'UserGrantedAssetAccountsApi',
|
|
|
|
'MyGrantedAssetAccountsApi',
|
|
|
|
'UserGrantedAssetSpecialAccountsApi',
|
|
|
|
'MyGrantedAssetSpecialAccountsApi',
|
2019-09-17 04:34:47 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-09-29 08:18:12 +00:00
|
|
|
class UserGrantedAssetAccountsApi(ListAPIView):
|
2022-09-15 02:46:57 +00:00
|
|
|
serializer_class = serializers.AccountsGrantedSerializer
|
|
|
|
rbac_perms = {
|
|
|
|
'list': 'perms.view_userassets'
|
|
|
|
}
|
|
|
|
|
|
|
|
@lazyproperty
|
2022-09-23 07:59:37 +00:00
|
|
|
def user(self) -> User:
|
2022-09-15 02:46:57 +00:00
|
|
|
user_id = self.kwargs.get('pk')
|
|
|
|
return User.objects.get(id=user_id)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def asset(self):
|
|
|
|
asset_id = self.kwargs.get('asset_id')
|
|
|
|
kwargs = {'id': asset_id, 'is_active': True}
|
|
|
|
asset = get_object_or_404(Asset, **kwargs)
|
|
|
|
return asset
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-10-18 08:42:32 +00:00
|
|
|
accounts = PermAccountUtil().get_perm_accounts_for_user_asset(
|
|
|
|
self.user, self.asset, with_actions=True
|
|
|
|
)
|
2022-09-28 10:40:33 +00:00
|
|
|
return accounts
|
2022-09-29 08:18:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyGrantedAssetAccountsApi(UserGrantedAssetAccountsApi):
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
|
|
|
|
class UserGrantedAssetSpecialAccountsApi(ListAPIView):
|
|
|
|
serializer_class = serializers.AccountsGrantedSerializer
|
|
|
|
rbac_perms = {
|
|
|
|
'list': 'perms.view_userassets'
|
|
|
|
}
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-09-15 02:46:57 +00:00
|
|
|
# 构造默认包含的账号,如: @INPUT @USER
|
2022-09-29 08:18:12 +00:00
|
|
|
accounts = [
|
|
|
|
Account.get_input_account(),
|
|
|
|
Account.get_user_account(self.user.username)
|
|
|
|
]
|
|
|
|
for account in accounts:
|
|
|
|
account.actions = Action.ALL
|
|
|
|
return accounts
|
2022-09-15 02:46:57 +00:00
|
|
|
|
|
|
|
|
2022-09-29 08:18:12 +00:00
|
|
|
class MyGrantedAssetSpecialAccountsApi(UserGrantedAssetSpecialAccountsApi):
|
2022-09-15 02:46:57 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
return self.request.user
|