analytics/lib/plausible_web/controllers/invitation_controller.ex
Adrian Gruntkowski e67850c11d
Fix and refactor invitation logic (#3376)
* Make membership creation and role updates more explicit in terms of changesets

* Extract invitation accept flow logic and refactor it slightly

* Improve acceptation logic

* Update moduledoc

* Improve SiteLocker API and add typespecs

* Stop naming function not returning a boolean like a predicate

* Refactor rest of invitation actions and safeguard against rogue requests

* Update code docs slightly

* Extend `Billing.check_needs_update/1` tests

* Parametrize selfhost flag and toggle SiteLocker logic on it

* Add tests for newly extracted services

* Add test case and a fix for locking site on grace period ended

* Make invitation controller tests async as there's no more env patching

* Add test cases for self-invites and fix one bug

* Add and refactor tests for rejecting and removing invitations

* Prevent issuing ownership transfer to existing owner

* Improve name of the test

* Improve `Billing.check_needs_to_upgrade/1` return value

* Improve `Billing.SiteLocker.update_sites_for/1` and its tests

* Fix typos

Co-authored-by: hq1 <hq@mtod.org>

* Make invitation removal and rejection resilient to races

---------

Co-authored-by: hq1 <hq@mtod.org>
2023-10-02 14:57:57 +02:00

55 lines
2.0 KiB
Elixir

defmodule PlausibleWeb.InvitationController do
use PlausibleWeb, :controller
plug PlausibleWeb.RequireAccountPlug
plug PlausibleWeb.AuthorizeSiteAccess, [:owner, :admin] when action in [:remove_invitation]
def accept_invitation(conn, %{"invitation_id" => invitation_id}) do
case Plausible.Site.Memberships.accept_invitation(invitation_id, conn.assigns.current_user) do
{:ok, membership} ->
conn
|> put_flash(:success, "You now have access to #{membership.site.domain}")
|> redirect(to: "/#{URI.encode_www_form(membership.site.domain)}")
{:error, :invitation_not_found} ->
conn
|> put_flash(:error, "Invitation missing or already accepted")
|> redirect(to: "/sites")
{:error, _} ->
conn
|> put_flash(:error, "Something went wrong, please try again")
|> redirect(to: "/sites")
end
end
def reject_invitation(conn, %{"invitation_id" => invitation_id}) do
case Plausible.Site.Memberships.reject_invitation(invitation_id, conn.assigns.current_user) do
{:ok, invitation} ->
conn
|> put_flash(:success, "You have rejected the invitation to #{invitation.site.domain}")
|> redirect(to: "/sites")
{:error, :invitation_not_found} ->
conn
|> put_flash(:error, "Invitation missing or already accepted")
|> redirect(to: "/sites")
end
end
def remove_invitation(conn, %{"invitation_id" => invitation_id}) do
case Plausible.Site.Memberships.remove_invitation(invitation_id, conn.assigns.site) do
{:ok, invitation} ->
conn
|> put_flash(:success, "You have removed the invitation for #{invitation.email}")
|> redirect(to: Routes.site_path(conn, :settings_people, invitation.site.domain))
{:error, :invitation_not_found} ->
conn
|> put_flash(:error, "Invitation missing or already removed")
|> redirect(to: Routes.site_path(conn, :settings_people, conn.assigns.site.domain))
end
end
end