From 7e4f20f443ec201a90e5118defd328b325314f57 Mon Sep 17 00:00:00 2001 From: xinwen Date: Thu, 8 Apr 2021 14:59:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=A7=BB=E5=8A=A8=20PermissionsMix?= =?UTF-8?q?in=20=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/mixins/views.py | 18 +++++++++++++++++- apps/common/permissions.py | 15 --------------- apps/jumpserver/views/index.py | 3 ++- apps/ops/views.py | 4 ++-- apps/users/views/profile/password.py | 3 ++- apps/users/views/profile/pubkey.py | 3 ++- apps/users/views/profile/reset.py | 3 ++- 7 files changed, 27 insertions(+), 22 deletions(-) diff --git a/apps/common/mixins/views.py b/apps/common/mixins/views.py index b6685def5..13f365662 100644 --- a/apps/common/mixins/views.py +++ b/apps/common/mixins/views.py @@ -1,12 +1,14 @@ # -*- coding: utf-8 -*- # # coding: utf-8 - +from django.contrib.auth.mixins import UserPassesTestMixin from django.utils import timezone __all__ = ["DatetimeSearchMixin"] +from rest_framework import permissions + class DatetimeSearchMixin: date_format = '%Y-%m-%d' @@ -36,3 +38,17 @@ class DatetimeSearchMixin: def get(self, request, *args, **kwargs): self.get_date_range() return super().get(request, *args, **kwargs) + + +class PermissionsMixin(UserPassesTestMixin): + permission_classes = [permissions.IsAuthenticated] + + def get_permissions(self): + return self.permission_classes + + def test_func(self): + permission_classes = self.get_permissions() + for permission_class in permission_classes: + if not permission_class().has_permission(self.request, self): + return False + return True \ No newline at end of file diff --git a/apps/common/permissions.py b/apps/common/permissions.py index 65a57827d..7df83046d 100644 --- a/apps/common/permissions.py +++ b/apps/common/permissions.py @@ -2,7 +2,6 @@ # import time from rest_framework import permissions -from django.contrib.auth.mixins import UserPassesTestMixin from django.conf import settings from orgs.utils import current_org @@ -95,20 +94,6 @@ class WithBootstrapToken(permissions.BasePermission): return settings.BOOTSTRAP_TOKEN == request_bootstrap_token -class PermissionsMixin(UserPassesTestMixin): - permission_classes = [permissions.IsAuthenticated] - - def get_permissions(self): - return self.permission_classes - - def test_func(self): - permission_classes = self.get_permissions() - for permission_class in permission_classes: - if not permission_class().has_permission(self.request, self): - return False - return True - - class UserCanAnyPermCurrentOrg(permissions.BasePermission): def has_permission(self, request, view): return current_org.can_any_by(request.user) diff --git a/apps/jumpserver/views/index.py b/apps/jumpserver/views/index.py index 5050d72c0..8f974a483 100644 --- a/apps/jumpserver/views/index.py +++ b/apps/jumpserver/views/index.py @@ -1,6 +1,7 @@ from django.views.generic import TemplateView from django.shortcuts import redirect -from common.permissions import PermissionsMixin, IsValidUser +from common.permissions import IsValidUser +from common.mixins.views import PermissionsMixin __all__ = ['IndexView'] diff --git a/apps/ops/views.py b/apps/ops/views.py index 9ae2d9755..7b18b0f46 100644 --- a/apps/ops/views.py +++ b/apps/ops/views.py @@ -3,8 +3,8 @@ from django.views.generic import TemplateView from django.conf import settings -from common.permissions import PermissionsMixin, IsOrgAdmin, IsOrgAuditor - +from common.permissions import IsOrgAdmin, IsOrgAuditor +from common.mixins.views import PermissionsMixin __all__ = ['CeleryTaskLogView'] diff --git a/apps/users/views/profile/password.py b/apps/users/views/profile/password.py index 1fbbd64a7..e2cd8f8e2 100644 --- a/apps/users/views/profile/password.py +++ b/apps/users/views/profile/password.py @@ -11,9 +11,10 @@ from django.contrib.auth import logout as auth_logout from common.utils import get_logger from common.permissions import ( - PermissionsMixin, IsValidUser, + IsValidUser, UserCanUpdatePassword ) +from common.mixins.views import PermissionsMixin from ... import forms from ...models import User from ...utils import ( diff --git a/apps/users/views/profile/pubkey.py b/apps/users/views/profile/pubkey.py index 4010fd996..e2125f0cc 100644 --- a/apps/users/views/profile/pubkey.py +++ b/apps/users/views/profile/pubkey.py @@ -8,9 +8,10 @@ from django.views.generic.edit import UpdateView from common.utils import get_logger, ssh_key_gen from common.permissions import ( - PermissionsMixin, IsValidUser, + IsValidUser, UserCanUpdateSSHKey, ) +from common.mixins.views import PermissionsMixin from ... import forms from ...models import User diff --git a/apps/users/views/profile/reset.py b/apps/users/views/profile/reset.py index 8c676c756..73c34396b 100644 --- a/apps/users/views/profile/reset.py +++ b/apps/users/views/profile/reset.py @@ -13,7 +13,8 @@ from formtools.wizard.views import SessionWizardView from django.views.generic import FormView from common.utils import get_object_or_none -from common.permissions import PermissionsMixin, IsValidUser +from common.permissions import IsValidUser +from common.mixins.views import PermissionsMixin from ...models import User from ...utils import ( send_reset_password_mail, get_password_check_rules, check_password_rules,