Skip to content

Commit aa39622

Browse files
authored
Add nullable annotations to M.A.Components.Authorization (#32831)
* Add nullable annotations to M.A.Components.Authorization
1 parent 2105ef1 commit aa39622

11 files changed

+110
-46
lines changed

src/Components/Authorization/src/AttributeAuthorizeDataCache.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ namespace Microsoft.AspNetCore.Components.Authorization
1010
{
1111
internal static class AttributeAuthorizeDataCache
1212
{
13-
private static ConcurrentDictionary<Type, IAuthorizeData[]> _cache
14-
= new ConcurrentDictionary<Type, IAuthorizeData[]>();
13+
private static readonly ConcurrentDictionary<Type, IAuthorizeData[]?> _cache = new();
1514

16-
public static IAuthorizeData[] GetAuthorizeDataForType(Type type)
15+
public static IAuthorizeData[]? GetAuthorizeDataForType(Type type)
1716
{
18-
IAuthorizeData[] result;
19-
if (!_cache.TryGetValue(type, out result))
17+
if (!_cache.TryGetValue(type, out var result))
2018
{
2119
result = ComputeAuthorizeDataForType(type);
2220
_cache[type] = result; // Safe race - doesn't matter if it overwrites
@@ -25,11 +23,11 @@ public static IAuthorizeData[] GetAuthorizeDataForType(Type type)
2523
return result;
2624
}
2725

28-
private static IAuthorizeData[] ComputeAuthorizeDataForType(Type type)
26+
private static IAuthorizeData[]? ComputeAuthorizeDataForType(Type type)
2927
{
3028
// Allow Anonymous skips all authorization
3129
var allAttributes = type.GetCustomAttributes(inherit: true);
32-
List<IAuthorizeData> authorizeDatas = null;
30+
List<IAuthorizeData>? authorizeDatas = null;
3331
for (var i = 0; i < allAttributes.Length; i++)
3432
{
3533
if (allAttributes[i] is IAllowAnonymous)

src/Components/Authorization/src/AuthenticationStateProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public abstract class AuthenticationStateProvider
2121
/// An event that provides notification when the <see cref="AuthenticationState"/>
2222
/// has changed. For example, this event may be raised if a user logs in or out.
2323
/// </summary>
24-
public event AuthenticationStateChangedHandler AuthenticationStateChanged;
24+
public event AuthenticationStateChangedHandler? AuthenticationStateChanged;
2525

2626
/// <summary>
2727
/// Raises the <see cref="AuthenticationStateChanged"/> event.

src/Components/Authorization/src/AuthorizeDataAdapter.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@ public AuthorizeDataAdapter(AuthorizeView component)
1616
_component = component ?? throw new ArgumentNullException(nameof(component));
1717
}
1818

19-
public string Policy
19+
public string? Policy
2020
{
2121
get => _component.Policy;
2222
set => throw new NotSupportedException();
2323
}
2424

25-
public string Roles
25+
public string? Roles
2626
{
2727
get => _component.Roles;
2828
set => throw new NotSupportedException();
2929
}
3030

3131
// AuthorizeView doesn't expose any such parameter, as it wouldn't be used anyway,
3232
// since we already have the ClaimsPrincipal by the time AuthorizeView gets involved.
33-
public string AuthenticationSchemes
33+
public string? AuthenticationSchemes
3434
{
3535
get => null;
3636
set => throw new NotSupportedException();

src/Components/Authorization/src/AuthorizeRouteView.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ public AuthorizeRouteView()
4949
/// The content that will be displayed if the user is not authorized.
5050
/// </summary>
5151
[Parameter]
52-
public RenderFragment<AuthenticationState> NotAuthorized { get; set; }
52+
public RenderFragment<AuthenticationState>? NotAuthorized { get; set; }
5353

5454
/// <summary>
5555
/// The content that will be displayed while asynchronous authorization is in progress.
5656
/// </summary>
5757
[Parameter]
58-
public RenderFragment Authorizing { get; set; }
58+
public RenderFragment? Authorizing { get; set; }
5959

6060
/// <summary>
6161
/// The resource to which access is being controlled.
6262
/// </summary>
6363
[Parameter]
64-
public object Resource { get; set; }
64+
public object? Resource { get; set; }
6565

6666
[CascadingParameter]
67-
private Task<AuthenticationState> ExistingCascadedAuthenticationState { get; set; }
67+
private Task<AuthenticationState>? ExistingCascadedAuthenticationState { get; set; }
6868

6969
/// <inheritdoc />
7070
protected override void Render(RenderTreeBuilder builder)
@@ -115,12 +115,12 @@ private void RenderAuthorizingInDefaultLayout(RenderTreeBuilder builder)
115115
RenderContentInDefaultLayout(builder, content);
116116
}
117117

118-
private class AuthorizeRouteViewCore : AuthorizeViewCore
118+
private sealed class AuthorizeRouteViewCore : AuthorizeViewCore
119119
{
120120
[Parameter]
121-
public RouteData RouteData { get; set; }
121+
public RouteData RouteData { get; set; } = default!;
122122

123-
protected override IAuthorizeData[] GetAuthorizeData()
123+
protected override IAuthorizeData[]? GetAuthorizeData()
124124
=> AttributeAuthorizeDataCache.GetAuthorizeDataForType(RouteData.PageType);
125125
}
126126
}

src/Components/Authorization/src/AuthorizeView.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public AuthorizeView()
2323
/// <summary>
2424
/// The policy name that determines whether the content can be displayed.
2525
/// </summary>
26-
[Parameter] public string Policy { get; set; }
26+
[Parameter] public string? Policy { get; set; }
2727

2828
/// <summary>
2929
/// A comma delimited list of roles that are allowed to display the content.
3030
/// </summary>
31-
[Parameter] public string Roles { get; set; }
31+
[Parameter] public string? Roles { get; set; }
3232

3333
/// <summary>
3434
/// Gets the data used for authorization.

src/Components/Authorization/src/AuthorizeViewCore.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,40 @@ namespace Microsoft.AspNetCore.Components.Authorization
1414
/// </summary>
1515
public abstract class AuthorizeViewCore : ComponentBase
1616
{
17-
private AuthenticationState currentAuthenticationState;
17+
private AuthenticationState? currentAuthenticationState;
1818
private bool? isAuthorized;
1919

2020
/// <summary>
2121
/// The content that will be displayed if the user is authorized.
2222
/// </summary>
23-
[Parameter] public RenderFragment<AuthenticationState> ChildContent { get; set; }
23+
[Parameter] public RenderFragment<AuthenticationState>? ChildContent { get; set; }
2424

2525
/// <summary>
2626
/// The content that will be displayed if the user is not authorized.
2727
/// </summary>
28-
[Parameter] public RenderFragment<AuthenticationState> NotAuthorized { get; set; }
28+
[Parameter] public RenderFragment<AuthenticationState>? NotAuthorized { get; set; }
2929

3030
/// <summary>
3131
/// The content that will be displayed if the user is authorized.
3232
/// If you specify a value for this parameter, do not also specify a value for <see cref="ChildContent"/>.
3333
/// </summary>
34-
[Parameter] public RenderFragment<AuthenticationState> Authorized { get; set; }
34+
[Parameter] public RenderFragment<AuthenticationState>? Authorized { get; set; }
3535

3636
/// <summary>
3737
/// The content that will be displayed while asynchronous authorization is in progress.
3838
/// </summary>
39-
[Parameter] public RenderFragment Authorizing { get; set; }
39+
[Parameter] public RenderFragment? Authorizing { get; set; }
4040

4141
/// <summary>
4242
/// The resource to which access is being controlled.
4343
/// </summary>
44-
[Parameter] public object Resource { get; set; }
44+
[Parameter] public object? Resource { get; set; }
4545

46-
[CascadingParameter] private Task<AuthenticationState> AuthenticationState { get; set; }
46+
[CascadingParameter] private Task<AuthenticationState>? AuthenticationState { get; set; }
4747

48-
[Inject] private IAuthorizationPolicyProvider AuthorizationPolicyProvider { get; set; }
48+
[Inject] private IAuthorizationPolicyProvider AuthorizationPolicyProvider { get; set; } = default!;
4949

50-
[Inject] private IAuthorizationService AuthorizationService { get; set; }
50+
[Inject] private IAuthorizationService AuthorizationService { get; set; } = default!;
5151

5252
/// <inheritdoc />
5353
protected override void BuildRenderTree(RenderTreeBuilder builder)
@@ -61,11 +61,11 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
6161
else if (isAuthorized == true)
6262
{
6363
var authorized = Authorized ?? ChildContent;
64-
builder.AddContent(0, authorized?.Invoke(currentAuthenticationState));
64+
builder.AddContent(0, authorized?.Invoke(currentAuthenticationState!));
6565
}
6666
else
6767
{
68-
builder.AddContent(0, NotAuthorized?.Invoke(currentAuthenticationState));
68+
builder.AddContent(0, NotAuthorized?.Invoke(currentAuthenticationState!));
6969
}
7070
}
7171

@@ -96,7 +96,7 @@ protected override async Task OnParametersSetAsync()
9696
/// <summary>
9797
/// Gets the data required to apply authorization rules.
9898
/// </summary>
99-
protected abstract IAuthorizeData[] GetAuthorizeData();
99+
protected abstract IAuthorizeData[]? GetAuthorizeData();
100100

101101
private async Task<bool> IsAuthorizedAsync(ClaimsPrincipal user)
102102
{
@@ -111,7 +111,7 @@ private async Task<bool> IsAuthorizedAsync(ClaimsPrincipal user)
111111

112112
var policy = await AuthorizationPolicy.CombineAsync(
113113
AuthorizationPolicyProvider, authorizeData);
114-
var result = await AuthorizationService.AuthorizeAsync(user, Resource, policy);
114+
var result = await AuthorizationService.AuthorizeAsync(user, Resource, policy!);
115115
return result.Succeeded;
116116
}
117117

src/Components/Authorization/src/CascadingAuthenticationState.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<CascadingValue TValue="Task<AuthenticationState>" Value="@_currentAuthenticationStateTask" ChildContent="@ChildContent" />
55

66
@code {
7-
private Task<AuthenticationState> _currentAuthenticationStateTask;
7+
private Task<AuthenticationState>? _currentAuthenticationStateTask;
88

99
/// <summary>
1010
/// The content to which the authentication state should be provided.
1111
/// </summary>
1212
[Parameter]
13-
public RenderFragment ChildContent { get; set; }
13+
public RenderFragment? ChildContent { get; set; }
1414

1515
protected override void OnInitialized()
1616
{

src/Components/Authorization/src/Microsoft.AspNetCore.Components.Authorization.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Description>Authentication and authorization support for Blazor applications.</Description>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<Trimmable>true</Trimmable>
9-
<Nullable>disable</Nullable>
9+
<Nullable>enable</Nullable>
1010
</PropertyGroup>
1111

1212
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -1 +1,65 @@
11
#nullable enable
2+
*REMOVED*Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider.AuthenticationStateChanged -> Microsoft.AspNetCore.Components.Authorization.AuthenticationStateChangedHandler
3+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthenticationState.AuthenticationState(System.Security.Claims.ClaimsPrincipal user) -> void
4+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthenticationState.User.get -> System.Security.Claims.ClaimsPrincipal
5+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider.NotifyAuthenticationStateChanged(System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState> task) -> void
6+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Authorizing.get -> Microsoft.AspNetCore.Components.RenderFragment
7+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Authorizing.set -> void
8+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.NotAuthorized.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Authorization.AuthenticationState>
9+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.NotAuthorized.set -> void
10+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Resource.get -> object
11+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Resource.set -> void
12+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeView.Policy.get -> string
13+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeView.Policy.set -> void
14+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeView.Roles.get -> string
15+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeView.Roles.set -> void
16+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Authorized.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Authorization.AuthenticationState>
17+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Authorized.set -> void
18+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Authorizing.get -> Microsoft.AspNetCore.Components.RenderFragment
19+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Authorizing.set -> void
20+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Authorization.AuthenticationState>
21+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.ChildContent.set -> void
22+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.NotAuthorized.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Authorization.AuthenticationState>
23+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.NotAuthorized.set -> void
24+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Resource.get -> object
25+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Resource.set -> void
26+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.IHostEnvironmentAuthenticationStateProvider.SetAuthenticationState(System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState> authenticationStateTask) -> void
27+
*REMOVED*~abstract Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider.GetAuthenticationStateAsync() -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState>
28+
*REMOVED*~abstract Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.GetAuthorizeData() -> Microsoft.AspNetCore.Authorization.IAuthorizeData[]
29+
*REMOVED*~override Microsoft.AspNetCore.Components.Authorization.AuthorizeView.GetAuthorizeData() -> Microsoft.AspNetCore.Authorization.IAuthorizeData[]
30+
*REMOVED*~override Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder builder) -> void
31+
*REMOVED*~override Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync() -> System.Threading.Tasks.Task
32+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment
33+
*REMOVED*~Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState.ChildContent.set -> void
34+
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider.AuthenticationStateChanged -> Microsoft.AspNetCore.Components.Authorization.AuthenticationStateChangedHandler?
35+
Microsoft.AspNetCore.Components.Authorization.AuthenticationState.AuthenticationState(System.Security.Claims.ClaimsPrincipal! user) -> void
36+
Microsoft.AspNetCore.Components.Authorization.AuthenticationState.User.get -> System.Security.Claims.ClaimsPrincipal!
37+
Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider.NotifyAuthenticationStateChanged(System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>! task) -> void
38+
Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Authorizing.get -> Microsoft.AspNetCore.Components.RenderFragment?
39+
Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Authorizing.set -> void
40+
Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.NotAuthorized.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>?
41+
Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.NotAuthorized.set -> void
42+
Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Resource.get -> object?
43+
Microsoft.AspNetCore.Components.Authorization.AuthorizeRouteView.Resource.set -> void
44+
Microsoft.AspNetCore.Components.Authorization.AuthorizeView.Policy.get -> string?
45+
Microsoft.AspNetCore.Components.Authorization.AuthorizeView.Policy.set -> void
46+
Microsoft.AspNetCore.Components.Authorization.AuthorizeView.Roles.get -> string?
47+
Microsoft.AspNetCore.Components.Authorization.AuthorizeView.Roles.set -> void
48+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Authorized.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>?
49+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Authorized.set -> void
50+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Authorizing.get -> Microsoft.AspNetCore.Components.RenderFragment?
51+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Authorizing.set -> void
52+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>?
53+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.ChildContent.set -> void
54+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.NotAuthorized.get -> Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>?
55+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.NotAuthorized.set -> void
56+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Resource.get -> object?
57+
Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.Resource.set -> void
58+
Microsoft.AspNetCore.Components.Authorization.IHostEnvironmentAuthenticationStateProvider.SetAuthenticationState(System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>! authenticationStateTask) -> void
59+
abstract Microsoft.AspNetCore.Components.Authorization.AuthenticationStateProvider.GetAuthenticationStateAsync() -> System.Threading.Tasks.Task<Microsoft.AspNetCore.Components.Authorization.AuthenticationState!>!
60+
abstract Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.GetAuthorizeData() -> Microsoft.AspNetCore.Authorization.IAuthorizeData![]?
61+
override Microsoft.AspNetCore.Components.Authorization.AuthorizeView.GetAuthorizeData() -> Microsoft.AspNetCore.Authorization.IAuthorizeData![]!
62+
override Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder! builder) -> void
63+
override Microsoft.AspNetCore.Components.Authorization.AuthorizeViewCore.OnParametersSetAsync() -> System.Threading.Tasks.Task!
64+
Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState.ChildContent.get -> Microsoft.AspNetCore.Components.RenderFragment?
65+
Microsoft.AspNetCore.Components.Authorization.CascadingAuthenticationState.ChildContent.set -> void

0 commit comments

Comments
 (0)