2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.AuthController do
|
|
|
|
use PlausibleWeb, :controller
|
|
|
|
use Plausible.Repo
|
|
|
|
alias Plausible.Auth
|
|
|
|
require Logger
|
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
plug PlausibleWeb.RequireLoggedOutPlug
|
|
|
|
when action in [:register_form, :register, :login_form, :login]
|
|
|
|
|
|
|
|
plug PlausibleWeb.RequireAccountPlug
|
2020-12-29 16:17:27 +03:00
|
|
|
when action in [
|
|
|
|
:user_settings,
|
|
|
|
:save_settings,
|
|
|
|
:delete_me,
|
|
|
|
:password_form,
|
|
|
|
:set_password,
|
|
|
|
:activate_form
|
|
|
|
]
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
def register_form(conn, _params) do
|
2020-07-02 11:21:11 +03:00
|
|
|
if Keyword.fetch!(Application.get_env(:plausible, :selfhost), :disable_registration) do
|
|
|
|
conn
|
|
|
|
|> redirect(to: "/login")
|
|
|
|
else
|
|
|
|
changeset = Plausible.Auth.User.changeset(%Plausible.Auth.User{})
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-07-02 11:21:11 +03:00
|
|
|
render(conn, "register_form.html",
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2020-08-28 15:00:16 +03:00
|
|
|
def register(conn, params) do
|
2021-01-26 12:44:48 +03:00
|
|
|
if Keyword.fetch!(Application.get_env(:plausible, :selfhost), :disable_registration) do
|
|
|
|
conn
|
|
|
|
|> redirect(to: "/login")
|
|
|
|
else
|
2021-06-16 15:00:07 +03:00
|
|
|
user = Plausible.Auth.User.new(params["user"])
|
2020-08-28 15:00:16 +03:00
|
|
|
|
2021-01-26 12:44:48 +03:00
|
|
|
if PlausibleWeb.Captcha.verify(params["h-captcha-response"]) do
|
|
|
|
case Repo.insert(user) do
|
|
|
|
{:ok, user} ->
|
|
|
|
code = Auth.issue_email_verification(user)
|
|
|
|
Logger.info("VERIFICATION CODE: #{code}")
|
|
|
|
email_template = PlausibleWeb.Email.activation_email(user, code)
|
|
|
|
Plausible.Mailer.send_email(email_template)
|
2020-08-28 15:00:16 +03:00
|
|
|
|
2021-01-26 12:44:48 +03:00
|
|
|
conn
|
|
|
|
|> put_session(:current_user_id, user.id)
|
|
|
|
|> put_resp_cookie("logged_in", "true",
|
|
|
|
http_only: false,
|
|
|
|
max_age: 60 * 60 * 24 * 365 * 5000
|
|
|
|
)
|
|
|
|
|> redirect(to: "/activate")
|
2020-08-28 15:00:16 +03:00
|
|
|
|
2021-01-26 12:44:48 +03:00
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "register_form.html",
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render(conn, "register_form.html",
|
|
|
|
changeset: user,
|
|
|
|
captcha_error: "Please complete the captcha to register",
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-08-28 15:00:16 +03:00
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def register_from_invitation_form(conn, %{"invitation_id" => invitation_id}) do
|
|
|
|
if Keyword.fetch!(Application.get_env(:plausible, :selfhost), :disable_registration) do
|
|
|
|
conn
|
|
|
|
|> redirect(to: "/login")
|
|
|
|
else
|
|
|
|
invitation = Repo.get_by(Plausible.Auth.Invitation, invitation_id: invitation_id)
|
|
|
|
changeset = Plausible.Auth.User.changeset(%Plausible.Auth.User{})
|
|
|
|
|
|
|
|
if invitation do
|
|
|
|
render(conn, "register_from_invitation_form.html",
|
|
|
|
changeset: changeset,
|
|
|
|
invitation: invitation,
|
2021-09-16 11:59:03 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"},
|
|
|
|
skip_plausible_tracking: true
|
2021-06-16 15:00:07 +03:00
|
|
|
)
|
|
|
|
else
|
|
|
|
render(conn, "invitation_expired.html", layout: {PlausibleWeb.LayoutView, "focus.html"})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def register_from_invitation(conn, %{"invitation_id" => invitation_id} = params) do
|
|
|
|
if Keyword.fetch!(Application.get_env(:plausible, :selfhost), :disable_registration) do
|
|
|
|
conn
|
|
|
|
|> redirect(to: "/login")
|
|
|
|
else
|
|
|
|
invitation = Repo.get_by(Plausible.Auth.Invitation, invitation_id: invitation_id)
|
|
|
|
user = Plausible.Auth.User.new(params["user"])
|
|
|
|
|
2021-09-08 15:15:37 +03:00
|
|
|
user =
|
|
|
|
case invitation.role do
|
|
|
|
:owner -> user
|
|
|
|
_ -> Plausible.Auth.User.remove_trial_expiry(user)
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
if PlausibleWeb.Captcha.verify(params["h-captcha-response"]) do
|
|
|
|
case Repo.insert(user) do
|
|
|
|
{:ok, user} ->
|
|
|
|
code = Auth.issue_email_verification(user)
|
|
|
|
Logger.info("VERIFICATION CODE: #{code}")
|
|
|
|
email_template = PlausibleWeb.Email.activation_email(user, code)
|
|
|
|
Plausible.Mailer.send_email(email_template)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_session(:current_user_id, user.id)
|
|
|
|
|> put_resp_cookie("logged_in", "true",
|
|
|
|
http_only: false,
|
|
|
|
max_age: 60 * 60 * 24 * 365 * 5000
|
|
|
|
)
|
|
|
|
|> redirect(to: "/activate")
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "register_from_invitation_form.html",
|
|
|
|
invitation: invitation,
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render(conn, "register_from_invitation_form.html",
|
|
|
|
invitation: invitation,
|
|
|
|
changeset: user,
|
|
|
|
captcha_error: "Please complete the captcha to register",
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-15 12:30:45 +03:00
|
|
|
def activate_form(conn, _params) do
|
|
|
|
user = conn.assigns[:current_user]
|
2021-06-25 11:33:00 +03:00
|
|
|
|
2021-06-25 11:37:57 +03:00
|
|
|
has_invitation =
|
|
|
|
Repo.exists?(
|
|
|
|
from i in Plausible.Auth.Invitation,
|
|
|
|
where: i.email == ^user.email
|
|
|
|
)
|
2020-03-23 12:34:25 +03:00
|
|
|
|
2020-12-29 16:17:27 +03:00
|
|
|
has_code =
|
|
|
|
Repo.exists?(
|
|
|
|
from c in "email_verification_codes",
|
|
|
|
where: c.user_id == ^user.id
|
|
|
|
)
|
2020-11-03 12:20:11 +03:00
|
|
|
|
2020-12-29 16:17:27 +03:00
|
|
|
render(conn, "activate.html",
|
|
|
|
has_pin: has_code,
|
2021-06-25 11:33:00 +03:00
|
|
|
has_invitation: has_invitation,
|
2020-12-29 16:17:27 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-12-15 12:30:45 +03:00
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-12-15 12:30:45 +03:00
|
|
|
def activate(conn, %{"code" => code}) do
|
|
|
|
user = conn.assigns[:current_user]
|
2021-06-25 12:10:20 +03:00
|
|
|
|
2021-06-25 12:00:15 +03:00
|
|
|
has_invitation =
|
|
|
|
Repo.exists?(
|
|
|
|
from i in Plausible.Auth.Invitation,
|
|
|
|
where: i.email == ^user.email
|
|
|
|
)
|
2021-06-25 12:10:20 +03:00
|
|
|
|
2020-12-15 12:30:45 +03:00
|
|
|
{code, ""} = Integer.parse(code)
|
|
|
|
|
|
|
|
case Auth.verify_email(user, code) do
|
|
|
|
:ok ->
|
2021-06-25 12:00:15 +03:00
|
|
|
if has_invitation do
|
2021-06-16 15:00:07 +03:00
|
|
|
redirect(conn, to: "/sites")
|
|
|
|
else
|
|
|
|
redirect(conn, to: "/sites/new")
|
|
|
|
end
|
2020-12-29 16:17:27 +03:00
|
|
|
|
2020-12-15 12:30:45 +03:00
|
|
|
{:error, :incorrect} ->
|
|
|
|
render(conn, "activate.html",
|
|
|
|
error: "Incorrect activation code",
|
|
|
|
has_pin: true,
|
2021-06-25 12:00:15 +03:00
|
|
|
has_invitation: has_invitation,
|
2020-12-15 12:30:45 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-12-29 16:17:27 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, :expired} ->
|
2020-12-15 12:30:45 +03:00
|
|
|
render(conn, "activate.html",
|
|
|
|
error: "Code is expired, please request another one",
|
|
|
|
has_pin: false,
|
2021-06-25 12:00:15 +03:00
|
|
|
has_invitation: has_invitation,
|
2020-12-15 12:30:45 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-15 12:30:45 +03:00
|
|
|
def request_activation_code(conn, _params) do
|
|
|
|
user = conn.assigns[:current_user]
|
2020-12-28 18:20:35 +03:00
|
|
|
code = Auth.issue_email_verification(user)
|
2020-12-15 12:30:45 +03:00
|
|
|
|
2020-12-28 18:20:35 +03:00
|
|
|
email_template = PlausibleWeb.Email.activation_email(user, code)
|
2020-12-15 12:30:45 +03:00
|
|
|
Plausible.Mailer.send_email(email_template)
|
|
|
|
|
2020-10-16 15:48:22 +03:00
|
|
|
conn
|
2020-12-15 12:30:45 +03:00
|
|
|
|> put_flash(:success, "Activation code was sent to #{user.email}")
|
|
|
|
|> redirect(to: "/activate")
|
2020-10-16 15:48:22 +03:00
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def password_reset_request_form(conn, _) do
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "password_reset_request_form.html",
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def password_reset_request(conn, %{"email" => ""}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "password_reset_request_form.html",
|
|
|
|
error: "Please enter an email address",
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2020-08-28 15:00:16 +03:00
|
|
|
def password_reset_request(conn, %{"email" => email} = params) do
|
|
|
|
if PlausibleWeb.Captcha.verify(params["h-captcha-response"]) do
|
|
|
|
user = Repo.get_by(Plausible.Auth.User, email: email)
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2020-08-28 15:00:16 +03:00
|
|
|
if user do
|
|
|
|
token = Auth.Token.sign_password_reset(email)
|
2020-10-05 15:01:54 +03:00
|
|
|
url = PlausibleWeb.Endpoint.url() <> "/password/reset?token=#{token}"
|
2020-08-28 15:00:16 +03:00
|
|
|
Logger.debug("PASSWORD RESET LINK: " <> url)
|
|
|
|
email_template = PlausibleWeb.Email.password_reset_email(email, url)
|
2021-03-30 15:05:08 +03:00
|
|
|
Plausible.Mailer.deliver_now!(email_template)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-08-28 15:00:16 +03:00
|
|
|
render(conn, "password_reset_request_success.html",
|
|
|
|
email: email,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
else
|
|
|
|
render(conn, "password_reset_request_success.html",
|
|
|
|
email: email,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
else
|
2020-08-28 15:00:16 +03:00
|
|
|
render(conn, "password_reset_request_form.html",
|
|
|
|
error: "Please complete the captcha to reset your password",
|
2020-06-08 10:35:13 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def password_reset_form(conn, %{"token" => token}) do
|
|
|
|
case Auth.Token.verify_password_reset(token) do
|
|
|
|
{:ok, _} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "password_reset_form.html",
|
|
|
|
token: token,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, :expired} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
401,
|
|
|
|
"Your token has expired. Please request another password reset link."
|
|
|
|
)
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, _} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
401,
|
|
|
|
"Your token is invalid. Please request another password reset link."
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def password_reset(conn, %{"token" => token, "password" => pw}) do
|
|
|
|
case Auth.Token.verify_password_reset(token) do
|
|
|
|
{:ok, %{email: email}} ->
|
|
|
|
user = Repo.get_by(Auth.User, email: email)
|
|
|
|
changeset = Auth.User.set_password(user, pw)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
case Repo.update(changeset) do
|
|
|
|
{:ok, _updated} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:login_title, "Password updated successfully")
|
|
|
|
|> put_flash(:login_instructions, "Please log in with your new credentials")
|
|
|
|
|> put_session(:current_user_id, nil)
|
2020-03-31 15:47:34 +03:00
|
|
|
|> delete_resp_cookie("logged_in")
|
2019-09-02 14:29:19 +03:00
|
|
|
|> redirect(to: "/login")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, changeset} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "password_reset_form.html",
|
|
|
|
changeset: changeset,
|
|
|
|
token: token,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, :expired} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
401,
|
|
|
|
"Your token has expired. Please request another password reset link."
|
|
|
|
)
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, _} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
401,
|
|
|
|
"Your token is invalid. Please request another password reset link."
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def login(conn, %{"email" => email, "password" => password}) do
|
2021-05-25 11:32:54 +03:00
|
|
|
with :ok <- check_ip_rate_limit(conn),
|
|
|
|
{:ok, user} <- find_user(email),
|
|
|
|
:ok <- check_user_rate_limit(user),
|
|
|
|
:ok <- check_password(user, password) do
|
|
|
|
login_dest = get_session(conn, :login_dest) || "/sites"
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-05-25 11:32:54 +03:00
|
|
|
conn
|
|
|
|
|> put_session(:current_user_id, user.id)
|
|
|
|
|> put_resp_cookie("logged_in", "true",
|
|
|
|
http_only: false,
|
|
|
|
max_age: 60 * 60 * 24 * 365 * 5000
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2021-05-25 11:32:54 +03:00
|
|
|
|> put_session(:login_dest, nil)
|
|
|
|
|> redirect(to: login_dest)
|
|
|
|
else
|
|
|
|
:wrong_password ->
|
|
|
|
render(conn, "login_form.html",
|
|
|
|
error: "Wrong email or password. Please try again.",
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-05-25 11:32:54 +03:00
|
|
|
:user_not_found ->
|
|
|
|
Plausible.Auth.Password.dummy_calculation()
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-05-25 11:32:54 +03:00
|
|
|
render(conn, "login_form.html",
|
2020-06-08 10:35:13 +03:00
|
|
|
error: "Wrong email or password. Please try again.",
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
|
2021-05-25 11:43:03 +03:00
|
|
|
{:rate_limit, _} ->
|
2021-05-25 11:32:54 +03:00
|
|
|
render_error(
|
|
|
|
conn,
|
|
|
|
429,
|
|
|
|
"Too many login attempts. Wait a minute before trying again."
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@login_interval 60_000
|
|
|
|
@login_limit 5
|
|
|
|
defp check_ip_rate_limit(conn) do
|
|
|
|
ip_address = PlausibleWeb.RemoteIp.get(conn)
|
|
|
|
|
|
|
|
case Hammer.check_rate("login:ip:#{ip_address}", @login_interval, @login_limit) do
|
|
|
|
{:allow, _} -> :ok
|
|
|
|
{:deny, _} -> {:rate_limit, :ip_address}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp find_user(email) do
|
|
|
|
user =
|
|
|
|
Repo.one(
|
|
|
|
from u in Plausible.Auth.User,
|
|
|
|
where: u.email == ^email
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2021-05-25 11:32:54 +03:00
|
|
|
|
|
|
|
if user, do: {:ok, user}, else: :user_not_found
|
|
|
|
end
|
|
|
|
|
|
|
|
defp check_user_rate_limit(user) do
|
|
|
|
case Hammer.check_rate("login:user:#{user.id}", @login_interval, @login_limit) do
|
|
|
|
{:allow, _} -> :ok
|
|
|
|
{:deny, _} -> {:rate_limit, :user}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp check_password(user, password) do
|
|
|
|
if Plausible.Auth.Password.match?(password, user.password_hash || "") do
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
:wrong_password
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def login_form(conn, _params) do
|
|
|
|
render(conn, "login_form.html", layout: {PlausibleWeb.LayoutView, "focus.html"})
|
|
|
|
end
|
|
|
|
|
|
|
|
def password_form(conn, _params) do
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "password_form.html",
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"},
|
|
|
|
skip_plausible_tracking: true
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_password(conn, %{"password" => pw}) do
|
|
|
|
changeset = Auth.User.set_password(conn.assigns[:current_user], pw)
|
|
|
|
|
|
|
|
case Repo.update(changeset) do
|
|
|
|
{:ok, _user} ->
|
|
|
|
redirect(conn, to: "/sites/new")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, changeset} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "password_form.html",
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_settings(conn, _params) do
|
|
|
|
changeset = Auth.User.changeset(conn.assigns[:current_user])
|
2020-07-21 09:58:00 +03:00
|
|
|
|
2021-01-15 17:28:57 +03:00
|
|
|
{usage_pageviews, usage_custom_events} =
|
|
|
|
Plausible.Billing.usage_breakdown(conn.assigns[:current_user])
|
|
|
|
|
2020-07-21 09:58:00 +03:00
|
|
|
render(conn, "user_settings.html",
|
2021-02-05 12:23:30 +03:00
|
|
|
user: conn.assigns[:current_user] |> Repo.preload(:api_keys),
|
2020-07-21 09:58:00 +03:00
|
|
|
changeset: changeset,
|
2020-12-16 12:57:28 +03:00
|
|
|
subscription: conn.assigns[:current_user].subscription,
|
2021-01-15 17:28:57 +03:00
|
|
|
theme: conn.assigns[:current_user].theme || "system",
|
|
|
|
usage_pageviews: usage_pageviews,
|
|
|
|
usage_custom_events: usage_custom_events
|
2020-07-21 09:58:00 +03:00
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def save_settings(conn, %{"user" => user_params}) do
|
|
|
|
changes = Auth.User.changeset(conn.assigns[:current_user], user_params)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
case Repo.update(changes) do
|
|
|
|
{:ok, _user} ->
|
|
|
|
conn
|
2021-03-15 12:40:53 +03:00
|
|
|
|> put_flash(:success, "Account settings saved successfully")
|
2019-09-02 14:29:19 +03:00
|
|
|
|> redirect(to: "/settings")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, changeset} ->
|
2020-08-19 14:43:32 +03:00
|
|
|
render(conn, "user_settings.html",
|
|
|
|
changeset: changeset,
|
|
|
|
subscription: conn.assigns[:current_user].subscription
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-05 12:23:30 +03:00
|
|
|
def new_api_key(conn, _params) do
|
|
|
|
key = :crypto.strong_rand_bytes(64) |> Base.url_encode64() |> binary_part(0, 64)
|
|
|
|
changeset = Auth.ApiKey.changeset(%Auth.ApiKey{}, %{key: key})
|
|
|
|
|
|
|
|
render(conn, "new_api_key.html",
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_api_key(conn, %{"api_key" => key_params}) do
|
|
|
|
api_key = %Auth.ApiKey{user_id: conn.assigns[:current_user].id}
|
|
|
|
changeset = Auth.ApiKey.changeset(api_key, key_params)
|
|
|
|
|
|
|
|
case Repo.insert(changeset) do
|
|
|
|
{:ok, _api_key} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "API key created successfully")
|
|
|
|
|> redirect(to: "/settings#api-keys")
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
render(conn, "new_api_key.html",
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_api_key(conn, %{"id" => id}) do
|
|
|
|
Repo.get_by(Auth.ApiKey, id: id)
|
|
|
|
|> Repo.delete!()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "API key revoked successfully")
|
|
|
|
|> redirect(to: "/settings#api-keys")
|
|
|
|
end
|
|
|
|
|
2020-03-31 15:47:34 +03:00
|
|
|
def delete_me(conn, params) do
|
2020-06-08 10:35:13 +03:00
|
|
|
user =
|
|
|
|
conn.assigns[:current_user]
|
2021-09-08 11:09:58 +03:00
|
|
|
|> Repo.preload(site_memberships: :site)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.preload(:subscription)
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-09-08 11:09:58 +03:00
|
|
|
for membership <- user.site_memberships do
|
|
|
|
Repo.delete!(membership)
|
|
|
|
|
|
|
|
if membership.role == :owner do
|
|
|
|
Repo.delete!(membership.site)
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2020-05-07 14:26:22 +03:00
|
|
|
if user.subscription, do: Repo.delete!(user.subscription)
|
2019-09-02 14:29:19 +03:00
|
|
|
Repo.delete!(user)
|
|
|
|
|
2020-03-31 15:47:34 +03:00
|
|
|
logout(conn, params)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2021-04-01 10:43:32 +03:00
|
|
|
def logout(conn, params) do
|
|
|
|
redirect_to = Map.get(params, "redirect", "/")
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
conn
|
|
|
|
|> configure_session(drop: true)
|
2020-03-31 15:47:34 +03:00
|
|
|
|> delete_resp_cookie("logged_in")
|
2021-04-01 10:43:32 +03:00
|
|
|
|> redirect(to: redirect_to)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def google_auth_callback(conn, %{"code" => code, "state" => site_id}) do
|
|
|
|
res = Plausible.Google.Api.fetch_access_token(code)
|
|
|
|
id_token = res["id_token"]
|
|
|
|
[_, body, _] = String.split(id_token, ".")
|
2020-06-08 10:35:13 +03:00
|
|
|
id = body |> Base.decode64!(padding: false) |> Jason.decode!()
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
Plausible.Site.GoogleAuth.changeset(%Plausible.Site.GoogleAuth{}, %{
|
|
|
|
email: id["email"],
|
|
|
|
refresh_token: res["refresh_token"],
|
|
|
|
access_token: res["access_token"],
|
|
|
|
expires: NaiveDateTime.utc_now() |> NaiveDateTime.add(res["expires_in"]),
|
|
|
|
user_id: conn.assigns[:current_user].id,
|
|
|
|
site_id: site_id
|
2020-06-08 10:35:13 +03:00
|
|
|
})
|
|
|
|
|> Repo.insert!()
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
site = Repo.get(Plausible.Site, site_id)
|
|
|
|
|
2021-01-19 16:40:01 +03:00
|
|
|
redirect(conn, to: "/#{URI.encode_www_form(site.domain)}/settings/search-console")
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|