analytics/lib/plausible_web/controllers/invitation_controller.ex
Adrian Gruntkowski 9d97dc1912
Move limit enforcement to accepting site ownership transfer (#3612)
* Move limit enforcement to accepting site ownerhsip transfer

* enforce pageview limit on ownership transfer accept

* Refactor plan limit check logic

* Extract `ensure_can_take_ownership` to `Invitations` context and refactor

* Improve styling of exceeded limits notice in invitation dialog and disable button

* styling improvements to notice

* make transfer_ownership return transfer to self error

* do not allow transferring to user without active subscription WIP

* Add missing typespec and improve existing ones

* Fix formatting

* Explicitly label direct match on function argument for clarity

* Slightly refactor `CreateInvitation.bulk_transfer_ownership_direct`

* Exclude quota enforcement tests from small build test suite

* Remove unused return type from `invite_error()` union type

* Do not block plan upgrade when there's pending ownership transfer

* Don't block and only warn about missing features on transfer

* Remove `x-init` attribute used for debugging

* Add tests for `Quota.monthly_pageview_usage/2`

* Test and improve site admin ownership transfer actions

* Extend tests for `AcceptInvitation.transfer_ownership`

* Test transfer ownership controller level accept action error cases

* Test choosing plan by user without sites but with a pending ownership transfer

* Test invitation x-data in sites LV

* Remove sitelocker trigger in invitation acceptance code and simplify logic

* Add Quota test for `user.allow_next_upgrade_override` being set

* ignore pageview limit only when subscribing to plan

* Use sandbox Paddle instance for staging

* Use sandbox paddle key for staging and dev

---------

Co-authored-by: Robert Joonas <robertjoonas16@gmail.com>
2023-12-20 14:56:49 +00:00

68 lines
2.3 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(external: "/#{URI.encode_www_form(membership.site.domain)}")
{:error, :invitation_not_found} ->
conn
|> put_flash(:error, "Invitation missing or already accepted")
|> redirect(to: "/sites")
{:error, :no_plan} ->
conn
|> put_flash(:error, "No existing subscription")
|> redirect(to: "/sites")
{:error, {:over_plan_limits, limits}} ->
conn
|> put_flash(
:error,
"Plan limits exceeded: #{PlausibleWeb.TextHelpers.pretty_list(limits)}."
)
|> 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(external: Routes.site_path(conn, :settings_people, invitation.site.domain))
{:error, :invitation_not_found} ->
conn
|> put_flash(:error, "Invitation missing or already removed")
|> redirect(external: Routes.site_path(conn, :settings_people, conn.assigns.site.domain))
end
end
end