Allow site creation for users owning the domain_changed_from (#4215)

* Allow site creation for users owning the `domain_changed_from`

* Format
This commit is contained in:
hq1 2024-06-11 15:48:46 +02:00 committed by GitHub
parent e1f0002d2e
commit 9f1f826801
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 1 deletions

View File

@ -169,7 +169,25 @@ defmodule Plausible.Sites do
def create(user, params) do
with :ok <- Quota.ensure_can_add_new_site(user) do
Ecto.Multi.new()
|> Ecto.Multi.insert(:site, Site.new(params))
|> Ecto.Multi.put(:site_changeset, Site.new(params))
|> Ecto.Multi.run(:clear_changed_from, fn
_repo, %{site_changeset: %{changes: %{domain: domain}}} ->
case get_for_user(user.id, domain, [:owner]) do
%Site{domain_changed_from: ^domain} = site ->
site
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:domain_changed_from, nil)
|> Ecto.Changeset.put_change(:domain_changed_at, nil)
|> Repo.update()
_ ->
{:ok, :ignore}
end
_repo, _context ->
{:ok, :ignore}
end)
|> Ecto.Multi.insert(:site, fn %{site_changeset: site} -> site end)
|> Ecto.Multi.insert(:site_membership, fn %{site: site} ->
Site.Membership.new(site, user)
end)

View File

@ -436,6 +436,31 @@ defmodule PlausibleWeb.SiteControllerTest do
assert html_response(conn, 200) =~
"This domain cannot be registered. Perhaps one of your colleagues registered it?"
end
test "allows creating the site if domain was changed by the owner", %{
conn: conn,
user: user
} do
:site
|> insert(
domain: "example.com",
memberships: [
build(:site_membership, user: user, role: :owner)
]
)
|> Plausible.Site.Domain.change("new.example.com")
conn =
post(conn, "/sites", %{
"site" => %{
"domain" => "example.com",
"timezone" => "Europe/London"
}
})
assert redirected_to(conn) ==
"/example.com/snippet?site_created=true"
end
end
describe "GET /:website/snippet" do