Add Mixin classes for custom access control for views.
[mailman-postorious.git] / src / postorius / auth / mixins.py
blobd6364da8479e610461d28a3dccaf86befa3b1352
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2018 by the Free Software Foundation, Inc.
4 # This file is part of Postorius.
6 # Postorius is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
9 # any later version.
11 # Postorius is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 # more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Postorius. If not, see <http://www.gnu.org/licenses/>.
21 from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
23 from postorius.auth.utils import set_domain_access_props, set_list_access_props
26 class ListOwnerMixin(LoginRequiredMixin, UserPassesTestMixin):
27 """Mixin to allow access to only List owners."""
29 def test_func(self):
30 user = self.request.user
31 mlist_id = self.kwargs['list_id']
32 if user.is_superuser:
33 return True
34 set_list_access_props(user, mlist_id)
35 if user.is_list_owner:
36 return True
37 return False
40 class ListModeratorMixin(LoginRequiredMixin, UserPassesTestMixin):
41 """Mixin to allow access to only List Moderators."""
43 def test_func(self):
44 user = self.request.user
45 mlist_id = self.kwargs['list_id']
46 if user.is_superuser:
47 return True
48 set_list_access_props(user, mlist_id)
49 if user.is_list_owner or user.is_list_moderator:
50 return True
51 return False
54 class DomainOwnerMixin(LoginRequiredMixin, UserPassesTestMixin):
55 """Mixin to allow access to only Domain Owner."""
57 def test_func(self):
58 user = self.request.user
59 domain = self.kwargs['domain']
60 if user.is_superuser:
61 return True
62 set_domain_access_props(user, domain)
63 if user.is_domain_owner:
64 return True
65 return False
68 class SuperUserRequiredMixin(LoginRequiredMixin, UserPassesTestMixin):
69 """Mixin to allow access to only Django Superusers."""
71 def test_func(self):
72 if self.request.user.is_superuser:
73 return True
74 return False