2022-03-21 19:08:06 +01:00
|
|
|
defmodule PhilomenaWeb.Admin.User.VerificationController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Users.User
|
|
|
|
alias Philomena.Users
|
|
|
|
|
|
|
|
plug :verify_authorized
|
|
|
|
plug :load_resource, model: User, id_name: "user_id", id_field: "slug", persisted: true
|
|
|
|
|
|
|
|
def create(conn, _params) do
|
|
|
|
{:ok, user} = Users.verify_user(conn.assigns.user)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "User verification granted.")
|
|
|
|
|> moderation_log(details: &log_details/3, data: user)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{user}")
|
2022-03-21 19:08:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
{:ok, user} = Users.unverify_user(conn.assigns.user)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "User verification revoked.")
|
|
|
|
|> moderation_log(details: &log_details/3, data: user)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{user}")
|
2022-03-21 19:08:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_authorized(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :index, User) do
|
|
|
|
true -> conn
|
|
|
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-29 03:03:37 +02:00
|
|
|
defp log_details(_conn, action, user) do
|
2022-03-21 19:08:06 +01:00
|
|
|
body =
|
|
|
|
case action do
|
|
|
|
:create -> "Granted verification to #{user.name}"
|
|
|
|
:delete -> "Revoked verification from #{user.name}"
|
|
|
|
end
|
|
|
|
|
2024-04-29 02:55:27 +02:00
|
|
|
%{body: body, subject_path: ~p"/profiles/#{user}"}
|
2022-03-21 19:08:06 +01:00
|
|
|
end
|
|
|
|
end
|