1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2018-2022 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)
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
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
33 user
= self
.request
.user
34 mlist_id
= self
.kwargs
['list_id']
35 mlist
= List
.objects
.get_or_404(mlist_id
)
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
48 user
= self
.request
.user
49 mlist_id
= self
.kwargs
['list_id']
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
62 user
= self
.request
.user
63 domain
= self
.kwargs
['domain']
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."""
74 return self
.request
.user
.is_superuser