From dac974ddabd4317296d4ec66f8ac2843c67265bb Mon Sep 17 00:00:00 2001 From: Jan Krcmar Date: Tue, 22 Dec 2015 15:09:02 +0100 Subject: [PATCH] request.user.is_authenticated() substitued for @login_required decorator settings.LOGIN_URL = /accounts/login --- accounts/views.py | 13 ++++--------- computes/views.py | 13 ++++--------- console/views.py | 5 ++--- create/views.py | 5 ++--- instances/views.py | 23 +++++++---------------- interfaces/views.py | 9 +++------ networks/views.py | 9 +++------ secrets/views.py | 5 ++--- storages/views.py | 9 +++------ webvirtcloud/settings.py | 3 +++ 10 files changed, 33 insertions(+), 61 deletions(-) diff --git a/accounts/views.py b/accounts/views.py index d2893bb..899a67c 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -3,18 +3,18 @@ from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse from django.utils.translation import ugettext_lazy as _ from django.contrib.auth.models import User +from django.contrib.auth.decorators import login_required from accounts.models import UserInstance, UserSSHKey from instances.models import Instance from accounts.forms import UserAddForm +@login_required def profile(request): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) error_messages = [] user = User.objects.get(id=request.user.id) @@ -63,16 +63,13 @@ def profile(request): return HttpResponseRedirect(request.get_full_path()) return render(request, 'profile.html', locals()) - +@login_required def accounts(request): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) @@ -123,15 +120,13 @@ def accounts(request): return render(request, 'accounts.html', locals()) +@login_required def account(request, user_id): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) diff --git a/computes/views.py b/computes/views.py index 63c17f8..e293107 100644 --- a/computes/views.py +++ b/computes/views.py @@ -3,6 +3,7 @@ import json from django.http import HttpResponse, HttpResponseRedirect from django.core.urlresolvers import reverse from django.shortcuts import render, get_object_or_404 +from django.contrib.auth.decorators import login_required from computes.models import Compute from instances.models import Instance from accounts.models import UserInstance @@ -12,15 +13,13 @@ from vrtManager.connection import CONN_SSH, CONN_TCP, CONN_TLS, CONN_SOCKET, con from libvirt import libvirtError +@login_required def computes(request): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) @@ -130,15 +129,13 @@ def computes(request): return render(request, 'computes.html', locals()) +@login_required def overview(request, compute_id): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) @@ -160,15 +157,13 @@ def overview(request, compute_id): return render(request, 'overview.html', locals()) +@login_required def compute_graph(request, compute_id): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('login')) - points = 5 datasets = {} cookies = {} diff --git a/console/views.py b/console/views.py index a123064..4651c87 100644 --- a/console/views.py +++ b/console/views.py @@ -2,6 +2,7 @@ import re from django.shortcuts import render from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse +from django.contrib.auth.decorators import login_required from instances.models import Instance from vrtManager.instance import wvmInstance from webvirtcloud.settings import WS_PORT @@ -9,15 +10,13 @@ from webvirtcloud.settings import WS_PUBLIC_HOST from libvirt import libvirtError +@login_required def console(request): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('login')) - if request.method == 'GET': token = request.GET.get('token', '') diff --git a/create/views.py b/create/views.py index 5b03f8c..3f97606 100644 --- a/create/views.py +++ b/create/views.py @@ -2,6 +2,7 @@ from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseRedirect from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse +from django.contrib.auth.decorators import login_required from computes.models import Compute from create.models import Flavor from create.forms import FlavorAddForm, NewVMForm @@ -11,15 +12,13 @@ from vrtManager import util from libvirt import libvirtError +@login_required def create_instance(request, compute_id): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) diff --git a/instances/views.py b/instances/views.py index a68f52c..f6f8007 100644 --- a/instances/views.py +++ b/instances/views.py @@ -9,6 +9,7 @@ from django.http import HttpResponse, HttpResponseRedirect from django.core.urlresolvers import reverse from django.shortcuts import render, get_object_or_404 from django.utils.translation import ugettext_lazy as _ +from django.contrib.auth.decorators import login_required from computes.models import Compute from instances.models import Instance from accounts.models import UserInstance, UserSSHKey @@ -20,27 +21,23 @@ from webvirtcloud.settings import QEMU_KEYMAPS, QEMU_CONSOLE_TYPES from logs.views import addlogmsg +@login_required def index(request): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('login')) - else: - return HttpResponseRedirect(reverse('instances')) + return HttpResponseRedirect(reverse('instances')) +@login_required def instances(request): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - error_messages = [] all_host_vms = {} all_user_vms = {} @@ -144,15 +141,13 @@ def instances(request): return render(request, 'instances.html', locals()) +@login_required def instance(request, compute_id, vname): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - error_messages = [] messages = [] compute = get_object_or_404(Compute, pk=compute_id) @@ -517,15 +512,13 @@ def instance(request, compute_id, vname): return render(request, 'instance.html', locals()) +@login_required def inst_status(request, compute_id, vname): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('login')) - compute = get_object_or_404(Compute, pk=compute_id) response = HttpResponse() response['Content-Type'] = "text/javascript" @@ -544,15 +537,13 @@ def inst_status(request, compute_id, vname): return response +@login_required def inst_graph(request, compute_id, vname): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('login')) - datasets = {} json_blk = [] datasets_blk = {} diff --git a/interfaces/views.py b/interfaces/views.py index a889b27..921d0b8 100644 --- a/interfaces/views.py +++ b/interfaces/views.py @@ -1,21 +1,20 @@ from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse +from django.contrib.auth.decorators import login_required from computes.models import Compute from interfaces.forms import AddInterface from vrtManager.interface import wvmInterface, wvmInterfaces from libvirt import libvirtError +@login_required def interfaces(request, compute_id): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) @@ -57,15 +56,13 @@ def interfaces(request, compute_id): return render(request, 'interfaces.html', locals()) +@login_required def interface(request, compute_id, iface): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) diff --git a/networks/views.py b/networks/views.py index 01347db..24d6bc6 100644 --- a/networks/views.py +++ b/networks/views.py @@ -2,6 +2,7 @@ from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseRedirect from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse +from django.contrib.auth.decorators import login_required from computes.models import Compute from networks.forms import AddNetPool from vrtManager.network import wvmNetwork, wvmNetworks @@ -9,15 +10,13 @@ from vrtManager.network import network_size from libvirt import libvirtError +@login_required def networks(request, compute_id): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) @@ -60,15 +59,13 @@ def networks(request, compute_id): return render(request, 'networks.html', locals()) +@login_required def network(request, compute_id, pool): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) diff --git a/secrets/views.py b/secrets/views.py index 5e6bf0e..90e5e36 100644 --- a/secrets/views.py +++ b/secrets/views.py @@ -1,21 +1,20 @@ from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse +from django.contrib.auth.decorators import login_required from computes.models import Compute from secrets.forms import AddSecret from vrtManager.secrets import wvmSecrets from libvirt import libvirtError +@login_required def secrets(request, compute_id): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) diff --git a/storages/views.py b/storages/views.py index 499d8b3..d3f1965 100644 --- a/storages/views.py +++ b/storages/views.py @@ -2,21 +2,20 @@ from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseRedirect from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse +from django.contrib.auth.decorators import login_required from computes.models import Compute from storages.forms import AddStgPool, AddImage, CloneImage from vrtManager.storage import wvmStorage, wvmStorages from libvirt import libvirtError +@login_required def storages(request, compute_id): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) @@ -68,15 +67,13 @@ def storages(request, compute_id): return render(request, 'storages.html', locals()) +@login_required def storage(request, compute_id, pool): """ :param request: :return: """ - if not request.user.is_authenticated(): - return HttpResponseRedirect(reverse('index')) - if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) diff --git a/webvirtcloud/settings.py b/webvirtcloud/settings.py index d8e8906..610f757 100644 --- a/webvirtcloud/settings.py +++ b/webvirtcloud/settings.py @@ -46,8 +46,11 @@ MIDDLEWARE_CLASSES = ( AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.RemoteUserBackend', + #'accounts.backends.MyRemoteUserBackend', ) +LOGIN_URL = '/accounts/login' + ROOT_URLCONF = 'webvirtcloud.urls' WSGI_APPLICATION = 'webvirtcloud.wsgi.application'