2020-10-08 11:57:51 +00:00
|
|
|
from admin.decorators import superuser_only
|
2020-10-14 08:37:46 +00:00
|
|
|
from django.conf import settings
|
2020-06-16 08:36:53 +00:00
|
|
|
from django.contrib import messages
|
2020-10-19 08:26:08 +00:00
|
|
|
from django.contrib.auth import get_user_model, update_session_auth_hash
|
2020-06-16 08:36:53 +00:00
|
|
|
from django.contrib.auth.decorators import permission_required
|
|
|
|
from django.contrib.auth.forms import PasswordChangeForm
|
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
|
2020-10-14 08:37:46 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-10-08 11:57:51 +00:00
|
|
|
from instances.models import Instance
|
2020-06-16 05:09:55 +00:00
|
|
|
|
2020-10-19 08:26:08 +00:00
|
|
|
from accounts.forms import EmailOTPForm, ProfileForm, UserSSHKeyForm
|
2016-03-23 08:00:42 +00:00
|
|
|
from accounts.models import *
|
2018-06-27 07:20:51 +00:00
|
|
|
|
2020-06-16 12:35:08 +00:00
|
|
|
from . import forms
|
2020-10-19 08:26:08 +00:00
|
|
|
from .utils import get_user_totp_device, send_email_with_otp
|
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-05-27 11:10:57 +00:00
|
|
|
publickeys = UserSSHKey.objects.filter(user_id=request.user.id)
|
2020-10-14 08:37:46 +00:00
|
|
|
profile_form = ProfileForm(request.POST or None, instance=request.user)
|
|
|
|
ssh_key_form = UserSSHKeyForm()
|
2020-05-19 16:53:54 +00:00
|
|
|
|
2020-10-14 08:37:46 +00:00
|
|
|
if profile_form.is_valid():
|
|
|
|
profile_form.save()
|
2020-11-05 09:34:31 +00:00
|
|
|
messages.success(request, _("Profile updated"))
|
|
|
|
return redirect("accounts:profile")
|
2020-10-14 08:37:46 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"profile.html",
|
|
|
|
{
|
|
|
|
"publickeys": publickeys,
|
|
|
|
"profile_form": profile_form,
|
|
|
|
"ssh_key_form": ssh_key_form,
|
|
|
|
},
|
|
|
|
)
|
2020-10-14 08:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ssh_key_create(request):
|
|
|
|
key_form = UserSSHKeyForm(request.POST or None, user=request.user)
|
|
|
|
if key_form.is_valid():
|
|
|
|
key_form.save()
|
2020-11-05 09:34:31 +00:00
|
|
|
messages.success(request, _("SSH key added"))
|
|
|
|
return redirect("accounts:profile")
|
2020-10-14 08:37:46 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"common/form.html",
|
|
|
|
{
|
|
|
|
"form": key_form,
|
|
|
|
"title": _("Add SSH key"),
|
|
|
|
},
|
|
|
|
)
|
2020-10-14 08:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def ssh_key_delete(request, pk):
|
|
|
|
ssh_key = get_object_or_404(UserSSHKey, pk=pk, user=request.user)
|
2020-11-05 09:34:31 +00:00
|
|
|
if request.method == "POST":
|
2020-10-14 08:37:46 +00:00
|
|
|
ssh_key.delete()
|
2020-11-05 09:34:31 +00:00
|
|
|
messages.success(request, _("SSH key deleted"))
|
|
|
|
return redirect("accounts:profile")
|
2020-10-14 08:37:46 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"common/confirm_delete.html",
|
|
|
|
{
|
|
|
|
"object": ssh_key,
|
|
|
|
"title": _("Delete SSH key"),
|
|
|
|
},
|
|
|
|
)
|
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
|
|
|
user = User.objects.get(id=user_id)
|
2015-03-10 14:24:10 +00:00
|
|
|
user_insts = UserInstance.objects.filter(user_id=user_id)
|
2020-09-25 12:57:17 +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
|
|
|
|
2020-10-19 08:26:08 +00:00
|
|
|
return render(
|
2020-11-05 09:34:31 +00:00
|
|
|
request,
|
|
|
|
"account.html",
|
|
|
|
{
|
|
|
|
"user": user,
|
|
|
|
"user_insts": user_insts,
|
|
|
|
"instances": instances,
|
|
|
|
"publickeys": publickeys,
|
|
|
|
"otp_enabled": settings.OTP_ENABLED,
|
|
|
|
},
|
|
|
|
)
|
2020-06-16 08:36:53 +00:00
|
|
|
|
|
|
|
|
2020-09-25 12:57:17 +00:00
|
|
|
@permission_required("accounts.change_password", raise_exception=True)
|
2020-06-16 08:36:53 +00:00
|
|
|
def change_password(request):
|
2020-10-14 08:37:46 +00:00
|
|
|
form = PasswordChangeForm(request.user, request.POST or None)
|
|
|
|
|
|
|
|
if form.is_valid():
|
|
|
|
user = form.save()
|
|
|
|
update_session_auth_hash(request, user) # Important!
|
|
|
|
messages.success(request, _("Password Changed"))
|
|
|
|
return redirect("accounts:profile")
|
|
|
|
|
2020-10-08 11:57:51 +00:00
|
|
|
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)
|
|
|
|
|
2020-09-25 12:57:17 +00:00
|
|
|
form = forms.UserInstanceForm(request.POST or None, initial={"user": user})
|
2020-06-16 12:35:08 +00:00
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2020-10-14 08:37:46 +00:00
|
|
|
return redirect(reverse("accounts:account", args=[user.id]))
|
2020-06-16 12:35:08 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"common/form.html",
|
2020-06-16 12:35:08 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"form": form,
|
|
|
|
"title": _("Create User Instance"),
|
2020-06-16 12:35:08 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@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()
|
2020-10-14 08:37:46 +00:00
|
|
|
return redirect(reverse("accounts:account", args=[user_instance.user.id]))
|
2020-06-16 12:35:08 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-11-05 09:34:31 +00:00
|
|
|
"common/form.html",
|
2020-06-16 12:35:08 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"form": form,
|
|
|
|
"title": _("Update User Instance"),
|
2020-06-16 12:35:08 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_instance_delete(request, pk):
|
|
|
|
user_instance = get_object_or_404(UserInstance, pk=pk)
|
2020-09-25 12:57:17 +00:00
|
|
|
if request.method == "POST":
|
2020-06-16 12:35:08 +00:00
|
|
|
user = user_instance.user
|
|
|
|
user_instance.delete()
|
2020-09-25 12:57:17 +00:00
|
|
|
next = request.GET.get("next", None)
|
2020-07-13 09:33:09 +00:00
|
|
|
if next:
|
|
|
|
return redirect(next)
|
|
|
|
else:
|
2020-10-14 08:37:46 +00:00
|
|
|
return redirect(reverse("accounts:account", args=[user.id]))
|
2020-06-16 12:35:08 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"common/confirm_delete.html",
|
|
|
|
{"object": user_instance},
|
2020-06-16 12:35:08 +00:00
|
|
|
)
|
2020-10-19 08:26:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
def email_otp(request):
|
|
|
|
form = EmailOTPForm(request.POST or None)
|
|
|
|
if form.is_valid():
|
|
|
|
UserModel = get_user_model()
|
|
|
|
try:
|
2020-11-05 09:34:31 +00:00
|
|
|
user = UserModel.objects.get(email=form.cleaned_data["email"])
|
2020-10-19 08:26:08 +00:00
|
|
|
except UserModel.DoesNotExist:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
device = get_user_totp_device(user)
|
|
|
|
send_email_with_otp(user, device)
|
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
messages.success(request, _("OTP Sent to %(email)s") % {"email": form.cleaned_data["email"]})
|
|
|
|
return redirect("accounts:login")
|
2020-10-19 08:26:08 +00:00
|
|
|
|
2020-11-05 09:34:31 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"accounts/email_otp_form.html",
|
|
|
|
{
|
|
|
|
"form": form,
|
|
|
|
"title": _("Email OTP"),
|
|
|
|
},
|
|
|
|
)
|
2020-10-19 08:26:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def admin_email_otp(request, user_id):
|
|
|
|
user = get_object_or_404(get_user_model(), pk=user_id)
|
|
|
|
device = get_user_totp_device(user)
|
2020-11-05 09:34:31 +00:00
|
|
|
if user.email != "":
|
2020-10-19 11:11:28 +00:00
|
|
|
send_email_with_otp(user, device)
|
2020-11-05 09:34:31 +00:00
|
|
|
messages.success(request, _("OTP QR code was emailed to user %(user)s") % {"user": user})
|
2020-10-19 11:11:28 +00:00
|
|
|
else:
|
2020-11-05 09:34:31 +00:00
|
|
|
messages.error(request, _("User email not set, failed to send QR code"))
|
|
|
|
return redirect("accounts:account", user.id)
|