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

fix ownership transfer when is_selfhost=true #2455

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -40,6 +40,7 @@ All notable changes to this project will be documented in this file.
- ARM64 support for docker images plausible/analytics#2103
- Add support for international domain names (IDNs) plausible/analytics#2034
- Allow self-hosters to register an account on first launch
- Fix ownership transfer invitation link in self-hosted deployments

### Fixed
- Plausible script does not prevent default if it's been prevented by an external script [plausible/analytics#1941](https://github.com/plausible/analytics/issues/1941)
Expand Down
4 changes: 3 additions & 1 deletion lib/plausible_web/controllers/invitation_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ defmodule PlausibleWeb.InvitationController do
end

defp maybe_end_trial_of_new_owner(multi, new_owner) do
if !Application.get_env(:plausible, :is_selfhost) do
if Application.get_env(:plausible, :is_selfhost) do
multi
else
end_trial_of_new_owner(multi, new_owner)
end
Copy link
Contributor Author

@ruslandoga ruslandoga Nov 19, 2022

Choose a reason for hiding this comment

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

This was retuning nil in self-hosted which was causing errors like this

plausible_1            | Server: example.com:80 (http)
plausible_1            | Request: POST /sites/invitations/sRdJ6PQkueG7fx0sfcENk/accept
plausible_1            | ** (exit) an exception was raised:
plausible_1            |     ** (FunctionClauseError) no function clause matching in Ecto.Multi.add_operation/3
plausible_1            |         (ecto 3.8.4) lib/ecto/multi.ex:640: Ecto.Multi.add_operation(nil, :membership, {:changeset, #Ecto.Changeset<action: :update, changes: %{role: :owner}, errors: [], data: #Plausible.Site.Membership<>, valid?: true>, []})
plausible_1            |         (plausible 0.0.1) lib/plausible_web/controllers/invitation_controller.ex:34: PlausibleWeb.InvitationController.accept_invitation/2
plausible_1            |         (plausible 0.0.1) lib/plausible_web/controllers/invitation_controller.ex:1: PlausibleWeb.InvitationController.action/2
plausible_1            |         (plausible 0.0.1) lib/plausible_web/controllers/invitation_controller.ex:1: PlausibleWeb.InvitationController.phoenix_controller_pipeline/2
plausible_1            |         (phoenix 1.6.6) lib/phoenix/router.ex:355: Phoenix.Router.__call__/2
plausible_1            |         (plausible 0.0.1) lib/plausible_web/endpoint.ex:1: PlausibleWeb.Endpoint.plug_builder_call/2
plausible_1            |         (plausible 0.0.1) lib/plausible_web/endpoint.ex:1: PlausibleWeb.Endpoint."call (overridable 3)"/2
plausible_1            |         (plausible 0.0.1) lib/plausible_web/endpoint.ex:1: PlausibleWeb.Endpoint.call/2

end
Expand Down
27 changes: 27 additions & 0 deletions test/plausible_web/controllers/invitation_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do

assert new_owner_membership.role == :owner
end

test "works in self-hosted", %{conn: conn, user: user} do
patch_env(:is_selfhost, true)

old_owner = insert(:user)
site = insert(:site, members: [old_owner])

invitation =
insert(:invitation,
site_id: site.id,
inviter: old_owner,
email: user.email,
role: :owner
)

post(conn, "/sites/invitations/#{invitation.invitation_id}/accept")

old_owner_membership =
Repo.get_by(Plausible.Site.Membership, user_id: old_owner.id, site_id: site.id)

assert old_owner_membership.role == :admin

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
Expand Down