2020-06-16 05:09:55 +00:00
|
|
|
import os
|
|
|
|
|
2020-06-16 08:36:53 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth import update_session_auth_hash
|
|
|
|
from django.contrib.auth.decorators import permission_required
|
|
|
|
from django.contrib.auth.forms import PasswordChangeForm
|
2020-05-27 12:24:06 +00:00
|
|
|
from django.core.validators import ValidationError
|
2015-03-03 09:34:09 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2020-06-16 12:35:08 +00:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
2020-01-24 07:09:46 +00:00
|
|
|
from django.urls import reverse
|
2015-03-11 09:39:44 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-06-16 05:09:55 +00:00
|
|
|
|
2016-03-23 08:00:42 +00:00
|
|
|
from accounts.models import *
|
2020-05-27 12:24:06 +00:00
|
|
|
from admin.decorators import superuser_only
|
2015-03-11 09:39:44 +00:00
|
|
|
from instances.models import Instance
|
2018-06-27 07:20:51 +00:00
|
|
|
|
2020-06-16 12:35:08 +00:00
|
|
|
from . import forms
|
2015-03-02 08:52:07 +00:00
|
|
|
|
2015-03-18 15:13:44 +00:00
|
|
|
|
2020-06-16 12:35:08 +00:00
|
|
|
def profile(request):
|
2015-03-24 12:45:38 +00:00
|
|
|
error_messages = []
|
2015-05-27 11:10:57 +00:00
|
|
|
publickeys = UserSSHKey.objects.filter(user_id=request.user.id)
|
2020-05-19 16:53:54 +00:00
|
|
|
|
2015-03-24 12:45:38 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
if 'username' in request.POST:
|
|
|
|
username = request.POST.get('username', '')
|
|
|
|
email = request.POST.get('email', '')
|
2020-09-25 06:21:37 +00:00
|
|
|
request.user.first_name = username
|
|
|
|
request.user.email = email
|
2020-05-27 12:24:06 +00:00
|
|
|
request.user.save()
|
2015-03-24 12:45:38 +00:00
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-05-27 11:10:57 +00:00
|
|
|
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)
|
2018-07-26 09:33:12 +00:00
|
|
|
if '\n' in keypublic or '\r' in keypublic:
|
|
|
|
msg = _("Invalid characters in public key")
|
|
|
|
error_messages.append(msg)
|
2015-05-27 11:10:57 +00:00
|
|
|
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())
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'profile.html', locals())
|
2015-03-18 15:13:44 +00:00
|
|
|
|
2019-09-10 06:48:31 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
2015-03-18 15:13:44 +00:00
|
|
|
def account(request, user_id):
|
2015-03-11 09:39:44 +00:00
|
|
|
error_messages = []
|
2015-03-02 08:52:07 +00:00
|
|
|
user = User.objects.get(id=user_id)
|
2015-03-10 14:24:10 +00:00
|
|
|
user_insts = UserInstance.objects.filter(user_id=user_id)
|
2016-03-21 09:24:09 +00:00
|
|
|
instances = Instance.objects.all().order_by('name')
|
2017-11-15 11:55:05 +00:00
|
|
|
publickeys = UserSSHKey.objects.filter(user_id=user_id)
|
2015-03-10 14:24:10 +00:00
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'account.html', locals())
|
2020-06-16 08:36:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@permission_required('accounts.change_password', raise_exception=True)
|
|
|
|
def change_password(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = PasswordChangeForm(request.user, request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
user = form.save()
|
|
|
|
update_session_auth_hash(request, user) # Important!
|
|
|
|
messages.success(request, _('Password Changed'))
|
|
|
|
return redirect('profile')
|
|
|
|
else:
|
|
|
|
messages.error(request, _('Wrong Data Provided'))
|
|
|
|
else:
|
|
|
|
form = PasswordChangeForm(request.user)
|
|
|
|
return render(request, 'accounts/change_password_form.html', {'form': form})
|
2020-06-16 12:35:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_instance_create(request, user_id):
|
|
|
|
user = get_object_or_404(User, pk=user_id)
|
|
|
|
|
|
|
|
form = forms.UserInstanceForm(request.POST or None, initial={'user': user})
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect(reverse('account', args=[user.id]))
|
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
'common/form.html',
|
|
|
|
{
|
|
|
|
'form': form,
|
|
|
|
'title': _('Create User Instance'),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_instance_update(request, pk):
|
|
|
|
user_instance = get_object_or_404(UserInstance, pk=pk)
|
|
|
|
form = forms.UserInstanceForm(request.POST or None, instance=user_instance)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect(reverse('account', args=[user_instance.user.id]))
|
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
'common/form.html',
|
|
|
|
{
|
|
|
|
'form': form,
|
|
|
|
'title': _('Update User Instance'),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_instance_delete(request, pk):
|
|
|
|
user_instance = get_object_or_404(UserInstance, pk=pk)
|
|
|
|
if request.method == 'POST':
|
|
|
|
user = user_instance.user
|
|
|
|
user_instance.delete()
|
2020-07-13 09:33:09 +00:00
|
|
|
next = request.GET.get('next', None)
|
|
|
|
if next:
|
|
|
|
return redirect(next)
|
|
|
|
else:
|
|
|
|
return redirect(reverse('account', args=[user.id]))
|
2020-06-16 12:35:08 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
'common/confirm_delete.html',
|
|
|
|
{'object': user_instance},
|
|
|
|
)
|