improve first launch experience for self-hosters (#2357)
* first launch * dynamic children, wait for repo * remove wait_for_repo and app env manipulations * don't mention free trial in self-hosted pages * add changelog * assigns[:is_selfhost] -> @is_selfhost * better changelog wording * rm admin_user, admin_email, admin_pwd from app env * rm DISABLE_AUTH * redirect / to /login when not authenticated * remove TODO * Update lib/plausible_web/controllers/page_controller.ex Co-authored-by: Uku Taht <Uku.taht@gmail.com> * format Co-authored-by: Uku Taht <Uku.taht@gmail.com>pull/2441/head r2022.11.10-17
parent
0e91ae9b58
commit
0b7870dc4d
@ -1,14 +1,15 @@
|
||||
defmodule PlausibleWeb.PageController do
|
||||
use PlausibleWeb, :controller
|
||||
use Plausible.Repo
|
||||
plug PlausibleWeb.AutoAuthPlug
|
||||
|
||||
@doc """
|
||||
The root path is never accessible in Plausible.Cloud because it is handled by the upstream reverse proxy.
|
||||
|
||||
This controller action is only ever triggered in self-hosted Plausible. It redirects the user to /login where `PlausibleWeb.RequireLoggedOutPlug` plug kicks in. If they are already logged in, they are redirected to /sites, otherwise they'll see the /login page.
|
||||
"""
|
||||
def index(conn, _params) do
|
||||
if conn.assigns[:current_user] do
|
||||
user = conn.assigns[:current_user] |> Repo.preload(:sites)
|
||||
render(conn, "sites.html", sites: user.sites)
|
||||
else
|
||||
render(conn, "index.html")
|
||||
end
|
||||
conn
|
||||
|> put_session(:login_dest, conn.request_path)
|
||||
|> redirect(to: "/login")
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,25 +0,0 @@
|
||||
defmodule PlausibleWeb.AutoAuthPlug do
|
||||
import Plug.Conn
|
||||
alias PlausibleWeb.AuthController
|
||||
|
||||
def init(options) do
|
||||
options
|
||||
end
|
||||
|
||||
def call(conn, _opts) do
|
||||
cond do
|
||||
Keyword.fetch!(Application.get_env(:plausible, :selfhost), :disable_authentication) ->
|
||||
conn
|
||||
|> AuthController.login(%{
|
||||
"email" => Application.fetch_env!(:plausible, :admin_email),
|
||||
"password" => Application.fetch_env!(:plausible, :admin_pwd)
|
||||
})
|
||||
|> halt
|
||||
|
||||
true ->
|
||||
Plug.Conn.put_session(conn, :login_dest, conn.request_path)
|
||||
|> Phoenix.Controller.redirect(to: "/login")
|
||||
|> halt
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,26 @@
|
||||
defmodule PlausibleWeb.FirstLaunchPlug do
|
||||
@moduledoc """
|
||||
Redirects first-launch users to registration page.
|
||||
"""
|
||||
|
||||
@behaviour Plug
|
||||
alias Plausible.Release
|
||||
|
||||
@impl true
|
||||
def init(opts) do
|
||||
_path = Keyword.fetch!(opts, :redirect_to)
|
||||
end
|
||||
|
||||
@impl true
|
||||
def call(%Plug.Conn{request_path: path} = conn, path), do: conn
|
||||
|
||||
def call(conn, redirect_to) do
|
||||
if Release.should_be_first_launch?() do
|
||||
conn
|
||||
|> Phoenix.Controller.redirect(to: redirect_to)
|
||||
|> Plug.Conn.halt()
|
||||
else
|
||||
conn
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,3 +0,0 @@
|
||||
<div class="relative bg-gray-50 dark:bg-gray-800 overflow-hidden text-center dark:text-gray-100">
|
||||
You will be redirected... If it doesn't work, please click login.
|
||||
</div>
|
||||
@ -1,15 +0,0 @@
|
||||
defmodule PlausibleWeb.PageView do
|
||||
use PlausibleWeb, :view
|
||||
|
||||
def admin_email do
|
||||
Application.get_env(:plausible, :admin_email)
|
||||
end
|
||||
|
||||
def base_domain do
|
||||
PlausibleWeb.Endpoint.host()
|
||||
end
|
||||
|
||||
def plausible_url do
|
||||
PlausibleWeb.Endpoint.url()
|
||||
end
|
||||
end
|
||||
@ -1,6 +1,5 @@
|
||||
#!/bin/sh
|
||||
# Create an admin user
|
||||
|
||||
BIN_DIR=$(dirname "$0")
|
||||
|
||||
"${BIN_DIR}"/bin/plausible eval Plausible.Release.init_admin
|
||||
echo "init-admin is deprecated and is no-op now"
|
||||
echo "user registration on first launch happens via Web UI instead"
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
defmodule Plausible.ReleaseTest do
|
||||
use Plausible.DataCase
|
||||
alias Plausible.{Release, Auth}
|
||||
|
||||
describe "should_be_first_launch?/0" do
|
||||
test "returns true when self-hosted and no users" do
|
||||
patch_env(:is_selfhost, true)
|
||||
refute Repo.exists?(Auth.User)
|
||||
assert Release.should_be_first_launch?()
|
||||
end
|
||||
|
||||
test "returns false when not self-hosted and has no users" do
|
||||
patch_env(:is_selfhost, false)
|
||||
refute Repo.exists?(Auth.User)
|
||||
refute Release.should_be_first_launch?()
|
||||
end
|
||||
|
||||
test "returns false when not self-hosted and has users" do
|
||||
insert(:user)
|
||||
patch_env(:is_selfhost, false)
|
||||
refute Release.should_be_first_launch?()
|
||||
end
|
||||
|
||||
test "returns false when self-hosted and has users" do
|
||||
insert(:user)
|
||||
patch_env(:is_selfhost, true)
|
||||
refute Release.should_be_first_launch?()
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,52 +1,33 @@
|
||||
defmodule PlausibleWeb.AdminAuthControllerTest do
|
||||
use PlausibleWeb.ConnCase
|
||||
alias Plausible.Release
|
||||
|
||||
setup_patch_env(:is_selfhost, true)
|
||||
|
||||
describe "GET /" do
|
||||
test "no landing page", %{conn: conn} do
|
||||
set_config(disable_authentication: false)
|
||||
conn = get(conn, "/")
|
||||
test "disable registration", %{conn: conn} do
|
||||
prevent_first_launch()
|
||||
patch_config(disable_registration: true)
|
||||
conn = get(conn, "/register")
|
||||
assert redirected_to(conn) == "/login"
|
||||
end
|
||||
|
||||
test "logs admin user in automatically when authentication is disabled", %{conn: conn} do
|
||||
set_config(disable_authentication: true)
|
||||
|
||||
admin_user =
|
||||
insert(:user,
|
||||
email: Application.get_env(:plausible, :admin_email),
|
||||
password: Application.get_env(:plausible, :admin_pwd)
|
||||
)
|
||||
|
||||
# goto landing page
|
||||
conn = get(conn, "/")
|
||||
assert get_session(conn, :current_user_id) == admin_user.id
|
||||
assert redirected_to(conn) == "/sites"
|
||||
test "disable registration + first launch", %{conn: conn} do
|
||||
patch_config(disable_registration: true)
|
||||
assert Release.should_be_first_launch?()
|
||||
|
||||
# trying logging out
|
||||
conn = get(conn, "/logout")
|
||||
assert redirected_to(conn) == "/"
|
||||
conn = get(conn, "/")
|
||||
assert redirected_to(conn) == "/sites"
|
||||
end
|
||||
|
||||
test "disable registration", %{conn: conn} do
|
||||
set_config(disable_registration: true)
|
||||
# "first launch" takes precedence
|
||||
conn = get(conn, "/register")
|
||||
assert redirected_to(conn) == "/login"
|
||||
assert html_response(conn, 200) =~ "Enter your details"
|
||||
end
|
||||
end
|
||||
|
||||
def set_config(config) do
|
||||
updated_config =
|
||||
Keyword.merge(
|
||||
[disable_authentication: false, disable_registration: false],
|
||||
config
|
||||
)
|
||||
def patch_config(config) do
|
||||
updated_config = Keyword.merge([disable_registration: false], config)
|
||||
patch_env(:selfhost, updated_config)
|
||||
end
|
||||
|
||||
Application.put_env(
|
||||
:plausible,
|
||||
:selfhost,
|
||||
updated_config
|
||||
)
|
||||
defp prevent_first_launch do
|
||||
insert(:user)
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1,73 @@
|
||||
defmodule PlausibleWeb.FirstLaunchPlugTest do
|
||||
use PlausibleWeb.ConnCase
|
||||
import Plug.Test
|
||||
|
||||
alias PlausibleWeb.FirstLaunchPlug
|
||||
alias Plausible.Release
|
||||
|
||||
describe "init/1" do
|
||||
test "requires :redirect_to option" do
|
||||
assert_raise KeyError, ~r"key :redirect_to not found", fn ->
|
||||
FirstLaunchPlug.init(_no_opts = [])
|
||||
end
|
||||
|
||||
path = FirstLaunchPlug.init(redirect_to: "/register")
|
||||
assert path == "/register"
|
||||
end
|
||||
end
|
||||
|
||||
@opts FirstLaunchPlug.init(redirect_to: "/register")
|
||||
|
||||
describe "call/2" do
|
||||
test "no-op for paths == :redirect_to" do
|
||||
conn = conn("GET", "/register")
|
||||
conn = FirstLaunchPlug.call(conn, @opts)
|
||||
refute conn.halted
|
||||
|
||||
# even when it's the first launch
|
||||
patch_env(:is_selfhost, true)
|
||||
assert Release.should_be_first_launch?()
|
||||
|
||||
conn = conn("GET", "/register")
|
||||
conn = FirstLaunchPlug.call(conn, @opts)
|
||||
refute conn.halted
|
||||
end
|
||||
|
||||
test "no-op when not first launch" do
|
||||
refute Release.should_be_first_launch?()
|
||||
conn = conn("GET", "/sites")
|
||||
conn = FirstLaunchPlug.call(conn, @opts)
|
||||
refute conn.halted
|
||||
end
|
||||
|
||||
test "redirects to :redirect_to when first launch" do
|
||||
patch_env(:is_selfhost, true)
|
||||
assert Release.should_be_first_launch?()
|
||||
|
||||
conn = conn("GET", "/sites")
|
||||
conn = FirstLaunchPlug.call(conn, @opts)
|
||||
assert conn.halted
|
||||
assert redirected_to(conn) == "/register"
|
||||
end
|
||||
end
|
||||
|
||||
describe "first launch plug in :browser pipeline" do
|
||||
test "redirects to /register on first launch", %{conn: conn} do
|
||||
patch_env(:is_selfhost, true)
|
||||
assert Release.should_be_first_launch?()
|
||||
|
||||
conn = get(conn, "/")
|
||||
assert redirected_to(conn) == "/register"
|
||||
end
|
||||
|
||||
test "no-op when not first launch", %{conn: conn} do
|
||||
patch_env(:is_selfhost, false)
|
||||
refute Release.should_be_first_launch?()
|
||||
|
||||
# gets redirected to login by auth plugs
|
||||
# "first launch" doesn't interfere
|
||||
conn = get(conn, "/sites")
|
||||
assert redirected_to(conn) == "/login"
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue