2019-07-12 10:31:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2019-09-12 10:25:22 +00:00
|
|
|
from rest_framework.generics import get_object_or_404
|
2019-09-17 04:34:47 +00:00
|
|
|
from common.permissions import IsValidUser, IsOrgAdminOrAppUser
|
2019-09-12 10:25:22 +00:00
|
|
|
from common.utils import get_logger
|
2020-03-12 08:24:38 +00:00
|
|
|
from orgs.utils import set_to_root_org, set_current_org, get_current_org
|
2019-09-18 14:06:46 +00:00
|
|
|
from ..hands import User, UserGroup
|
2019-09-12 10:25:22 +00:00
|
|
|
|
2019-07-12 10:31:55 +00:00
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2019-09-12 10:25:22 +00:00
|
|
|
__all__ = [
|
2019-09-17 09:46:18 +00:00
|
|
|
'UserPermissionMixin', 'UserGroupPermissionMixin',
|
2019-09-12 10:25:22 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class UserPermissionMixin:
|
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2019-10-18 07:05:45 +00:00
|
|
|
current_org = None
|
2019-09-12 10:25:22 +00:00
|
|
|
obj = None
|
|
|
|
|
|
|
|
def initial(self, *args, **kwargs):
|
2020-07-23 02:55:01 +00:00
|
|
|
super().initial(*args, **kwargs)
|
2019-09-12 10:25:22 +00:00
|
|
|
self.obj = self.get_obj()
|
|
|
|
|
|
|
|
def get_obj(self):
|
|
|
|
user_id = self.kwargs.get('pk', '')
|
|
|
|
if user_id:
|
|
|
|
user = get_object_or_404(User, id=user_id)
|
|
|
|
else:
|
2020-03-12 08:24:38 +00:00
|
|
|
self.current_org = get_current_org()
|
|
|
|
set_to_root_org()
|
2019-09-12 10:25:22 +00:00
|
|
|
user = self.request.user
|
|
|
|
return user
|
|
|
|
|
|
|
|
def get_permissions(self):
|
|
|
|
if self.kwargs.get('pk') is None:
|
|
|
|
self.permission_classes = (IsValidUser,)
|
|
|
|
return super().get_permissions()
|
2019-07-12 10:31:55 +00:00
|
|
|
|
2019-10-18 07:05:45 +00:00
|
|
|
def finalize_response(self, request, response, *args, **kwargs):
|
|
|
|
response = super().finalize_response(request, response, *args, **kwargs)
|
|
|
|
org = getattr(self, 'current_org', None)
|
|
|
|
if org:
|
|
|
|
set_current_org(org)
|
|
|
|
return response
|
|
|
|
|
2019-07-12 10:31:55 +00:00
|
|
|
|
2019-09-17 09:46:18 +00:00
|
|
|
class UserGroupPermissionMixin:
|
|
|
|
obj = None
|
|
|
|
|
|
|
|
def get_obj(self):
|
|
|
|
user_group_id = self.kwargs.get('pk', '')
|
|
|
|
user_group = get_object_or_404(UserGroup, id=user_group_id)
|
|
|
|
return user_group
|
|
|
|
|
|
|
|
|