legendary-doc-site/apps/core/lib/auth_web/helpers.ex

17 lines
558 B
Elixir
Raw Permalink Normal View History

defmodule Legendary.AuthWeb.Helpers do
@moduledoc """
Utility functions for working with users and roles.
"""
def current_user(%Phoenix.LiveView.Socket{assigns: %{current_user: user}}), do: user
def current_user(%Phoenix.LiveView.Socket{assigns: %{__assigns__: %{current_user: user}}}), do: user
2021-06-19 17:26:20 +00:00
def current_user(%Phoenix.LiveView.Socket{}), do: nil
def current_user(conn), do: Pow.Plug.current_user(conn)
def has_role?(conn_or_socket, role) do
conn_or_socket
|> current_user()
|> Legendary.Auth.Roles.has_role?(role)
2020-10-09 22:08:28 +00:00
end
end