philomena/lib/philomena_web/controllers/admin/user_ban_controller.ex

130 lines
3.7 KiB
Elixir
Raw Normal View History

2019-12-13 18:14:34 +01:00
defmodule PhilomenaWeb.Admin.UserBanController do
use PhilomenaWeb, :controller
alias Philomena.Bans.User, as: UserBan
alias Philomena.Bans
alias Philomena.Repo
import Ecto.Query
plug :verify_authorized
plug :load_resource, model: UserBan, only: [:edit, :update, :delete]
2020-09-17 20:35:47 +02:00
plug :check_can_delete when action in [:delete]
2019-12-13 18:14:34 +01:00
def index(conn, %{"q" => q}) when is_binary(q) do
like_q = "%#{q}%"
UserBan
|> join(:inner, [ub], _ in assoc(ub, :user))
2020-01-11 05:20:19 +01:00
|> where(
[ub, u],
ilike(u.name, ^like_q) or
ub.generated_ban_id == ^q or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", ub.reason, ^q) or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", ub.note, ^q)
2019-12-13 18:14:34 +01:00
)
|> load_bans(conn)
end
def index(conn, %{"user_id" => user_id}) when is_binary(user_id) do
UserBan
|> where(user_id: ^user_id)
|> load_bans(conn)
end
def index(conn, _params) do
load_bans(UserBan, conn)
end
def new(conn, %{"username" => username}) do
changeset = Bans.change_user(%UserBan{username: username})
2019-12-16 20:24:38 +01:00
render(conn, "new.html", title: "New User Ban", changeset: changeset)
end
2019-12-13 18:14:34 +01:00
def new(conn, _params) do
changeset = Bans.change_user(%UserBan{})
2019-12-16 20:24:38 +01:00
render(conn, "new.html", title: "New User Ban", changeset: changeset)
2019-12-13 18:14:34 +01:00
end
def create(conn, %{"user" => user_ban_params}) do
case Bans.create_user(conn.assigns.current_user, user_ban_params) do
2021-11-07 19:51:55 +01:00
{:ok, user_ban} ->
2019-12-13 18:14:34 +01:00
conn
|> put_flash(:info, "User was successfully banned.")
|> moderation_log(details: &log_details/3, data: user_ban)
|> redirect(to: ~p"/admin/user_bans")
2019-12-13 18:14:34 +01:00
2019-12-20 19:58:06 +01:00
{:error, :user_ban, changeset, _changes} ->
2019-12-13 18:14:34 +01:00
render(conn, "new.html", changeset: changeset)
2021-01-21 21:24:22 +01:00
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
2019-12-13 18:14:34 +01:00
end
end
def edit(conn, _params) do
changeset = Bans.change_user(conn.assigns.user)
2019-12-16 20:24:38 +01:00
render(conn, "edit.html", title: "Editing User Ban", changeset: changeset)
2019-12-13 18:14:34 +01:00
end
def update(conn, %{"user" => user_ban_params}) do
case Bans.update_user(conn.assigns.user, user_ban_params) do
2021-11-07 19:51:55 +01:00
{:ok, user_ban} ->
2019-12-13 18:14:34 +01:00
conn
|> put_flash(:info, "User ban successfully updated.")
|> moderation_log(details: &log_details/3, data: user_ban)
|> redirect(to: ~p"/admin/user_bans")
2019-12-13 18:14:34 +01:00
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
def delete(conn, _params) do
2021-11-07 19:51:55 +01:00
{:ok, user_ban} = Bans.delete_user(conn.assigns.user)
2019-12-13 18:14:34 +01:00
conn
|> put_flash(:info, "User ban successfully deleted.")
|> moderation_log(details: &log_details/3, data: user_ban)
|> redirect(to: ~p"/admin/user_bans")
2019-12-13 18:14:34 +01:00
end
defp load_bans(queryable, conn) do
user_bans =
queryable
|> order_by(desc: :created_at)
|> preload([:user, :banning_user])
|> Repo.paginate(conn.assigns.scrivener)
2020-01-11 05:20:19 +01:00
render(conn, "index.html",
title: "Admin - User Bans",
layout_class: "layout--wide",
user_bans: user_bans
)
2019-12-13 18:14:34 +01:00
end
defp verify_authorized(conn, _opts) do
case Canada.Can.can?(conn.assigns.current_user, :index, UserBan) do
2020-01-11 05:20:19 +01:00
true -> conn
2019-12-13 18:14:34 +01:00
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
2020-09-17 20:35:47 +02:00
defp check_can_delete(conn, _opts) do
case conn.assigns.current_user.role == "admin" do
true -> conn
false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
2021-11-07 19:51:55 +01:00
defp log_details(_conn, action, ban) do
2021-11-07 19:51:55 +01:00
body =
case action do
:create -> "Created a user ban #{ban.generated_ban_id}"
:update -> "Updated a user ban #{ban.generated_ban_id}"
:delete -> "Deleted a user ban #{ban.generated_ban_id}"
end
%{body: body, subject_path: ~p"/admin/user_bans"}
2021-11-07 19:51:55 +01:00
end
2019-12-13 18:14:34 +01:00
end