2020-03-12 08:24:38 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
|
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from django.views import View
|
|
|
|
from django.views.generic.edit import UpdateView
|
|
|
|
|
|
|
|
from common.utils import get_logger, ssh_key_gen
|
|
|
|
from common.permissions import (
|
2021-04-08 06:59:14 +00:00
|
|
|
IsValidUser,
|
2020-03-12 08:24:38 +00:00
|
|
|
UserCanUpdateSSHKey,
|
|
|
|
)
|
2021-04-08 06:59:14 +00:00
|
|
|
from common.mixins.views import PermissionsMixin
|
2020-03-12 08:24:38 +00:00
|
|
|
from ... import forms
|
|
|
|
from ...models import User
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'UserPublicKeyUpdateView', 'UserPublicKeyGenerateView',
|
|
|
|
]
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class UserPublicKeyUpdateView(PermissionsMixin, UpdateView):
|
|
|
|
template_name = 'users/user_pubkey_update.html'
|
|
|
|
model = User
|
|
|
|
form_class = forms.UserPublicKeyForm
|
|
|
|
permission_classes = [IsValidUser, UserCanUpdateSSHKey]
|
|
|
|
success_url = reverse_lazy('users:user-profile')
|
|
|
|
|
|
|
|
def get_object(self, queryset=None):
|
|
|
|
return self.request.user
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = {
|
|
|
|
'app': _('Users'),
|
|
|
|
'action': _('Public key update'),
|
|
|
|
}
|
|
|
|
kwargs.update(context)
|
|
|
|
return super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class UserPublicKeyGenerateView(PermissionsMixin, View):
|
|
|
|
permission_classes = [IsValidUser]
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
username = request.user.username
|
2020-03-19 08:28:08 +00:00
|
|
|
private, public = ssh_key_gen(username=username, hostname='jumpserver')
|
2020-06-04 09:05:36 +00:00
|
|
|
request.user.set_public_key(public)
|
2020-03-12 08:24:38 +00:00
|
|
|
response = HttpResponse(private, content_type='text/plain')
|
|
|
|
filename = "{0}-jumpserver.pem".format(username)
|
|
|
|
response['Content-Disposition'] = 'attachment; filename={}'.format(filename)
|
|
|
|
return response
|