Apply UI authorization to User LiveViews (Index and Show)

Gate New User button, Edit and Delete links with can?/3.
Edit button on User Show visible only when user can update the user.
This commit is contained in:
Moritz 2026-02-03 16:35:32 +01:00
parent 5e361ba400
commit 2f67c7099d
Signed by: moritz
GPG key ID: 1020A035E5DD0824
2 changed files with 21 additions and 13 deletions

View file

@ -2,9 +2,11 @@
<.header> <.header>
{gettext("Listing Users")} {gettext("Listing Users")}
<:actions> <:actions>
<%= if can?(@current_user, :create, Mv.Accounts.User) do %>
<.button variant="primary" navigate={~p"/users/new"}> <.button variant="primary" navigate={~p"/users/new"}>
<.icon name="hero-plus" /> {gettext("New User")} <.icon name="hero-plus" /> {gettext("New User")}
</.button> </.button>
<% end %>
</:actions> </:actions>
</.header> </.header>
@ -62,16 +64,20 @@
<.link navigate={~p"/users/#{user}"}>{gettext("Show")}</.link> <.link navigate={~p"/users/#{user}"}>{gettext("Show")}</.link>
</div> </div>
<%= if can?(@current_user, :update, user) do %>
<.link navigate={~p"/users/#{user}/edit"}>{gettext("Edit")}</.link> <.link navigate={~p"/users/#{user}/edit"}>{gettext("Edit")}</.link>
<% end %>
</:action> </:action>
<:action :let={user}> <:action :let={user}>
<%= if can?(@current_user, :destroy, user) do %>
<.link <.link
phx-click={JS.push("delete", value: %{id: user.id}) |> hide("#row-#{user.id}")} phx-click={JS.push("delete", value: %{id: user.id}) |> hide("#row-#{user.id}")}
data-confirm={gettext("Are you sure?")} data-confirm={gettext("Are you sure?")}
> >
{gettext("Delete")} {gettext("Delete")}
</.link> </.link>
<% end %>
</:action> </:action>
</.table> </.table>
</Layouts.app> </Layouts.app>

View file

@ -41,9 +41,11 @@ defmodule MvWeb.UserLive.Show do
<.icon name="hero-arrow-left" /> <.icon name="hero-arrow-left" />
<span class="sr-only">{gettext("Back to users list")}</span> <span class="sr-only">{gettext("Back to users list")}</span>
</.button> </.button>
<%= if can?(@current_user, :update, @user) do %>
<.button variant="primary" navigate={~p"/users/#{@user}/edit?return_to=show"}> <.button variant="primary" navigate={~p"/users/#{@user}/edit?return_to=show"}>
<.icon name="hero-pencil-square" /> {gettext("Edit User")} <.icon name="hero-pencil-square" /> {gettext("Edit User")}
</.button> </.button>
<% end %>
</:actions> </:actions>
</.header> </.header>