@@ -41,7 +41,7 @@ namespace Privileged.Components;
4141/// </PrivilegeForm>
4242/// </Loaded>
4343/// </PrivilegeContextView>
44- ///
44+ ///
4545/// <!-- Simplified usage with ChildContent -->
4646/// <PrivilegeContextView>
4747/// <PrivilegeInputText @bind-Value="model.SecretField" Subject="Document" Field="Secret" />
@@ -53,37 +53,14 @@ namespace Privileged.Components;
5353/// <seealso cref="AuthenticationStateProvider"/>
5454public class PrivilegeContextView : ComponentBase
5555{
56- /// <summary>
57- /// Gets or sets the provider for retrieving the privilege context.
58- /// This service is automatically injected and must be registered in the dependency injection container.
59- /// </summary>
60- /// <value>
61- /// An <see cref="IPrivilegeContextProvider"/> instance used to asynchronously load the privilege context.
62- /// This service should be registered in the dependency injection container.
63- /// </value>
64- /// <remarks>
65- /// The provider is responsible for loading user-specific privilege rules and aliases, typically from
66- /// a database, cache, or external authorization service. The implementation should handle user identity
67- /// and return appropriate privilege contexts based on the authenticated user's roles and permissions.
68- /// </remarks>
69- [ Inject ]
70- protected IPrivilegeContextProvider PrivilegeContextProvider { get ; set ; } = default ! ;
56+ public PrivilegeContextView (
57+ IPrivilegeContextProvider privilegeContextProvider ,
58+ AuthenticationStateProvider ? authenticationStateProvider = null )
59+ {
60+ PrivilegeContextProvider = privilegeContextProvider ;
61+ AuthenticationStateProvider = authenticationStateProvider ;
62+ }
7163
72- /// <summary>
73- /// Gets or sets the authentication state provider used to retrieve the current user's authentication information.
74- /// This service is automatically injected from the Blazor authentication system.
75- /// </summary>
76- /// <value>
77- /// An <see cref="AuthenticationStateProvider"/> instance used to retrieve the current user's authentication state.
78- /// This is passed to the privilege context provider to load user-specific privileges.
79- /// </value>
80- /// <remarks>
81- /// The authentication state contains the current user's <see cref="System.Security.Claims.ClaimsPrincipal"/>,
82- /// which is used by the privilege context provider to determine what privileges should be loaded.
83- /// This enables user-specific privilege evaluation based on identity, roles, and claims.
84- /// </remarks>
85- [ Inject ]
86- protected AuthenticationStateProvider AuthenticationStateProvider { get ; set ; } = default ! ;
8764
8865 /// <summary>
8966 /// Gets or sets the render fragment to display while the privilege context is loading.
@@ -164,6 +141,37 @@ public class PrivilegeContextView : ComponentBase
164141 [ Parameter ]
165142 public RenderFragment ? ChildContent { get ; set ; }
166143
144+
145+ /// <summary>
146+ /// Gets or sets the provider for retrieving the privilege context.
147+ /// This service is automatically injected and must be registered in the dependency injection container.
148+ /// </summary>
149+ /// <value>
150+ /// An <see cref="IPrivilegeContextProvider"/> instance used to asynchronously load the privilege context.
151+ /// This service should be registered in the dependency injection container.
152+ /// </value>
153+ /// <remarks>
154+ /// The provider is responsible for loading user-specific privilege rules and aliases, typically from
155+ /// a database, cache, or external authorization service. The implementation should handle user identity
156+ /// and return appropriate privilege contexts based on the authenticated user's roles and permissions.
157+ /// </remarks>
158+ protected IPrivilegeContextProvider PrivilegeContextProvider { get ; }
159+
160+ /// <summary>
161+ /// Gets or sets the authentication state provider used to retrieve the current user's authentication information.
162+ /// This service is automatically injected from the Blazor authentication system.
163+ /// </summary>
164+ /// <value>
165+ /// An <see cref="AuthenticationStateProvider"/> instance used to retrieve the current user's authentication state.
166+ /// This is passed to the privilege context provider to load user-specific privileges.
167+ /// </value>
168+ /// <remarks>
169+ /// The authentication state contains the current user's <see cref="System.Security.Claims.ClaimsPrincipal"/>,
170+ /// which is used by the privilege context provider to determine what privileges should be loaded.
171+ /// This enables user-specific privilege evaluation based on identity, roles, and claims.
172+ /// </remarks>
173+ protected AuthenticationStateProvider ? AuthenticationStateProvider { get ; }
174+
167175 /// <summary>
168176 /// Gets or sets the privilege context used for evaluating privilege rules.
169177 /// </summary>
@@ -177,7 +185,8 @@ public class PrivilegeContextView : ComponentBase
177185 /// privilege-aware behavior throughout the component tree. The loading state can be determined by
178186 /// checking if this property is <c>null</c>.
179187 /// </remarks>
180- protected PrivilegeContext ? PrivilegeContext { get ; set ; }
188+ protected PrivilegeContext ? PrivilegeContext { get ; private set ; }
189+
181190
182191 /// <summary>
183192 /// Builds the render tree for the component, displaying the appropriate content based on the loading state
@@ -204,8 +213,8 @@ public class PrivilegeContextView : ComponentBase
204213 protected override void BuildRenderTree ( RenderTreeBuilder builder )
205214 {
206215 builder . OpenComponent < CascadingValue < PrivilegeContext ? > > ( 0 ) ;
207- builder . AddAttribute ( 1 , nameof ( CascadingValue < PrivilegeContext ? > . Value ) , PrivilegeContext ) ;
208- builder . AddAttribute ( 2 , nameof ( CascadingValue < PrivilegeContext ? > . ChildContent ) , BuildChildContent ( ) ) ;
216+ builder . AddAttribute ( 1 , nameof ( CascadingValue < > . Value ) , PrivilegeContext ) ;
217+ builder . AddAttribute ( 2 , nameof ( CascadingValue < > . ChildContent ) , BuildChildContent ( ) ) ;
209218 builder . CloseComponent ( ) ;
210219 }
211220
@@ -232,8 +241,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
232241 /// </remarks>
233242 protected override async Task OnInitializedAsync ( )
234243 {
235- var authenticationState = await AuthenticationStateProvider . GetAuthenticationStateAsync ( ) ;
236- PrivilegeContext = await PrivilegeContextProvider . GetContextAsync ( authenticationState . User ) ;
244+ var authenticationState = AuthenticationStateProvider != null ? await AuthenticationStateProvider . GetAuthenticationStateAsync ( ) : null ;
245+ PrivilegeContext = await PrivilegeContextProvider . GetContextAsync ( authenticationState ? . User ) ;
237246 StateHasChanged ( ) ;
238247 }
239248
0 commit comments