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,
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
31 def key_get(request
, key_id
=None, user_id
=None):
36 user_cmp
= get_object_or_404(User
, pk
=user_id
) \
38 form
= SSHKeyForm(initial
={'user': user_cmp
.pk
})
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"))
55 def key_save(request
, key_id
=None):
59 key_edit
= get_object_or_404(SSHKey
, pk
=key_id
)
60 owner_id
= key_edit
.user
.id
62 key_edit
= SSHKey(user
=request
.user
)
63 owner_id
= request
.user
.id
65 # check if the user has appropriate permissions
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
)
74 return render_to_response("ganeti/ssh_keys/row.html", {"key": obj
},
75 context_instance
=RequestContext(request
))
77 return HttpResponse(json
.dumps(form
.errors
),
78 mimetype
="application/json")
79 return HttpResponse(_("Cannot retrieve information"))
83 def key_delete(request
, key_id
):
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":
93 return HttpResponse("1", mimetype
="application/json")
95 return HttpResponse(_("Cannot retrieve information"))
98 class SSHKeyForm(forms
.ModelForm
):
102 def __init__(self
, *args
, **kwargs
):
103 super(SSHKeyForm
, self
).__init
__(*args
, **kwargs
)
104 self
.fields
['user'].widget
= forms
.HiddenInput()
107 value
= self
.cleaned_data
.get('key', None)
108 if value
is not None:
109 value
= value
.replace('\n', ' ')