aboutsummaryrefslogtreecommitdiff
path: root/src/django_pgpmailman/decorators.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/django_pgpmailman/decorators.py')
-rw-r--r--src/django_pgpmailman/decorators.py42
1 files changed, 40 insertions, 2 deletions
diff --git a/src/django_pgpmailman/decorators.py b/src/django_pgpmailman/decorators.py
index 41e94da..5f994b6 100644
--- a/src/django_pgpmailman/decorators.py
+++ b/src/django_pgpmailman/decorators.py
@@ -15,18 +15,56 @@
#
# You should have received a copy of the GNU General Public License along with
# Postorius. If not, see <http://www.gnu.org/licenses/>.
+from allauth.account.models import EmailAddress
+from django.core.exceptions import PermissionDenied
from django.http import Http404
+from six import wraps
from six.moves.urllib_error import HTTPError
from django_pgpmailman.plugin import get_pgp_plugin
def list_view(fn):
- def wrapper(request, list_id, *args, **kwargs):
+ @wraps(fn)
+ def wrapper(request, *args, **kwargs):
try:
- pgp_list = get_pgp_plugin().get_list(list_id)
+ pgp_list = get_pgp_plugin().get_list(kwargs.pop('list_id'))
except HTTPError:
raise Http404
return fn(request, pgp_list, *args, **kwargs)
return wrapper
+
+
+def list_class_view(fn):
+ @wraps(fn)
+ def wrapper(self, request, *args, **kwargs):
+ self.pgp_list = get_pgp_plugin().get_list(kwargs.pop('list_id'))
+ return fn(self, request, *args, **kwargs)
+
+ return wrapper
+
+
+def member_role_required(*roles):
+ def wrapper(fn):
+ @wraps(fn)
+ def wrapped(self, request, *args, **kwargs):
+ user = request.user
+ if not user.is_authenticated():
+ raise PermissionDenied
+ mlist = self.pgp_list.mlist
+ addresses = set(EmailAddress.objects.filter(
+ user=user, verified=True).values_list('email', flat=True))
+ for role in roles:
+ for address in addresses:
+ members = mlist.find_members(address, role)
+ if len(members) >= 0:
+ break
+ else:
+ raise PermissionDenied
+
+ return fn(self, request, *args, **kwargs)
+
+ return wrapped
+
+ return wrapper