root / snf-cyclades-app / synnefo / helpdesk / views.py @ e620ca07
History | View | Annotate | Download (3.9 kB)
1 |
from itertools import chain |
---|---|
2 |
|
3 |
from django.shortcuts import redirect |
4 |
from django.views.generic.simple import direct_to_template |
5 |
from django.db.models import get_apps |
6 |
from django.conf import settings |
7 |
from django.core.exceptions import PermissionDenied |
8 |
from django.db.models import Q |
9 |
from django.http import Http404, HttpResponse |
10 |
from django.utils import simplejson as json |
11 |
from urllib import unquote |
12 |
|
13 |
from synnefo.lib.astakos import get_user |
14 |
from synnefo.db.models import * |
15 |
|
16 |
def get_token_from_cookie(request, cookiename): |
17 |
"""
|
18 |
Extract token from the cookie name provided. Cookie should be in the same
|
19 |
form as astakos service sets its cookie contents::
|
20 |
|
21 |
<user_uniq>|<user_token>
|
22 |
"""
|
23 |
try:
|
24 |
cookie_content = unquote(request.COOKIES.get(cookiename, None))
|
25 |
return cookie_content.split("|")[1] |
26 |
except AttributeError: |
27 |
pass
|
28 |
|
29 |
return None |
30 |
|
31 |
# TODO: here we mix ui setting with helpdesk settings
|
32 |
# if sometime in the future helpdesk gets splitted from the
|
33 |
# cyclades api code this should change and helpdesk should provide
|
34 |
# its own setting HELPDESK_AUTH_COOKIE_NAME.
|
35 |
HELPDESK_AUTH_COOKIE = getattr(settings, 'UI_AUTH_COOKIE_NAME', '_pithos2_a') |
36 |
|
37 |
def helpdesk_user_required(func, groups=['helpdesk']): |
38 |
"""
|
39 |
Django view wrapper that checks if identified request user has helpdesk
|
40 |
permissions (exists in helpdesk group)
|
41 |
"""
|
42 |
def wrapper(request, *args, **kwargs): |
43 |
return func(request, *args, **kwargs)
|
44 |
token = get_token_from_cookie(request, HELPDESK_AUTH_COOKIE) |
45 |
get_user(request, settings.ASTAKOS_URL, fallback_token=token) |
46 |
if hasattr(request, 'user') and request.user: |
47 |
groups = request.user.get('groups', [])
|
48 |
|
49 |
if not groups: |
50 |
raise PermissionDenied
|
51 |
|
52 |
for g in groups: |
53 |
if not g in groups: |
54 |
raise PermissionDenied
|
55 |
else:
|
56 |
raise PermissionDenied
|
57 |
|
58 |
return func(request, *args, **kwargs)
|
59 |
|
60 |
return wrapper
|
61 |
|
62 |
|
63 |
@helpdesk_user_required
|
64 |
def index(request): |
65 |
"""
|
66 |
Helpdesk index view.
|
67 |
"""
|
68 |
|
69 |
# if form submitted redirect to details
|
70 |
account = request.GET.get('account', None) |
71 |
if account:
|
72 |
return redirect('synnefo.helpdesk.views.account', account=account) |
73 |
|
74 |
# show index template
|
75 |
return direct_to_template(request, "helpdesk/index.html") |
76 |
|
77 |
|
78 |
@helpdesk_user_required
|
79 |
def account(request, account): |
80 |
"""
|
81 |
Account details view.
|
82 |
"""
|
83 |
|
84 |
# all user vms
|
85 |
vms = VirtualMachine.objects.filter(userid=account).order_by('deleted')
|
86 |
|
87 |
# return all user private and public networks
|
88 |
public_networks = Network.objects.filter(public=True).order_by('state') |
89 |
private_networks = Network.objects.filter(userid=account).order_by('state')
|
90 |
networks = list(public_networks) + list(private_networks) |
91 |
|
92 |
account_exists = True
|
93 |
if vms.count() == 0 and private_networks.count() == 0: |
94 |
account_exists = False
|
95 |
|
96 |
user_context = { |
97 |
'account_exists': account_exists,
|
98 |
'account': account,
|
99 |
'vms': vms,
|
100 |
'networks': networks,
|
101 |
'UI_MEDIA_URL': settings.UI_MEDIA_URL
|
102 |
} |
103 |
return direct_to_template(request, "helpdesk/account.html", |
104 |
extra_context=user_context) |
105 |
|
106 |
|
107 |
@helpdesk_user_required
|
108 |
def user_list(request): |
109 |
"""
|
110 |
Return a json list of users based on the prefix provided. Prefix
|
111 |
should end with "@".
|
112 |
"""
|
113 |
|
114 |
prefix = request.GET.get('prefix', None) |
115 |
if not prefix or "@" not in prefix: |
116 |
raise Http404
|
117 |
|
118 |
# keep only the user part (e.g. "user@")
|
119 |
prefix = prefix.split("@")[0] + "@" |
120 |
|
121 |
q = Q(userid__startswith=prefix) & ~Q(userid=None)
|
122 |
vm_users = VirtualMachine.objects.filter(q).values_list("userid", flat=True) |
123 |
net_users = Network.objects.filter(q).values_list("userid", flat=True) |
124 |
users = list(set(list(vm_users) + list(net_users))) |
125 |
return HttpResponse(json.dumps(users), content_type="application/json") |
126 |
|