|
|
@ -6,6 +6,8 @@ defmodule DecentralisedBookIndexWeb.LiveUserAuth do
|
|
|
|
import Phoenix.Component
|
|
|
|
import Phoenix.Component
|
|
|
|
use DecentralisedBookIndexWeb, :verified_routes
|
|
|
|
use DecentralisedBookIndexWeb, :verified_routes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
alias DecentralisedBookIndex.Accounts.Role
|
|
|
|
|
|
|
|
|
|
|
|
def on_mount(:live_user_optional, _params, _session, socket) do
|
|
|
|
def on_mount(:live_user_optional, _params, _session, socket) do
|
|
|
|
if socket.assigns[:current_user] do
|
|
|
|
if socket.assigns[:current_user] do
|
|
|
|
{:cont, socket}
|
|
|
|
{:cont, socket}
|
|
|
@ -29,4 +31,34 @@ defmodule DecentralisedBookIndexWeb.LiveUserAuth do
|
|
|
|
{:cont, assign(socket, :current_user, nil)}
|
|
|
|
{:cont, assign(socket, :current_user, nil)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_mount(:moderator_required, _params, _session, socket) do
|
|
|
|
|
|
|
|
current_user = socket.assigns[:current_user]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if current_user && Role.can_moderate?(current_user.role) do
|
|
|
|
|
|
|
|
{:cont, socket}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
socket =
|
|
|
|
|
|
|
|
socket
|
|
|
|
|
|
|
|
|> Phoenix.LiveView.put_flash(:error, "Unauthorized!")
|
|
|
|
|
|
|
|
|> Phoenix.LiveView.redirect(to: ~p"/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{:halt, socket}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def on_mount(:admin_required, _params, _session, socket) do
|
|
|
|
|
|
|
|
current_user = socket.assigns[:current_user]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if current_user && Role.can_administrate?(current_user.role) do
|
|
|
|
|
|
|
|
{:cont, socket}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
socket =
|
|
|
|
|
|
|
|
socket
|
|
|
|
|
|
|
|
|> Phoenix.LiveView.put_flash(:error, "Unauthorized!")
|
|
|
|
|
|
|
|
|> Phoenix.LiveView.redirect(to: ~p"/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
{:halt, socket}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|