diff --git a/docs/deployment.md b/docs/deployment.md index 228dfef..eb04269 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -2709,6 +2709,10 @@ password. Preview the email before creating the account. To add a login for an existing unassigned hosting account, open the account and choose **Add customer login** or **Link existing customer**. +Operators can open **Contact and invitation** from **Show All Users** to +review the welcome email and send a fresh password setup link to a pending +login. Sending a new invitation replaces the previous link. + Managers handle customer onboarding and hosting assignments in their portfolio. Customers manage their own website files, databases, mailboxes, and other hosting resources in the customer workspace. Operators can diff --git a/src/coulobre/hosting/account_administration.py b/src/coulobre/hosting/account_administration.py index c0bdf22..11097a5 100644 --- a/src/coulobre/hosting/account_administration.py +++ b/src/coulobre/hosting/account_administration.py @@ -277,6 +277,10 @@ class AllUsersView(OperatorOnlyMixin, PermissionRequiredMixin, ListView): (login, measurements.get(profile.pk) if profile else None) for login, profile in zip(context["logins"], profiles, strict=True) ] + actor = self.request.user + context["may_manage_invitations"] = actor.has_perm( + "hosting.change_customer" + ) or actor.has_perm("hosting.change_reseller") return context diff --git a/src/coulobre/hosting/invitations.py b/src/coulobre/hosting/invitations.py index 5d35ba0..cedb495 100644 --- a/src/coulobre/hosting/invitations.py +++ b/src/coulobre/hosting/invitations.py @@ -17,8 +17,10 @@ from django.shortcuts import get_object_or_404, redirect from django.template.loader import render_to_string from django.urls import reverse, reverse_lazy from django.utils import timezone +from django.utils.decorators import method_decorator from django.utils.encoding import force_bytes from django.utils.http import urlsafe_base64_encode +from django.views.decorators.cache import never_cache from django.views.decorators.debug import sensitive_variables from django.views.generic import FormView from kombu.exceptions import OperationalError @@ -143,7 +145,7 @@ def enqueue_delivery(invitation_id, revision): ).update(delivery=PanelInvitation.Delivery.FAILED) -@sensitive_variables("path", "message") +@sensitive_variables("path", "context") def deliver_invitation(invitation_id, revision): pending = PanelInvitation.objects.filter( pk=invitation_id, @@ -166,6 +168,28 @@ def deliver_invitation(invitation_id, revision): "token": invitation_token.make_token(user), }, ) + context = invitation_message_context(user, setup_url=origin + path) + try: + sent = send_mail( + "Welcome to Coulobre Panel", + render_to_string("hosting/panel_invitation.txt", context), + None, + [user.email], + html_message=render_to_string( + "hosting/panel_invitation.html", + {"invitation_content": context}, + ), + ) + if sent != 1: + raise OSError("The email backend did not accept the invitation.") + except (OSError, ValueError): + pending.update(delivery=PanelInvitation.Delivery.FAILED) + else: + pending.update(delivery=PanelInvitation.Delivery.SENT) + + +def invitation_message_context(user, *, setup_url=""): + invitation = getattr(user, "panel_invitation", None) account = ( HostingAccount.objects.filter( customer__user=user, retired_at__isnull=True @@ -179,10 +203,10 @@ def deliver_invitation(invitation_id, revision): if account else None ) - message = render_welcome_message( + return welcome_message_context( username=user.get_username(), - welcome_message=invitation.welcome_message, - setup_url=origin + path, + welcome_message=invitation.welcome_message if invitation else "", + setup_url=setup_url, account=account, nameservers=zone.nameservers if zone else (), mail_configured=bool( @@ -192,19 +216,9 @@ def deliver_invitation(invitation_id, revision): ).exists() ), ) - try: - sent = send_mail( - "Your Coulobre Panel invitation", message, None, [user.email] - ) - if sent != 1: - raise OSError("The email backend did not accept the invitation.") - except (OSError, ValueError): - pending.update(delivery=PanelInvitation.Delivery.FAILED) - else: - pending.update(delivery=PanelInvitation.Delivery.SENT) -def render_welcome_message( +def welcome_message_context( *, username, welcome_message, @@ -213,24 +227,31 @@ def render_welcome_message( nameservers, mail_configured, ): + return { + "username": username, + "welcome_message": welcome_message, + "setup_url": setup_url, + "panel_url": ( + settings.PASSWORD_RESET_ORIGIN.rstrip("/") + "/" + if settings.PASSWORD_RESET_ORIGIN + else "" + ), + "account": account, + "nameservers": nameservers, + "website_records": ( + website_records(account.primary_domain, wildcard=False) + if account and not nameservers + else () + ), + "imap_host": settings.MAIL_IMAP_HOST, + "submission_host": settings.MAIL_SUBMISSION_HOST, + "mail_configured": mail_configured, + } + + +def render_welcome_message(**kwargs): return render_to_string( - "hosting/panel_invitation.txt", - { - "username": username, - "welcome_message": welcome_message, - "setup_url": setup_url, - "panel_url": settings.PASSWORD_RESET_ORIGIN.rstrip("/") + "/", - "account": account, - "nameservers": nameservers, - "website_records": ( - website_records(account.primary_domain, wildcard=False) - if account and not nameservers - else () - ), - "imap_host": settings.MAIL_IMAP_HOST, - "submission_host": settings.MAIL_SUBMISSION_HOST, - "mail_configured": mail_configured, - }, + "hosting/panel_invitation.txt", welcome_message_context(**kwargs) ) @@ -259,6 +280,7 @@ class InvitationEmailForm(forms.Form): email = forms.EmailField(label="Contact email") +@method_decorator(never_cache, name="dispatch") class ManageInvitation(AccessMixin, FormView): form_class = InvitationEmailForm template_name = "hosting/manage_invitation.html" @@ -292,10 +314,14 @@ class ManageInvitation(AccessMixin, FormView): def get_context_data(self, **kwargs): user = self.record.user + can_send = user.is_active and not user.has_usable_password() return super().get_context_data( login_user=user, invitation=getattr(user, "panel_invitation", None), - can_send=user.is_active and not user.has_usable_password(), + can_send=can_send, + welcome_preview=( + invitation_message_context(user) if can_send else None + ), **kwargs, ) diff --git a/src/coulobre/hosting/templates/hosting/all_users.html b/src/coulobre/hosting/templates/hosting/all_users.html index 16c91bf..3303601 100644 --- a/src/coulobre/hosting/templates/hosting/all_users.html +++ b/src/coulobre/hosting/templates/hosting/all_users.html @@ -18,11 +18,29 @@
- + {% if may_manage_invitations %}{% endif %} {% for login, usage in login_rows %} - {% include "hosting/account_metrics_cells.html" %} - {% empty %}{% endfor %} + + + + + + {% include "hosting/account_metrics_cells.html" %} + + {% if may_manage_invitations %} + + {% endif %} + + {% empty %}{% endfor %}
UserWorkspaceManagerHosting accountsDiskMonthly trafficDomainsDatabasesMailboxesAdded
UserWorkspaceManagerHosting accountsDiskMonthly trafficDomainsDatabasesMailboxesAddedActions
{{ login.get_username }}{% if login.is_superuser %}Operator{% elif login.hosting_reseller %}Manager{% else %}Customer{% endif %}{{ login.hosting_customer.reseller|default:"Unassigned" }}{{ login.account_count }}{{ login.date_joined|date:"j M Y" }}
No logins found.
{{ login.get_username }}{% if login.is_superuser %}Operator{% elif login.hosting_reseller %}Manager{% else %}Customer{% endif %}{{ login.hosting_customer.reseller|default:"Unassigned" }}{{ login.account_count }}{{ login.date_joined|date:"j M Y" }} + {% if not login.is_staff and not login.is_superuser %} + {% if login.hosting_reseller and perms.hosting.change_reseller %} + Contact and invitation + {% elif not login.hosting_reseller and login.hosting_customer and perms.hosting.change_customer %} + Contact and invitation + {% endif %} + {% endif %} +
No logins found.
diff --git a/src/coulobre/hosting/templates/hosting/manage_invitation.html b/src/coulobre/hosting/templates/hosting/manage_invitation.html index 4deef72..38e9e6b 100644 --- a/src/coulobre/hosting/templates/hosting/manage_invitation.html +++ b/src/coulobre/hosting/templates/hosting/manage_invitation.html @@ -13,5 +13,14 @@ {% if can_send %}{% endif %} + {% if welcome_preview %} +
+ Welcome email preview +

A password setup link is added when the invitation is sent.

+
+ {% include "hosting/panel_invitation_body.html" with message=welcome_preview %} +
+
+ {% endif %}
{% endblock %} diff --git a/src/coulobre/hosting/templates/hosting/panel_invitation.html b/src/coulobre/hosting/templates/hosting/panel_invitation.html new file mode 100644 index 0000000..82e7d7a --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/panel_invitation.html @@ -0,0 +1,19 @@ + + + + + + Welcome to Coulobre Panel + + + + + + +
+
+ {% include "hosting/panel_invitation_body.html" with message=invitation_content email_format=True %} +
+
+ + diff --git a/src/coulobre/hosting/templates/hosting/panel_invitation.txt b/src/coulobre/hosting/templates/hosting/panel_invitation.txt index 303e717..0ce6178 100644 --- a/src/coulobre/hosting/templates/hosting/panel_invitation.txt +++ b/src/coulobre/hosting/templates/hosting/panel_invitation.txt @@ -1,9 +1,11 @@ -{% if welcome_message %}{{ welcome_message }} +{% autoescape off %}{% if welcome_message %}{{ welcome_message }} {% endif %}Welcome to Coulobre Panel. -Username: {{ username }} -Panel: {{ panel_url }} +Your hosting provider created a panel login for you. + +Username: {{ username }}{% if panel_url %} +Panel: {{ panel_url }}{% endif %} Set your password by opening this link within 48 hours: {{ setup_url }} @@ -32,3 +34,4 @@ After signing in, open the hosting account for website status, DNS settings, and instructions for configuring email. {% else %}Once you have set your password, sign in to see the hosting you can manage. {% endif %} +{% endautoescape %} diff --git a/src/coulobre/hosting/templates/hosting/panel_invitation_body.html b/src/coulobre/hosting/templates/hosting/panel_invitation_body.html new file mode 100644 index 0000000..7f26720 --- /dev/null +++ b/src/coulobre/hosting/templates/hosting/panel_invitation_body.html @@ -0,0 +1,46 @@ +Welcome to Coulobre Panel +{% if message.welcome_message %}{{ message.welcome_message|linebreaks }}{% endif %} +

Your hosting provider created a panel login for you. Set your password within 48 hours.

+

Username {{ message.username }}

+{% if message.setup_url %} +

Set your password

+{% else %} +

Set your password

+{% endif %} +{% if message.panel_url %}

Open Coulobre Panel

{% endif %} +{% if message.account %} +

Your hosting

+

Domain {{ message.account.primary_domain }}

+ {% with package=message.account.hosting_plan %} + {% if package %} +

Hosting package {{ package.name }}

+ + {% endif %} + {% endwith %} + {% if message.nameservers %} +

DNS

+

To use Coulobre DNS, set these nameservers at your registrar.

+ + {% elif message.website_records %} +

DNS

+

If your DNS is hosted elsewhere, add these records at your DNS provider.

+ + {% endif %} + {% if message.mail_configured %} + {% if message.imap_host or message.submission_host %} +

Connect your email client

+

Use your full mailbox address as the username.

+ + {% endif %} + {% endif %} +{% endif %} diff --git a/src/coulobre/hosting/templates/hosting/workspace_form.html b/src/coulobre/hosting/templates/hosting/workspace_form.html index 19987af..6836e5d 100644 --- a/src/coulobre/hosting/templates/hosting/workspace_form.html +++ b/src/coulobre/hosting/templates/hosting/workspace_form.html @@ -9,7 +9,7 @@ {% if welcome_preview %}
Welcome email preview -
{{ welcome_preview }}
+
{{ welcome_preview|force_escape }}
{% endif %} {% if moving %} diff --git a/src/coulobre/hosting/test_panel_authentication.py b/src/coulobre/hosting/test_panel_authentication.py index f88f19a..c303cf6 100644 --- a/src/coulobre/hosting/test_panel_authentication.py +++ b/src/coulobre/hosting/test_panel_authentication.py @@ -15,7 +15,13 @@ from django_otp.plugins.otp_email.models import EmailDevice from django_otp.plugins.otp_totp.models import TOTPDevice from .invitations import deliver_invitation, invitation_token, queue_invitation -from .models import Customer, HostingAccount, PanelRecoveryCode, Reseller +from .models import ( + Customer, + HostingAccount, + PanelInvitation, + PanelRecoveryCode, + Reseller, +) from .sign_in_limits import client_address from .tasks import send_customer_password_reset @@ -128,6 +134,91 @@ class PanelAuthenticationTests(TestCase): self.assertTrue(user.check_password(NEW_PASSWORD)) self.assertContains(self.client.get(link), "invitation is unavailable") + def test_add_user_delivers_welcome_email_and_list_opens_resend(self): + self.client.force_login(self.operator) + device = TOTPDevice.objects.create(user=self.operator, name="Panel") + session = self.client.session + session["otp_device_id"] = device.persistent_id + session.save() + recipient = "new@example.test" + + def run_delivery(*, args, retry): + self.assertFalse(retry) + deliver_invitation(*args) + + with ( + patch( + "coulobre.hosting.tasks.send_panel_invitation.apply_async", + side_effect=run_delivery, + ), + self.captureOnCommitCallbacks(execute=True), + ): + response = self.client.post( + reverse("panel:customer_create"), + {"username": "new-user", "email": recipient}, + ) + self.assertRedirects(response, reverse("panel:customers")) + customer = Customer.objects.get(user__email=recipient) + self.assertFalse(customer.user.has_usable_password()) + self.assertEqual( + customer.user.panel_invitation.delivery, + PanelInvitation.Delivery.SENT, + ) + self.assertEqual(len(mail.outbox), 1) + invitation = mail.outbox[0] + self.assertEqual(invitation.to, [recipient]) + self.assertIn("Username: new-user", invitation.body) + self.assertIn("48 hours", invitation.body) + link = self.invitation_path(invitation.body) + html, content_type = invitation.alternatives[0] + self.assertEqual(content_type, "text/html") + self.assertIn("Welcome to Coulobre Panel", html) + self.assertIn(f'https://panel.example.test{link}"', html) + + manage_url = reverse( + "panel:manage_invitation", args=["customer", customer.pk] + ) + self.assertContains( + self.client.get(reverse("panel:all_users")), manage_url + ) + PanelInvitation.objects.filter(user=customer.user).update( + welcome_message="Use your own password." + ) + preview = self.client.get(manage_url) + self.assertContains(preview, "Welcome email preview") + self.assertContains(preview, "Set your password") + self.assertContains( + preview, "Use <b>your own password</b>." + ) + self.assertNotContains(preview, link) + self.assertIn("no-store", preview.headers["Cache-Control"]) + + with ( + patch( + "coulobre.hosting.tasks.send_panel_invitation.apply_async", + side_effect=run_delivery, + ), + self.captureOnCommitCallbacks(execute=True), + ): + response = self.client.post( + manage_url, {"email": recipient, "invite": "1"} + ) + self.assertRedirects(response, manage_url) + self.assertEqual(len(mail.outbox), 2) + self.assertEqual(mail.outbox[1].to, [recipient]) + self.assertIn("Use your own password.", mail.outbox[1].body) + self.assertIn( + "Use <b>your own password</b>.", + mail.outbox[1].alternatives[0].content, + ) + replacement = self.invitation_path(mail.outbox[1].body) + self.assertNotEqual(link, replacement) + invited_browser = Client() + self.assertContains( + invited_browser.get(link), "invitation is unavailable" + ) + self.assertEqual(invited_browser.get(replacement).status_code, 302) + def invitation_path(self, body): return urlsplit( next(line for line in body.splitlines() if "/welcome/" in line)