Skip to content

Commit 00783a2

Browse files
committed
Added group_required authentication/authorization decorator
- Legacy-Id: 1887
1 parent b522fe9 commit 00783a2

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

ietf/ietfauth/decorators.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Portion Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
2+
# All rights reserved. Contact: Pasi Eronen <pasi.eronen@nokia.com>
3+
#
4+
# Redistribution and use in source and binary forms, with or without
5+
# modification, are permitted provided that the following conditions
6+
# are met:
7+
#
8+
# * Redistributions of source code must retain the above copyright
9+
# notice, this list of conditions and the following disclaimer.
10+
#
11+
# * Redistributions in binary form must reproduce the above
12+
# copyright notice, this list of conditions and the following
13+
# disclaimer in the documentation and/or other materials provided
14+
# with the distribution.
15+
#
16+
# * Neither the name of the Nokia Corporation and/or its
17+
# subsidiary(-ies) nor the names of its contributors may be used
18+
# to endorse or promote products derived from this software
19+
# without specific prior written permission.
20+
#
21+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32+
33+
from django.utils.http import urlquote
34+
from django.contrib.auth.decorators import _CheckLogin
35+
from django.http import HttpResponseRedirect, HttpResponseForbidden
36+
37+
# based on http://www.djangosnippets.org/snippets/254/
38+
class _CheckLogin403(_CheckLogin):
39+
def __init__(self, view_func, test_func, forbidden_message=None):
40+
self.forbidden_message = forbidden_message
41+
super(_CheckLogin403, self).__init__(view_func, test_func)
42+
43+
def __call__(self, request, *args, **kwargs):
44+
if not request.user.is_authenticated():
45+
path = urlquote(request.get_full_path())
46+
tup = self.login_url, self.redirect_field_name, path
47+
return HttpResponseRedirect('%s?%s=%s' % tup)
48+
elif self.test_func(request.user):
49+
return self.view_func(request, *args, **kwargs)
50+
else:
51+
return HttpResponseForbidden(self.forbidden_message)
52+
53+
# based on http://www.djangosnippets.org/snippets/1703/
54+
def group_required(*group_names):
55+
"""
56+
Decorator for views that checks that the user is logged in,
57+
and belongs to (at least) one of the listed groups. Users who
58+
are not logged in are redirected to the login page; users
59+
who don't belong to any of the groups (but are logged in)
60+
get a "403" page.
61+
"""
62+
def decorate(view_func):
63+
return _CheckLogin403(view_func, lambda u: bool(u.groups.filter(name__in=group_names)), "Restricted to group(s) "+",".join(group_names))
64+
return decorate

0 commit comments

Comments
 (0)