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

32 lines
867 B
Elixir
Raw 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)
|> 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