diff --git a/accounts/migrations/0003_usersshkey.py b/accounts/migrations/0003_usersshkey.py new file mode 100644 index 0000000..b00bc62 --- /dev/null +++ b/accounts/migrations/0003_usersshkey.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations +from django.conf import settings + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('accounts', '0002_auto_20150325_0846'), + ] + + operations = [ + migrations.CreateModel( + name='UserSSHKey', + fields=[ + ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)), + ('keyname', models.CharField(max_length=25)), + ('keypublic', models.CharField(max_length=500)), + ('user', models.ForeignKey(to=settings.AUTH_USER_MODEL)), + ], + ), + ] diff --git a/accounts/models.py b/accounts/models.py index 5151861..15cedee 100644 --- a/accounts/models.py +++ b/accounts/models.py @@ -11,3 +11,12 @@ class UserInstance(models.Model): def __unicode__(self): return self.instance.name + + +class UserSSHKey(models.Model): + user = models.ForeignKey(User) + keyname = models.CharField(max_length=25) + keypublic = models.CharField(max_length=500) + + def __unicode__(self): + return self.keyname diff --git a/accounts/templates/profile.html b/accounts/templates/profile.html index 2a522b2..3d15393 100644 --- a/accounts/templates/profile.html +++ b/accounts/templates/profile.html @@ -1,5 +1,6 @@ {% extends "base.html" %} {% load i18n %} +{% load tags_fingerprint %} {% block title %}{% trans "Profile" %}{% endblock %} {% block content %} @@ -66,6 +67,49 @@ + + {% if publickeys %} +
+
+ + + {% for key in publickeys %} + + + + + {% endfor %} + +
{{ key.keyname }} ({% ssh_to_fingerprint key.keypublic %}) +
{% csrf_token %} + + +
+
+
+
+ {% endif %} +
{% csrf_token %} +
+ +
+ +
+
+
+ +
+ +
+
+
+
+ +
+
+
{% endblock %} \ No newline at end of file diff --git a/accounts/templatetags/__init__.py b/accounts/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/accounts/templatetags/tags_fingerprint.py b/accounts/templatetags/tags_fingerprint.py new file mode 100644 index 0000000..d15c473 --- /dev/null +++ b/accounts/templatetags/tags_fingerprint.py @@ -0,0 +1,12 @@ +from django import template +import base64 +import hashlib + +register = template.Library() + + +@register.simple_tag +def ssh_to_fingerprint(line): + key = base64.b64decode(line.strip().split()[1].encode('ascii')) + fp_plain = hashlib.md5(key).hexdigest() + return ':'.join(a+b for a, b in zip(fp_plain[::2], fp_plain[1::2])) diff --git a/accounts/views.py b/accounts/views.py index c237350..d2893bb 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -3,7 +3,7 @@ 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 accounts.models import UserInstance +from accounts.models import UserInstance, UserSSHKey from instances.models import Instance from accounts.forms import UserAddForm @@ -18,6 +18,7 @@ def profile(request): error_messages = [] user = User.objects.get(id=request.user.id) + publickeys = UserSSHKey.objects.filter(user_id=request.user.id) if request.method == 'POST': if 'username' in request.POST: @@ -41,7 +42,25 @@ def profile(request): user.set_password(password1) user.save() return HttpResponseRedirect(request.get_full_path()) - + if 'keyname' in request.POST: + keyname = request.POST.get('keyname', '') + keypublic = request.POST.get('keypublic', '') + for key in publickeys: + if keyname == key.keyname: + msg = _("Key name already exist") + error_messages.append(msg) + if keypublic == key.keypublic: + msg = _("Public key already exist") + error_messages.append(msg) + if not error_messages: + addkeypublic = UserSSHKey(user_id=request.user.id, keyname=keyname, keypublic=keypublic) + addkeypublic.save() + return HttpResponseRedirect(request.get_full_path()) + if 'keydelete' in request.POST: + keyid = request.POST.get('keyid', '') + delkeypublic = UserSSHKey.objects.get(id=keyid) + delkeypublic.delete() + return HttpResponseRedirect(request.get_full_path()) return render(request, 'profile.html', locals())