Merge tag '0.10.2'
[ganeti_webmgr.git] / ganeti_web / views / users.py
blobc02ff64f65b0fc97d68b8733f97aa5c496122213
1 # Copyright (C) 2010 Oregon State University et al.
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
16 # USA.
18 from django import forms
19 from django.contrib.auth.decorators import login_required
20 from django.contrib.auth.models import User
21 from django.http import HttpResponse, HttpResponseForbidden
22 from django.shortcuts import get_object_or_404, render_to_response
23 from django.template import RequestContext
24 from django.utils import simplejson as json
25 from django.utils.translation import ugettext as _
27 from ganeti_web.models import SSHKey
30 @login_required
31 def key_get(request, key_id=None, user_id=None):
32 if request.is_ajax:
33 user = request.user
35 if not key_id:
36 user_cmp = get_object_or_404(User, pk=user_id) \
37 if user_id else user
38 form = SSHKeyForm(initial={'user': user_cmp.pk})
39 else:
40 key_edit = get_object_or_404(SSHKey, pk=key_id)
41 form = SSHKeyForm(instance=key_edit)
42 user_cmp = key_edit.user
44 if not (user.is_superuser or user_cmp == user):
45 return HttpResponseForbidden(_("Only superuser or owner "
46 "can get user's SSH key."))
48 return render_to_response("ganeti/ssh_keys/form.html",
49 {"key_form": form, "key_id": key_id},
50 context_instance=RequestContext(request))
51 return HttpResponse(_("Cannot retrieve information"))
54 @login_required
55 def key_save(request, key_id=None):
56 if request.is_ajax:
57 # get key's user id
58 if key_id:
59 key_edit = get_object_or_404(SSHKey, pk=key_id)
60 owner_id = key_edit.user.id
61 else:
62 key_edit = SSHKey(user=request.user)
63 owner_id = request.user.id
65 # check if the user has appropriate permissions
66 user = request.user
67 if not (user.is_superuser or user.id == owner_id):
68 return HttpResponseForbidden(_("Only superuser or owner "
69 "can save user's SSH key."))
71 form = SSHKeyForm(data=request.POST, instance=key_edit)
72 if form.is_valid():
73 obj = form.save()
74 return render_to_response("ganeti/ssh_keys/row.html", {"key": obj},
75 context_instance=RequestContext(request))
76 else:
77 return HttpResponse(json.dumps(form.errors),
78 mimetype="application/json")
79 return HttpResponse(_("Cannot retrieve information"))
82 @login_required
83 def key_delete(request, key_id):
84 user = request.user
85 key_edit = get_object_or_404(SSHKey, pk=key_id)
87 if not (user.is_superuser or key_edit.user == user):
88 return HttpResponseForbidden(_('Only superuser or owner '
89 'can delete user\'s SSH key.'))
91 if request.method == "DELETE":
92 key_edit.delete()
93 return HttpResponse("1", mimetype="application/json")
95 return HttpResponse(_("Cannot retrieve information"))
98 class SSHKeyForm(forms.ModelForm):
99 class Meta:
100 model = SSHKey
102 def __init__(self, *args, **kwargs):
103 super(SSHKeyForm, self).__init__(*args, **kwargs)
104 self.fields['user'].widget = forms.HiddenInput()
106 def clean_key(self):
107 value = self.cleaned_data.get('key', None)
108 if value is not None:
109 value = value.replace('\n', ' ')
110 return value