Some minor housekeeping fixes.
[mailman-postorious.git] / src / postorius / auth / mixins.py
blob1b044d2396c523400223c0aba57d90b80603a5cc
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2018-2021 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
24 from postorius.models import List
27 class ListOwnerMixin(LoginRequiredMixin, UserPassesTestMixin):
28 """Mixin to allow access to only List owners."""
30 raise_exception = True
32 def test_func(self):
33 user = self.request.user
34 mlist_id = self.kwargs['list_id']
35 mlist = List.objects.get_or_404(mlist_id)
36 if user.is_superuser:
37 return True
38 set_list_access_props(user, mlist, moderator=False)
39 return user.is_list_owner
42 class ListModeratorMixin(LoginRequiredMixin, UserPassesTestMixin):
43 """Mixin to allow access to only List Moderators."""
45 raise_exception = True
47 def test_func(self):
48 user = self.request.user
49 mlist_id = self.kwargs['list_id']
50 if user.is_superuser:
51 return True
52 set_list_access_props(user, mlist_id)
53 return user.is_list_owner or user.is_list_moderator
56 class DomainOwnerMixin(LoginRequiredMixin, UserPassesTestMixin):
57 """Mixin to allow access to only Domain Owner."""
59 raise_exception = True
61 def test_func(self):
62 user = self.request.user
63 domain = self.kwargs['domain']
64 if user.is_superuser:
65 return True
66 set_domain_access_props(user, domain)
67 return user.is_domain_owner
70 class SuperUserRequiredMixin(LoginRequiredMixin, UserPassesTestMixin):
71 """Mixin to allow access to only Django Superusers."""
73 def test_func(self):
74 return self.request.user.is_superuser