0005-utils-add-check_session_roles-decorator.patch
mellon/utils.py | ||
---|---|---|
8 | 8 |
from django.contrib import auth |
9 | 9 |
from django.contrib.auth.models import Group |
10 | 10 |
from django.core.urlresolvers import reverse |
11 |
from django.http import QueryDict, HttpResponseRedirect |
|
11 | 12 |
from django.template.loader import render_to_string |
12 | 13 |
from django.utils.timezone import make_aware, now, make_naive, is_aware, get_default_timezone |
13 | 14 |
from django.conf import settings |
... | ... | |
308 | 309 |
if role.uuid in request.session['mellon_session']['role-slug']: |
309 | 310 |
return True |
310 | 311 |
raise RolesNotInSession((role.uuid,)) |
312 | ||
313 | ||
314 |
def check_session_roles(func, login_url=None): |
|
315 |
"""Redirect to mellon login if a required role is missing. |
|
316 | ||
317 |
login_url can be specified in order to redirect to another view. In that |
|
318 |
case be sure to handle the 'next' and the multiple 'roles' query parameters |
|
319 |
that get added. |
|
320 |
""" |
|
321 |
if not login_url: |
|
322 |
login_url = reverse(app_settings.LOGIN_URL) |
|
323 |
def wrapped(request, *args, **kwargs): |
|
324 |
try: |
|
325 |
return func(request, *args, **kwargs) |
|
326 |
except RolesNotInSession as e: |
|
327 |
q = QueryDict(mutable=True) |
|
328 |
q.setlist('roles', e.roles) |
|
329 |
q['next'] = request.get_full_path() |
|
330 |
redirect_url = '?'.join((login_url, q.urlencode(safe='/'))) |
|
331 |
return HttpResponseRedirect(redirect_url) |
|
332 |
return wrapped |
|
311 |
- |