jumpserver/apps/perms/api/user_permission/accounts.py

54 lines
1.6 KiB
Python
Raw Normal View History

from django.shortcuts import get_object_or_404
from rest_framework.generics import ListAPIView, get_object_or_404
2022-11-15 08:26:18 +00:00
<<<<<<< HEAD
from common.utils import get_logger, lazyproperty
2022-11-15 08:26:18 +00:00
=======
from common.exceptions import JMSObjectDoesNotExist
from common.utils import get_logger, lazyproperty, is_uuid
2022-11-15 08:26:18 +00:00
>>>>>>> 0d3c5dddf9c838c5dcd28c20b6a8498088e45ce2
from perms import serializers
2022-11-15 08:24:53 +00:00
from perms.hands import Asset
from perms.utils import PermAccountUtil
2022-11-15 08:24:53 +00:00
from .mixin import SelfOrPKUserMixin
logger = get_logger(__name__)
__all__ = [
'UserGrantedAssetAccountsApi',
]
2022-11-15 08:24:53 +00:00
class UserGrantedAssetAccountsApi(SelfOrPKUserMixin, ListAPIView):
serializer_class = serializers.AccountsGrantedSerializer
2022-11-15 07:26:31 +00:00
rbac_perms = (
('GET', 'perms.view_userassets'),
2022-11-15 07:26:31 +00:00
('list', 'perms.view_userassets'),
)
@lazyproperty
2022-11-15 08:26:18 +00:00
<<<<<<< HEAD
=======
def user(self) -> User:
query_user = self.kwargs.get('user')
if is_uuid(query_user):
user = User.objects.get(id=query_user)
elif query_user == 'my':
user = self.request.user
else:
raise JMSObjectDoesNotExist(object_name=_('User'))
return user
@lazyproperty
2022-11-15 08:26:18 +00:00
>>>>>>> 0d3c5dddf9c838c5dcd28c20b6a8498088e45ce2
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):
util = PermAccountUtil()
accounts = util.get_permed_accounts_for_user(self.user, self.asset)
return accounts