2020-06-16 05:09:55 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
import sass
|
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-05-27 12:24:06 +00:00
|
|
|
from django.shortcuts import 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
|
2020-05-28 21:43:26 +00:00
|
|
|
from appsettings.models import AppSettings
|
2015-03-11 09:39:44 +00:00
|
|
|
from instances.models import Instance
|
2018-06-27 07:20:51 +00:00
|
|
|
|
2015-03-02 08:52:07 +00:00
|
|
|
|
2015-03-18 15:13:44 +00:00
|
|
|
def profile(request):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-03-24 12:45:38 +00:00
|
|
|
error_messages = []
|
2020-05-27 12:24:06 +00:00
|
|
|
# user = User.objects.get(id=request.user.id)
|
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', '')
|
|
|
|
user.first_name = username
|
|
|
|
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())
|
|
|
|
if 'oldpasswd' in request.POST:
|
|
|
|
oldpasswd = request.POST.get('oldpasswd', '')
|
|
|
|
password1 = request.POST.get('passwd1', '')
|
|
|
|
password2 = request.POST.get('passwd2', '')
|
2015-03-25 09:36:37 +00:00
|
|
|
if not password1 or not password2:
|
|
|
|
error_messages.append("Passwords didn't enter")
|
2015-03-24 12:45:38 +00:00
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
error_messages.append("Passwords don't match")
|
2020-05-27 12:24:06 +00:00
|
|
|
if not request.user.check_password(oldpasswd):
|
2015-03-24 12:45:38 +00:00
|
|
|
error_messages.append("Old password is wrong!")
|
|
|
|
if not error_messages:
|
2020-05-27 12:24:06 +00:00
|
|
|
request.user.set_password(password1)
|
|
|
|
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-02 08:52:07 +00:00
|
|
|
"""
|
|
|
|
:param request:
|
2019-09-10 06:48:31 +00:00
|
|
|
:param user_id:
|
2015-03-02 08:52:07 +00:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
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
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'delete' in request.POST:
|
|
|
|
user_inst = request.POST.get('user_inst', '')
|
|
|
|
del_user_inst = UserInstance.objects.get(id=user_inst)
|
|
|
|
del_user_inst.delete()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-16 13:46:44 +00:00
|
|
|
if 'permission' in request.POST:
|
2015-03-10 14:24:10 +00:00
|
|
|
user_inst = request.POST.get('user_inst', '')
|
2016-05-09 10:08:31 +00:00
|
|
|
inst_vnc = request.POST.get('inst_vnc', '')
|
2015-03-10 14:24:10 +00:00
|
|
|
inst_change = request.POST.get('inst_change', '')
|
|
|
|
inst_delete = request.POST.get('inst_delete', '')
|
|
|
|
edit_user_inst = UserInstance.objects.get(id=user_inst)
|
|
|
|
edit_user_inst.is_change = bool(inst_change)
|
|
|
|
edit_user_inst.is_delete = bool(inst_delete)
|
2016-05-09 10:08:31 +00:00
|
|
|
edit_user_inst.is_vnc = bool(inst_vnc)
|
2015-03-10 14:24:10 +00:00
|
|
|
edit_user_inst.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-11 09:39:44 +00:00
|
|
|
if 'add' in request.POST:
|
|
|
|
inst_id = request.POST.get('inst_id', '')
|
2020-06-04 12:22:55 +00:00
|
|
|
|
2020-05-28 21:43:26 +00:00
|
|
|
if AppSettings.objects.get(key="ALLOW_INSTANCE_MULTIPLE_OWNER").value == 'True':
|
2016-01-19 14:43:56 +00:00
|
|
|
check_inst = UserInstance.objects.filter(instance_id=int(inst_id), user_id=int(user_id))
|
2016-01-15 09:12:03 +00:00
|
|
|
else:
|
2016-01-19 14:43:56 +00:00
|
|
|
check_inst = UserInstance.objects.filter(instance_id=int(inst_id))
|
2020-05-27 12:24:06 +00:00
|
|
|
|
2016-01-19 14:43:56 +00:00
|
|
|
if check_inst:
|
2015-03-11 09:39:44 +00:00
|
|
|
msg = _("Instance already added")
|
|
|
|
error_messages.append(msg)
|
2016-01-19 14:43:56 +00:00
|
|
|
else:
|
|
|
|
add_user_inst = UserInstance(instance_id=int(inst_id), user_id=int(user_id))
|
2015-03-11 09:39:44 +00:00
|
|
|
add_user_inst.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-02 08:52:07 +00:00
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'account.html', locals())
|