2019-09-17 04:34:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-09-27 08:02:44 +00:00
|
|
|
from rest_framework.request import Request
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
from common.permissions import IsOrgAdminOrAppUser, IsValidUser
|
2019-09-19 09:27:49 +00:00
|
|
|
from common.utils import lazyproperty
|
2021-02-05 05:29:29 +00:00
|
|
|
from common.http import is_true
|
2020-10-20 02:45:23 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2020-08-16 15:08:58 +00:00
|
|
|
from users.models import User
|
2021-02-05 05:29:29 +00:00
|
|
|
from perms.utils.asset.user_permission import UserGrantedTreeRefreshController
|
2020-08-16 15:08:58 +00:00
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class PermBaseMixin:
|
|
|
|
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
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class RoleAdminMixin(PermBaseMixin):
|
2020-08-16 15:08:58 +00:00
|
|
|
permission_classes = (IsOrgAdminOrAppUser,)
|
2020-09-27 08:02:44 +00:00
|
|
|
kwargs: dict
|
2019-09-19 09:27:49 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
2020-08-16 15:08:58 +00:00
|
|
|
def user(self):
|
|
|
|
user_id = self.kwargs.get('pk')
|
|
|
|
return User.objects.get(id=user_id)
|
2019-09-17 04:34:47 +00:00
|
|
|
|
|
|
|
|
2021-02-05 05:29:29 +00:00
|
|
|
class RoleUserMixin(PermBaseMixin):
|
2020-08-16 15:08:58 +00:00
|
|
|
permission_classes = (IsValidUser,)
|
2020-09-27 08:02:44 +00:00
|
|
|
request: Request
|
2019-09-17 04:34:47 +00:00
|
|
|
|
2020-10-20 02:45:23 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
with tmp_to_root_org():
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
2020-08-16 15:08:58 +00:00
|
|
|
@lazyproperty
|
|
|
|
def user(self):
|
|
|
|
return self.request.user
|