2019-12-16 03:21:14 +01:00
|
|
|
defmodule PhilomenaWeb.Admin.UserController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Roles.Role
|
|
|
|
alias Philomena.Users.User
|
|
|
|
alias Philomena.Users
|
|
|
|
alias Philomena.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
plug :verify_authorized
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: User,
|
|
|
|
only: [:edit, :update],
|
|
|
|
id_field: "slug",
|
|
|
|
preload: [:roles]
|
|
|
|
|
2021-12-19 17:54:21 +01:00
|
|
|
plug :load_roles when action in [:edit, :update]
|
2019-12-16 03:21:14 +01:00
|
|
|
|
|
|
|
def index(conn, %{"q" => q}) do
|
|
|
|
User
|
|
|
|
|> where([u], u.email == ^q or ilike(u.name, ^"%#{q}%"))
|
|
|
|
|> load_users(conn)
|
|
|
|
end
|
|
|
|
|
|
|
|
def index(conn, %{"twofactor" => _twofactor}) do
|
|
|
|
User
|
|
|
|
|> where([u], u.otp_required_for_login == true)
|
|
|
|
|> load_users(conn)
|
|
|
|
end
|
|
|
|
|
|
|
|
def index(conn, %{"staff" => _staff}) do
|
|
|
|
User
|
|
|
|
|> where([u], u.role != "user")
|
|
|
|
|> load_users(conn)
|
|
|
|
end
|
|
|
|
|
|
|
|
def index(conn, _params) do
|
|
|
|
load_users(User, conn)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp load_users(queryable, conn) do
|
|
|
|
users =
|
|
|
|
queryable
|
|
|
|
|> order_by(desc: :id)
|
|
|
|
|> Repo.paginate(conn.assigns.scrivener)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "index.html",
|
|
|
|
title: "Admin - Users",
|
|
|
|
layout_class: "layout--medium",
|
|
|
|
users: users
|
|
|
|
)
|
2019-12-16 03:21:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, _params) do
|
|
|
|
changeset = Users.change_user(conn.assigns.user)
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "edit.html", title: "Editing User", changeset: changeset)
|
2019-12-16 03:21:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"user" => user_params}) do
|
|
|
|
case Users.update_user(conn.assigns.user, user_params) do
|
2021-11-07 19:51:55 +01:00
|
|
|
{:ok, user} ->
|
2019-12-16 03:21:14 +01:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "User successfully updated.")
|
2024-06-23 16:42:29 +02:00
|
|
|
|> moderation_log(details: &log_details/2, data: user)
|
2024-04-29 02:55:27 +02:00
|
|
|
|> redirect(to: ~p"/profiles/#{user}")
|
2019-12-16 03:21:14 +01:00
|
|
|
|
2021-12-19 17:54:21 +01:00
|
|
|
{:error, changeset} ->
|
2019-12-16 03:21:14 +01:00
|
|
|
render(conn, "edit.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_authorized(conn, _opts) do
|
|
|
|
case Canada.Can.can?(conn.assigns.current_user, :index, User) do
|
2020-01-11 05:20:19 +01:00
|
|
|
true -> conn
|
2019-12-16 03:21:14 +01:00
|
|
|
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp load_roles(conn, _opts) do
|
|
|
|
assign(conn, :roles, Repo.all(Role))
|
|
|
|
end
|
2021-11-07 19:51:55 +01:00
|
|
|
|
2024-06-23 16:42:29 +02:00
|
|
|
defp log_details(_action, user) do
|
2021-11-07 19:51:55 +01:00
|
|
|
%{
|
|
|
|
body: "Updated user details for #{user.name}",
|
2024-04-29 02:55:27 +02:00
|
|
|
subject_path: ~p"/profiles/#{user}"
|
2021-11-07 19:51:55 +01:00
|
|
|
}
|
|
|
|
end
|
2019-12-16 03:21:14 +01:00
|
|
|
end
|