2021-01-07 11:42:45 +03:00
|
|
|
defmodule PlausibleWeb.CRMAuthPlug do
|
|
|
|
import Plug.Conn
|
|
|
|
use Plausible.Repo
|
|
|
|
|
|
|
|
def init(options) do
|
|
|
|
options
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(conn, _opts) do
|
|
|
|
case get_session(conn, :current_user_id) do
|
|
|
|
nil ->
|
2021-01-07 17:53:29 +03:00
|
|
|
conn |> send_resp(403, "Not allowed") |> halt
|
2021-01-07 11:42:45 +03:00
|
|
|
|
|
|
|
id ->
|
|
|
|
user = Repo.get_by(Plausible.Auth.User, id: id)
|
|
|
|
|
2021-06-16 15:33:37 +03:00
|
|
|
if user && user.id in admin_user_ids() do
|
2021-01-07 11:42:45 +03:00
|
|
|
assign(conn, :current_user, user)
|
|
|
|
else
|
|
|
|
conn |> send_resp(403, "Not allowed") |> halt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-01-07 16:16:04 +03:00
|
|
|
|
2021-06-16 15:33:37 +03:00
|
|
|
defp admin_user_ids(), do: Application.get_env(:plausible, :admin_user_ids)
|
2021-01-07 11:42:45 +03:00
|
|
|
end
|