1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

27 lines
970 B
Python
Raw Normal View History

2017-05-24 11:33:11 -04:00
from __future__ import unicode_literals
2016-03-01 11:23:03 -05:00
from django.contrib import messages
from django.shortcuts import redirect
from .models import UserKey
def userkey_required():
"""
Decorator for views which require that the user has an active UserKey (typically for encryption/decryption of
Secrets).
"""
def _decorator(view):
def wrapped_view(request, *args, **kwargs):
try:
uk = UserKey.objects.get(user=request.user)
except UserKey.DoesNotExist:
2017-05-24 11:33:11 -04:00
messages.warning(request, "This operation requires an active user key, but you don't have one.")
2017-03-14 12:36:44 -04:00
return redirect('user:userkey')
2016-03-01 11:23:03 -05:00
if not uk.is_active():
2017-05-24 11:33:11 -04:00
messages.warning(request, "This operation is not available. Your user key has not been activated.")
2017-03-14 12:36:44 -04:00
return redirect('user:userkey')
2016-03-01 11:23:03 -05:00
return view(request, *args, **kwargs)
return wrapped_view
return _decorator