mirror of
https://github.com/plausible/analytics.git
synced 2024-12-23 17:44:43 +03:00
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:
parent
e1f0002d2e
commit
9f1f826801
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user