Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve transfer ownership error message #2651

Merged
merged 6 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
- Fix breakdown API pagination when using event metrics plausible/analytics#2562
- Automatically update all visible dashboard reports in the realtime view
- Connect via TLS when using HTTPS scheme in ClickHouse URL plausible/analytics#2570
- Add more descriptive error message in case a transfer to an invited (but not joined) user is requested plausible/analytics#2651

### Changed
- Reject events with long URIs and data URIs plausible/analytics#2536
Expand Down
33 changes: 25 additions & 8 deletions lib/plausible_web/controllers/site/membership_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,39 @@ defmodule PlausibleWeb.Site.MembershipController do
site = Sites.get_for_user!(conn.assigns[:current_user].id, site_domain)
user = Plausible.Auth.find_user_by(email: email)

invitation =
invite_result =
Invitation.new(%{
email: email,
role: :owner,
site_id: site.id,
inviter_id: conn.assigns[:current_user].id
})
|> Repo.insert!()
|> Repo.preload([:site, :inviter])
|> Repo.insert()

PlausibleWeb.Email.ownership_transfer_request(invitation, user)
|> Plausible.Mailer.send()
conn =
case invite_result do
{:ok, invitation} ->
invitation
|> Repo.preload([:site, :inviter])
|> PlausibleWeb.Email.ownership_transfer_request(user)
|> Plausible.Mailer.send()

put_flash(conn, :success, "Site transfer request has been sent to #{email}")

{:error, changeset} ->
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _} -> msg end)

message =
if errors[:invitation] do
"#{email} has already been invited but hasn't yet accepted the join request to #{site_domain}"
ruslandoga marked this conversation as resolved.
Show resolved Hide resolved
else
"Site transfer request to #{email} has failed"
end

put_flash(conn, :error, message)
end

conn
|> put_flash(:success, "Site transfer request has been sent to #{email}")
|> redirect(to: Routes.site_path(conn, :settings_people, site.domain))
redirect(conn, to: Routes.site_path(conn, :settings_people, site.domain))
end

@doc """
Expand Down
27 changes: 27 additions & 0 deletions test/plausible_web/controllers/site/membership_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,33 @@ defmodule PlausibleWeb.Site.MembershipControllerTest do

refute Repo.get_by(Plausible.Auth.Invitation, email: "john.doe@example.com")
end

test "fails to transfer ownership to invited user with proper error message", ctx do
%{conn: conn, user: user} = ctx
site = insert(:site, members: [user])
invited = "john.doe@example.com"

# invite a user but don't join

conn =
post(conn, "/sites/#{site.domain}/memberships/invite", %{
email: invited,
role: "admin"
})

conn = get(recycle(conn), redirected_to(conn, 302))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: recycle, nice


assert html_response(conn, 200) =~
"#{invited} has been invited to #{site.domain} as an admin"

# transferring ownership to that domain now fails

conn = post(conn, "/sites/#{site.domain}/transfer-ownership", %{email: invited})
conn = get(recycle(conn), redirected_to(conn, 302))

assert html_response(conn, 200) =~
"#{invited} has already been invited but hasn't yet accepted the join request to #{site.domain}"
end
end

describe "PUT /sites/memberships/:id/role/:new_role" do
Expand Down