Allow ownership transfers

This commit is contained in:
Uku Taht 2022-08-11 13:15:33 +03:00
parent 6b8ed12567
commit a5757bc61e
3 changed files with 35 additions and 11 deletions

View File

@ -17,6 +17,12 @@ defmodule Plausible.Site.Membership do
|> validate_inclusion(:role, valid_roles(schema.role))
end
def override_role(schema, role) do
schema
|> change(%{role: role})
|> validate_required([:user_id, :site_id, :role])
end
defp valid_roles(_prev_role = nil), do: [:owner, :admin, :viewer]
defp valid_roles(:owner), do: [:owner, :admin, :viewer]
defp valid_roles(:admin), do: [:admin, :viewer]

View File

@ -25,11 +25,9 @@ defmodule PlausibleWeb.InvitationController do
end
membership_changeset =
Membership.changeset(existing_membership || %Membership{}, %{
user_id: user.id,
site_id: invitation.site.id,
role: invitation.role
})
(existing_membership ||
%Membership{user_id: user.id, site_id: invitation.site.id})
|> Membership.override_role(invitation.role)
multi =
multi
@ -46,7 +44,7 @@ defmodule PlausibleWeb.InvitationController do
|> put_flash(:success, "You now have access to #{invitation.site.domain}")
|> redirect(to: "/#{URI.encode_www_form(invitation.site.domain)}")
{:error, _} ->
{:error, _, _} ->
conn
|> put_flash(:error, "Something went wrong, please try again")
|> redirect(to: "/sites")

View File

@ -40,8 +40,10 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
subject: "[Plausible Analytics] #{user.email} accepted your invitation to #{site.domain}"
)
end
end
test "ownership transfer - notifies the original inviter with a different email", %{
describe "POST /sites/invitations/:invitation_id/accept - ownership transfer" do
test "notifies the original inviter with a different email", %{
conn: conn,
user: user
} do
@ -60,7 +62,7 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
)
end
test "ownership transfer - downgrades previous owner to admin", %{conn: conn, user: user} do
test "downgrades previous owner to admin", %{conn: conn, user: user} do
old_owner = insert(:user)
site = insert(:site, members: [old_owner])
@ -82,7 +84,7 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
assert new_owner_membership.role == :owner
end
test "ownership transfer - will lock the site if new owner does not have an active subscription or trial",
test "will lock the site if new owner does not have an active subscription or trial",
%{
conn: conn,
user: user
@ -102,7 +104,7 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
assert Repo.reload!(site).locked
end
test "ownership transfer - will end the trial of the new owner immediately", %{
test "will end the trial of the new owner immediately", %{
conn: conn,
user: user
} do
@ -122,7 +124,7 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
assert Repo.reload!(site).locked
end
test "ownership transfer - if new owner does not have a trial - will set trial_expiry_date to yesterday",
test "if new owner does not have a trial - will set trial_expiry_date to yesterday",
%{
conn: conn,
user: user
@ -142,6 +144,24 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
assert Timex.before?(Repo.reload!(user).trial_expiry_date, Timex.today())
assert Repo.reload!(site).locked
end
test "can upgrade admin to owner", %{conn: conn, user: user} do
old_owner = insert(:user)
site = insert(:site, members: [old_owner])
insert(:site_membership, site: site, user: user, role: :admin)
invitation =
insert(:invitation, site_id: site.id, inviter: old_owner, email: user.email, role: :owner)
post(conn, "/sites/invitations/#{invitation.invitation_id}/accept")
refute Repo.exists?(from(i in Plausible.Auth.Invitation, where: i.email == ^user.email))
new_owner_membership =
Repo.get_by(Plausible.Site.Membership, user_id: user.id, site_id: site.id)
assert new_owner_membership.role == :owner
end
end
describe "POST /sites/invitations/:invitation_id/reject" do