Skip to content
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
20 changes: 19 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ config :plausible,
mailer_email: System.get_env("MAILER_EMAIL", "hello@plausible.local"),
admin_pwd: System.get_env("ADMIN_USER_PWD", "!@d3in"),
ecto_repos: [Plausible.Repo, Plausible.ClickhouseRepo],
environment: System.get_env("ENVIRONMENT", "dev")
environment: System.get_env("ENVIRONMENT", "dev"),
admin_emails: [System.get_env("ADMIN_EMAIL")]

disable_auth = String.to_existing_atom(System.get_env("DISABLE_AUTH", "false"))

Expand Down Expand Up @@ -212,6 +213,23 @@ config :geolix,
}
]

config :kaffy,
otp_app: :plausible,
ecto_repo: Plausible.Repo,
router: PlausibleWeb.Router,
resources: [
users: [
resources: [
users: [schema: Plausible.Auth.User, admin: Plausible.Auth.UserAdmin]
]
],
sites: [
resources: [
sites: [schema: Plausible.Site, admin: Plausible.SiteAdmin]
]
]
]

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
4 changes: 3 additions & 1 deletion config/releases.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ db_url =

admin_user = System.get_env("ADMIN_USER_NAME")
admin_email = System.get_env("ADMIN_USER_EMAIL")
admin_emails = System.get_env("ADMIN_EMAILS") |> String.split(",")
admin_pwd = System.get_env("ADMIN_USER_PWD")
env = System.get_env("ENVIRONMENT", "prod")
mailer_adapter = System.get_env("MAILER_ADAPTER", "Bamboo.SMTPAdapter")
Expand Down Expand Up @@ -62,7 +63,8 @@ config :plausible,
admin_email: admin_email,
admin_pwd: admin_pwd,
environment: env,
mailer_email: mailer_email
mailer_email: mailer_email,
admin_emails: admin_emails

config :plausible, :selfhost,
disable_authentication: disable_auth,
Expand Down
22 changes: 22 additions & 0 deletions lib/plausible/auth/user_admin.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule Plausible.Auth.UserAdmin do
use Plausible.Repo

def form_fields(_) do
[
name: nil,
email: nil,
trial_expiry_date: nil
]
end

def index(_) do
[
name: nil,
email: nil,
email_verified: nil,
trial_expiry_date: nil,
inserted_at: nil,
last_seen: nil
]
end
end
24 changes: 24 additions & 0 deletions lib/plausible/site/admin.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Plausible.SiteAdmin do
use Plausible.Repo

def custom_index_query(_conn, _schema, query) do
from(r in query, preload: [:members])
end

def form_fields(_) do
[
domain: nil,
timezone: nil,
public: nil
]
end

def index(_) do
[
domain: nil,
timezone: nil,
public: nil,
members: %{value: fn s -> Enum.map(s.members, & &1.email) end}
]
end
end
6 changes: 6 additions & 0 deletions lib/plausible_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ defmodule PlausibleWeb.Endpoint do
gzip: false,
only: ~w(css fonts images js favicon.ico robots.txt)

plug Plug.Static,
at: "/kaffy",
from: :kaffy,
gzip: false,
only: ~w(assets)

# Code reloading can be explicitly enabled under the
# :code_reloader configuration of your endpoint.
if code_reloading? do
Expand Down
25 changes: 25 additions & 0 deletions lib/plausible_web/plugs/crm_auth_plug.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
defmodule PlausibleWeb.CRMAuthPlug do
import Plug.Conn
use Plausible.Repo
@admin_emails Application.get_env(:plausible, :admin_emails)

def init(options) do
options
end

def call(conn, _opts) do
case get_session(conn, :current_user_id) do
nil ->
conn

id ->
user = Repo.get_by(Plausible.Auth.User, id: id)

if user && user.email in @admin_emails do
assign(conn, :current_user, user)
else
conn |> send_resp(403, "Not allowed") |> halt
end
end
end
end
2 changes: 2 additions & 0 deletions lib/plausible_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ defmodule PlausibleWeb.Router do
forward "/sent-emails", Bamboo.SentEmailViewerPlug
end

use Kaffy.Routes, scope: "/crm", pipe_through: [PlausibleWeb.CRMAuthPlug]

scope "/api/stats", PlausibleWeb.Api do
pipe_through :stats_api

Expand Down
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ defmodule Plausible.MixProject do
{:clickhouse_ecto, git: "https://github.com/plausible/clickhouse_ecto.git"},
{:geolix_adapter_mmdb2, "~> 0.5.0"},
{:mix_test_watch, "~> 1.0", only: :dev, runtime: false},
{:cachex, "~> 3.3"}
{:cachex, "~> 3.3"},
{:kaffy, "~> 0.9.0"}
]
end

Expand Down
1 change: 1 addition & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fdf843bca858203ae1de16da2ee206f53416bbda5dc8c9e78f43243de4bc3afe"},
"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},
"junit_formatter": {:hex, :junit_formatter, "3.1.0", "3f69c61c5413750f9c45e367d77aabbeac9b395acf478d8e70b4ee9d1989c709", [:mix], [], "hexpm", "da52401a93f711fc4f77ffabdda68f9a16fcad5d96f5fce4ae606ab1d73b72f4"},
"kaffy": {:hex, :kaffy, "0.9.0", "bef34c9729f6a3af4d0dea8eede8bcb9e11371a83ac9a8b393991bce81839517", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.11", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "d18ff57b8e68feb433aed11e71510cd357abc7034e75358af5deff7d0d4c6ed3"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mime": {:hex, :mime, "1.5.0", "203ef35ef3389aae6d361918bf3f952fa17a09e8e43b5aa592b93eba05d0fb8d", [:mix], [], "hexpm", "55a94c0f552249fc1a3dd9cd2d3ab9de9d3c89b559c2bd01121f824834f24746"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
Expand Down