From 1435149d32bd5d7842a57991b58e80f4e16fef00 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 23 Dec 2019 09:23:25 -0500 Subject: [PATCH] admin badge user list --- .../admin/badge/user_controller.ex | 31 +++++++++++++++++++ lib/philomena_web/router.ex | 4 ++- .../admin/badge/user/index.html.slime | 25 +++++++++++++++ .../views/admin/badge/user_view.ex | 3 ++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 lib/philomena_web/controllers/admin/badge/user_controller.ex create mode 100644 lib/philomena_web/templates/admin/badge/user/index.html.slime create mode 100644 lib/philomena_web/views/admin/badge/user_view.ex diff --git a/lib/philomena_web/controllers/admin/badge/user_controller.ex b/lib/philomena_web/controllers/admin/badge/user_controller.ex new file mode 100644 index 00000000..11cdd9ad --- /dev/null +++ b/lib/philomena_web/controllers/admin/badge/user_controller.ex @@ -0,0 +1,31 @@ +defmodule PhilomenaWeb.Admin.Badge.UserController do + use PhilomenaWeb, :controller + + alias Philomena.Users.User + alias Philomena.Badges.Badge + alias Philomena.Repo + import Ecto.Query + + plug :verify_authorized + plug :load_resource, model: Badge, id_name: "badge_id", persisted: true + + def index(conn, _params) do + badge = conn.assigns.badge + + users = + User + |> join(:inner, [u], _ in assoc(u, :awards)) + |> where([_u, a], a.badge_id == ^badge.id) + |> order_by([u, _a], asc: u.name) + |> Repo.paginate(conn.assigns.pagination) + + render(conn, "index.html", title: "Users with badge #{badge.title}", users: users) + end + + defp verify_authorized(conn, _opts) do + case Canada.Can.can?(conn.assigns.current_user, :index, Badge) do + true -> conn + _false -> PhilomenaWeb.NotAuthorizedPlug.call(conn) + end + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index ab8c8cce..1240c22d 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -230,7 +230,9 @@ defmodule PhilomenaWeb.Router do resources "/adverts", AdvertController, except: [:show] resources "/forums", ForumController, except: [:show, :delete] - resources "/badges", BadgeController, except: [:show, :delete] + resources "/badges", BadgeController, except: [:show, :delete] do + resources "/users", Badge.UserController, only: [:index] + end resources "/mod_notes", ModNoteController, except: [:show] resources "/users", UserController, only: [:index, :edit, :update] do resources "/avatar", User.AvatarController, only: [:delete], singleton: true diff --git a/lib/philomena_web/templates/admin/badge/user/index.html.slime b/lib/philomena_web/templates/admin/badge/user/index.html.slime new file mode 100644 index 00000000..d41506bf --- /dev/null +++ b/lib/philomena_web/templates/admin/badge/user/index.html.slime @@ -0,0 +1,25 @@ +h1 + ' Users with + => @badge.title + ' badge + +- route = fn p -> Routes.admin_badge_user_path(@conn, :index, p) end +- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @users, route: route, conn: @conn + +.block + .block__header + = pagination + + .block__content + table.table + thead + tr + th User + tbody + = for user <- @users do + tr + td + = link user.name, to: Routes.profile_path(@conn, :show, user) + + .block__header.block__header--light + = pagination diff --git a/lib/philomena_web/views/admin/badge/user_view.ex b/lib/philomena_web/views/admin/badge/user_view.ex new file mode 100644 index 00000000..1040734d --- /dev/null +++ b/lib/philomena_web/views/admin/badge/user_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Admin.Badge.UserView do + use PhilomenaWeb, :view +end