217 lines
7.0 KiB
Python
217 lines
7.0 KiB
Python
from django import forms
|
|
from django.conf import settings
|
|
from django.contrib import messages
|
|
from django.core.exceptions import PermissionDenied, ValidationError
|
|
from django.db import transaction
|
|
from django.http import Http404
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
from django.views.decorators.cache import never_cache
|
|
from django.views.decorators.http import (
|
|
require_http_methods,
|
|
require_POST,
|
|
require_safe,
|
|
)
|
|
|
|
from .access import customer_for_user
|
|
from .account_backups import queue_account_backup
|
|
from .credential_views import DatabaseCredentialView
|
|
from .installation_features import panel_feature_allowed
|
|
from .models import DatabaseLogin, HostingAccount, HostingDatabase
|
|
from .phpmyadmin import phpmyadmin_available
|
|
from .resource_requests import lock_hosting_account, require_active_services
|
|
from .service_credentials import CredentialKind, enabled_credential_access
|
|
from .sftp import site_is_active
|
|
|
|
|
|
class AddDatabaseLoginForm(forms.Form):
|
|
label = forms.CharField(max_length=80)
|
|
access = forms.ChoiceField(choices=DatabaseLogin.Access.choices)
|
|
|
|
|
|
def signed_in_customer(request):
|
|
customer = customer_for_user(request.user)
|
|
if customer is None or getattr(request, "impersonation", None) is not None:
|
|
raise PermissionDenied
|
|
return customer
|
|
|
|
|
|
@require_safe
|
|
@never_cache
|
|
def customer_databases(request, pk):
|
|
customer = signed_in_customer(request)
|
|
account = get_object_or_404(
|
|
HostingAccount, pk=pk, customer=customer, retired_at__isnull=True
|
|
)
|
|
can_reveal = enabled_credential_access(request, CredentialKind.DATABASE)
|
|
return render(
|
|
request,
|
|
"hosting/customer_databases.html",
|
|
{
|
|
"account": account,
|
|
"databases": account.databases.select_related(
|
|
"provisioning"
|
|
).prefetch_related("logins"),
|
|
"show_sso": phpmyadmin_available(request),
|
|
"can_reveal": can_reveal,
|
|
},
|
|
)
|
|
|
|
|
|
@transaction.atomic
|
|
def update_database_login(
|
|
database, customer, action, *, login_id=None, data=None
|
|
):
|
|
account = lock_hosting_account(database.account_id, customer.pk)
|
|
require_active_services(account)
|
|
if (
|
|
getattr(getattr(database, "provisioning", None), "state", "")
|
|
!= "configured"
|
|
):
|
|
raise ValidationError("Finish provisioning the database first.")
|
|
if action == "add":
|
|
if database.logins.count() >= 25:
|
|
raise ValidationError(
|
|
"This database already has 25 managed logins."
|
|
)
|
|
DatabaseLogin.objects.create(
|
|
database=database,
|
|
requested_customer_id=customer.pk,
|
|
**data,
|
|
)
|
|
return
|
|
if (
|
|
not login_id
|
|
or not login_id.isascii()
|
|
or not login_id.isdecimal()
|
|
or len(login_id) > 18
|
|
):
|
|
raise Http404
|
|
login = get_object_or_404(database.logins, pk=int(login_id))
|
|
if login.finished_at is None:
|
|
raise ValidationError("Wait for the current login operation.")
|
|
DatabaseLogin.objects.requeue(
|
|
database.logins.filter(pk=login.pk),
|
|
action="remove",
|
|
requested_customer_id=customer.pk,
|
|
)
|
|
|
|
|
|
@require_http_methods(("GET", "HEAD", "POST"))
|
|
@never_cache
|
|
def database_login_editor(request, pk):
|
|
customer = signed_in_customer(request)
|
|
if not panel_feature_allowed("LOCAL_DATABASE_PROVISIONING"):
|
|
raise Http404
|
|
database = get_object_or_404(
|
|
HostingDatabase.objects.select_related("account", "provisioning"),
|
|
account__retired_at__isnull=True,
|
|
account__customer=customer,
|
|
pk=pk,
|
|
)
|
|
action = request.POST.get("action")
|
|
form = AddDatabaseLoginForm(request.POST if action == "add" else None)
|
|
problem = ""
|
|
if request.method == "POST":
|
|
if action not in {"add", "remove"}:
|
|
raise Http404
|
|
if action == "remove" or form.is_valid():
|
|
try:
|
|
update_database_login(
|
|
database,
|
|
customer,
|
|
action,
|
|
login_id=request.POST.get("login"),
|
|
data=form.cleaned_data if action == "add" else None,
|
|
)
|
|
except HostingAccount.DoesNotExist:
|
|
raise Http404 from None
|
|
except ValidationError as error:
|
|
problem = " ".join(error.messages)
|
|
else:
|
|
return redirect("panel:database_logins", pk=pk)
|
|
return render(
|
|
request,
|
|
"hosting/database_logins.html",
|
|
{
|
|
"database": database,
|
|
"form": form,
|
|
"problem": problem,
|
|
"logins": database.logins.order_by("label"),
|
|
"can_export": can_export_database(database),
|
|
"export_job": database.account.backup_jobs.filter(
|
|
export_database_suffix=database.suffix,
|
|
export_customer_id=customer.pk,
|
|
)
|
|
.order_by("-pk")
|
|
.first(),
|
|
"show_sso": phpmyadmin_available(request),
|
|
"can_reveal": enabled_credential_access(
|
|
request, CredentialKind.DATABASE
|
|
),
|
|
},
|
|
)
|
|
|
|
|
|
def can_export_database(database):
|
|
return (
|
|
panel_feature_allowed("LOCAL_ACCOUNT_BACKUPS")
|
|
and settings.LOCAL_WEB_SFTP
|
|
and site_is_active(database.account)
|
|
and not database.account.services_suspended
|
|
and getattr(getattr(database, "provisioning", None), "state", "")
|
|
== "configured"
|
|
)
|
|
|
|
|
|
@require_POST
|
|
@never_cache
|
|
def database_sql_export(request, pk):
|
|
customer = signed_in_customer(request)
|
|
database = get_object_or_404(
|
|
HostingDatabase.objects.select_related("account", "provisioning"),
|
|
pk=pk,
|
|
account__customer=customer,
|
|
account__retired_at__isnull=True,
|
|
)
|
|
if not can_export_database(database):
|
|
raise Http404
|
|
try:
|
|
job = queue_account_backup(
|
|
database.account_id,
|
|
customer_id=customer.pk,
|
|
include_website=False,
|
|
database_suffix=database.suffix,
|
|
)
|
|
except HostingAccount.DoesNotExist:
|
|
raise Http404 from None
|
|
except ValidationError as error:
|
|
messages.error(request, " ".join(error.messages))
|
|
else:
|
|
if job:
|
|
messages.success(request, "SQL export queued.")
|
|
else:
|
|
messages.error(
|
|
request, "Wait for the current account backup to finish."
|
|
)
|
|
return redirect("panel:database_logins", pk=database.pk)
|
|
|
|
|
|
class DatabaseLoginCredentialView(DatabaseCredentialView):
|
|
queryset = DatabaseLogin.objects.filter(
|
|
finished_at__isnull=False,
|
|
problem="",
|
|
action="grant",
|
|
database__account__retired_at__isnull=True,
|
|
database__account__services_suspended=False,
|
|
).select_related("database__account")
|
|
customer_relation = "database__account__customer"
|
|
|
|
@property
|
|
def account(self):
|
|
return self.resource.database.account
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context.pop("rotation_url", None)
|
|
return context
|