philomena/lib/philomena_web/controllers/admin/badge/user_controller.ex

32 lines
864 B
Elixir
Raw Permalink Normal View History

2019-12-23 15:23:25 +01:00
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)
2020-02-11 04:26:45 +01:00
|> Repo.paginate(conn.assigns.scrivener)
2019-12-23 15:23:25 +01:00
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
2020-01-11 05:20:19 +01:00
true -> conn
2019-12-23 15:23:25 +01:00
_false -> PhilomenaWeb.NotAuthorizedPlug.call(conn)
end
end
end