LgAuthorizeView & LgCascadingPolicies
Two components govern authorizations in the UI:
LgAuthorizeViewconditions the display (PolicyVisible) and editing (PolicyEdit) of its content based on policies, and propagates these policies cascading to child Lagoon components;LgCascadingPoliciesdeclares policies always validated in its subtree, avoiding re-evaluation of rights component by component.
Policies themselves are declared on the server side — see Roles and policies.
<LgAuthorizeView PolicyVisible="@Policies.IsAdmin">
...
</LgAuthorizeView>
- Namespace:
Lagoon.UI.Components - Inherits from:
ComponentBase(both) - Interfaces:
ILgComponentPolicies(LgAuthorizeView);IComponentContextProvider(LgCascadingPolicies) - NuGet package:
Lagoon.UI
Usage
Hide an area based on a policy
The content of LgAuthorizeView is only rendered if the user satisfies
PolicyVisible. NotAuthorized provides fallback content and
Authorizing provides content while rights are being evaluated asynchronously.
<LgAuthorizeView PolicyVisible="@Policies.IsAdmin">
<ChildContent>
<LgTitle Text="#Administration" TitleLevel="TitleLevel.Level2" />
<LgButton Text="#PurgeData" OnClick="PurgeAsync" />
</ChildContent>
<NotAuthorized>
<LgBigMessage Title="#AccessDenied" />
</NotAuthorized>
</LgAuthorizeView>
public partial class AdminSectionPage : LgPage
{
// Action reserved for administrators: the entire area is hidden without the policy.
private async Task PurgeAsync(ActionEventArgs args)
{
await Task.Delay(100, args.CancellationToken);
ShowSuccess("#DataPurged");
}
}
Authorized is equivalent to ChildContent (with authentication state
in context) — do not define both, an exception would be raised.
Make an area read-only
PolicyEdit leaves the content visible but makes it non-editable if
the policy is not satisfied: child Lagoon fields (consuming the cascading
parameter ParentAuthorizeView) switch to read-only.
<LgAuthorizeView PolicyEdit="@Policies.CanEditOrders">
<LgTextBox Label="#Reference" @bind-Value="_model.Reference" />
<LgDateBox Label="#OrderDate" @bind-Value="_model.OrderDate" />
</LgAuthorizeView>
public partial class OrderFormPage : LgPage
{
private readonly OrderVm _model = new();
private sealed class OrderVm
{
public string Reference { get; set; }
public DateTime? OrderDate { get; set; }
}
}
Child Lagoon components also expose PolicyVisible / PolicyEdit
individually (e.g., LgButton):
LgAuthorizeView serves to factorize the rule across an entire area.
Render container
By default, authorized content is rendered without a wrapper element, unless
CssClass is defined. ShowDiv forces (or prevents) encapsulation in a
<div>.
<LgAuthorizeView PolicyVisible="@Policies.SeeStatistics" CssClass="dashboard-stats">
...
</LgAuthorizeView>
Propagate validated policies with LgCascadingPolicies
LgCascadingPolicies marks policies as already validated for its entire
subtree: child components that require one of these policies
(via PolicyVisible / PolicyEdit) consider them satisfied without
re-evaluation. This is useful when a screen has already verified the right upstream,
or for repeated renders (grid rows, lists).
<LgCascadingPolicies ValidatedPolicies="@_validatedPolicies">
@foreach (TaskVm task in _tasks)
{
<LgButton Text="@task.Name" PolicyEdit="@Policies.CanEditTasks" />
}
</LgCascadingPolicies>
public partial class ProjectPage : LgPage
{
// Policies already verified on screen entry: avoids re-evaluation per button.
private string _validatedPolicies = Policies.CanEditTasks;
private readonly List<TaskVm> _tasks = new();
private sealed class TaskVm
{
public string Name { get; set; }
}
}
The AuthorizeAsync(policyName) method (via @ref) verifies a policy in
the component's context.
Properties
All [Parameter] properties of both components.
LgAuthorizeView
| Name | Type | Default | Declared in | Description |
|---|---|---|---|---|
Authorized | RenderFragment<AuthenticationState> | null | LgAuthorizeView | Content displayed if the user is authorized. Incompatible with ChildContent. |
Authorizing | RenderFragment | null | LgAuthorizeView | Content displayed while rights are being evaluated asynchronously. |
ChildContent | RenderFragment | null | LgAuthorizeView | Content displayed if the user is authorized. |
CssClass | string | null | LgAuthorizeView | CSS classes for the wrapper <div> (its presence enables encapsulation). |
NotAuthorized | RenderFragment<AuthenticationState> | null | LgAuthorizeView | Content displayed if the user is not authorized. |
PolicyEdit | string | null | LgAuthorizeView | Policy required to edit the content; otherwise child components switch to read-only. |
PolicyVisible | string | null | LgAuthorizeView | Policy required to display the content. |
ShowDiv | bool? | null | LgAuthorizeView | Forces (true) or prevents (false) encapsulation in a <div>. If null, a <div> is only rendered if CssClass is defined. |
LgCascadingPolicies
| Name | Type | Default | Declared in | Description |
|---|---|---|---|---|
ChildContent | RenderFragment | null | LgCascadingPolicies | Content of the subtree benefiting from validated policies. |
ValidatedPolicies | string | null | LgCascadingPolicies | Policies considered as always validated in the subtree. |
Methods & events
| Member | Signature | Description |
|---|---|---|
LgCascadingPolicies.AuthorizeAsync | Task<bool> AuthorizeAsync(string policyName) | Verifies whether the user satisfies the policy in the component's context. |
Best practices / accessibility
- Declare policy names in a shared client/server class
(
Policies.XXX) rather than as literal strings. - Client-side hiding is a UI convenience, not protection:
controllers must carry their own
[Authorize]attributes — see Roles and policies. - Use
LgCascadingPoliciesfor areas with repeated rendering: each avoided policy evaluation is one fewer round-trip to the authorization context.
All parameters: API Reference — LgAuthorizeView and API Reference — LgCascadingPolicies.
Related components
LgButton—PolicyVisible/PolicyEditat the component level.LgEditForm— form whose fields inherit cascading policies.