From 650378f367977fa882c62e6eb8659af4bb3a588b Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Thu, 7 Jan 2021 10:42:45 +0200 Subject: [PATCH] Add CRM for admin emails (#557) --- config/config.exs | 20 ++++++++++++++++++- config/releases.exs | 4 +++- lib/plausible/auth/user_admin.ex | 22 +++++++++++++++++++++ lib/plausible/site/admin.ex | 24 +++++++++++++++++++++++ lib/plausible_web/endpoint.ex | 6 ++++++ lib/plausible_web/plugs/crm_auth_plug.ex | 25 ++++++++++++++++++++++++ lib/plausible_web/router.ex | 2 ++ mix.exs | 3 ++- mix.lock | 1 + 9 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 lib/plausible/auth/user_admin.ex create mode 100644 lib/plausible/site/admin.ex create mode 100644 lib/plausible_web/plugs/crm_auth_plug.ex diff --git a/config/config.exs b/config/config.exs index 338f2d0ef..57a620608 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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")) @@ -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" diff --git a/config/releases.exs b/config/releases.exs index 77c09325c..36f827268 100644 --- a/config/releases.exs +++ b/config/releases.exs @@ -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") @@ -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, diff --git a/lib/plausible/auth/user_admin.ex b/lib/plausible/auth/user_admin.ex new file mode 100644 index 000000000..8fb5595f9 --- /dev/null +++ b/lib/plausible/auth/user_admin.ex @@ -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 diff --git a/lib/plausible/site/admin.ex b/lib/plausible/site/admin.ex new file mode 100644 index 000000000..062a4430a --- /dev/null +++ b/lib/plausible/site/admin.ex @@ -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 diff --git a/lib/plausible_web/endpoint.ex b/lib/plausible_web/endpoint.ex index 1c183fb2b..d1f9045ed 100644 --- a/lib/plausible_web/endpoint.ex +++ b/lib/plausible_web/endpoint.ex @@ -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 diff --git a/lib/plausible_web/plugs/crm_auth_plug.ex b/lib/plausible_web/plugs/crm_auth_plug.ex new file mode 100644 index 000000000..141138760 --- /dev/null +++ b/lib/plausible_web/plugs/crm_auth_plug.ex @@ -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 diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index 57ef97cf7..13e881c21 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -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 diff --git a/mix.exs b/mix.exs index 8aff674e6..49a9383d5 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/mix.lock b/mix.lock index 1e9f195dd..8627054b3 100644 --- a/mix.lock +++ b/mix.lock @@ -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"},