2020-10-22 09:05:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2021-05-07 11:40:04 +00:00
|
|
|
import time
|
|
|
|
|
2020-10-22 09:05:47 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2021-02-03 04:01:18 +00:00
|
|
|
from django.utils.decorators import method_decorator
|
2020-10-22 09:05:47 +00:00
|
|
|
from rest_framework.views import APIView, Response
|
2021-05-07 11:40:04 +00:00
|
|
|
from rest_framework import status
|
2020-10-22 09:05:47 +00:00
|
|
|
from rest_framework.generics import (
|
|
|
|
ListAPIView, get_object_or_404
|
|
|
|
)
|
|
|
|
|
2021-02-03 04:01:18 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2020-10-22 09:05:47 +00:00
|
|
|
from applications.models import Application
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms.utils.application.permission import (
|
2021-05-07 11:40:04 +00:00
|
|
|
get_application_system_user_ids,
|
|
|
|
validate_permission,
|
2020-10-22 09:05:47 +00:00
|
|
|
)
|
2021-02-05 05:29:29 +00:00
|
|
|
from perms.api.asset.user_permission.mixin import RoleAdminMixin, RoleUserMixin
|
2020-10-22 09:05:47 +00:00
|
|
|
from common.permissions import IsOrgAdminOrAppUser
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms.hands import User, SystemUser
|
|
|
|
from perms import serializers
|
2020-10-22 09:05:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserGrantedApplicationSystemUsersApi',
|
|
|
|
'MyGrantedApplicationSystemUsersApi',
|
|
|
|
'ValidateUserApplicationPermissionApi'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class GrantedApplicationSystemUsersMixin(ListAPIView):
|
|
|
|
serializer_class = serializers.ApplicationSystemUserSerializer
|
|
|
|
only_fields = serializers.ApplicationSystemUserSerializer.Meta.only_fields
|
|
|
|
user: None
|
|
|
|
|
2021-03-08 02:08:51 +00:00
|
|
|
def get_application_system_user_ids(self, application):
|
|
|
|
return get_application_system_user_ids(self.user, application)
|
2020-10-22 09:05:47 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
application_id = self.kwargs.get('application_id')
|
|
|
|
application = get_object_or_404(Application, id=application_id)
|
2021-03-08 02:08:51 +00:00
|
|
|
system_user_ids = self.get_application_system_user_ids(application)
|
|
|
|
system_users = SystemUser.objects.filter(id__in=system_user_ids)\
|
2020-10-22 09:05:47 +00:00
|
|
|
.only(*self.only_fields).order_by('priority')
|
|
|
|
return system_users
|
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class UserGrantedApplicationSystemUsersApi(RoleAdminMixin, GrantedApplicationSystemUsersMixin):
|
2020-10-22 09:05:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class MyGrantedApplicationSystemUsersApi(RoleUserMixin, GrantedApplicationSystemUsersMixin):
|
2020-10-22 09:05:47 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-02-03 04:01:18 +00:00
|
|
|
@method_decorator(tmp_to_root_org(), name='get')
|
2020-10-22 09:05:47 +00:00
|
|
|
class ValidateUserApplicationPermissionApi(APIView):
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
user_id = request.query_params.get('user_id', '')
|
|
|
|
application_id = request.query_params.get('application_id', '')
|
|
|
|
system_user_id = request.query_params.get('system_user_id', '')
|
|
|
|
|
2021-12-22 09:35:32 +00:00
|
|
|
data = {
|
|
|
|
'has_permission': False,
|
|
|
|
'expire_at': int(time.time()),
|
|
|
|
'actions': []
|
|
|
|
}
|
2021-05-07 11:40:04 +00:00
|
|
|
if not all((user_id, application_id, system_user_id)):
|
2021-12-22 09:35:32 +00:00
|
|
|
return Response(data)
|
2020-10-22 09:05:47 +00:00
|
|
|
|
2021-05-07 11:40:04 +00:00
|
|
|
user = User.objects.get(id=user_id)
|
|
|
|
application = Application.objects.get(id=application_id)
|
|
|
|
system_user = SystemUser.objects.get(id=system_user_id)
|
2021-12-22 09:35:32 +00:00
|
|
|
has_perm, actions, expire_at = validate_permission(user, application, system_user)
|
|
|
|
status_code = status.HTTP_200_OK if has_perm else status.HTTP_403_FORBIDDEN
|
|
|
|
data = {
|
|
|
|
'has_permission': has_perm,
|
|
|
|
'expire_at': int(expire_at),
|
|
|
|
'actions': actions
|
|
|
|
}
|
|
|
|
return Response(data, status=status_code)
|