Skip to content

Commit bff1b40

Browse files
Split AuthorizeView in two, so "Core" part can be reused from routing
1 parent f14a3f0 commit bff1b40

File tree

2 files changed

+52
-21
lines changed

2 files changed

+52
-21
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.AspNetCore.Authorization;
5+
6+
namespace Microsoft.AspNetCore.Components
7+
{
8+
#pragma warning disable PUB0001 // Pubternal type in public API
9+
/// <summary>
10+
/// Displays differing content depending on the user's authorization status.
11+
/// </summary>
12+
public class AuthorizeView : Internal.AuthorizeViewCore
13+
#pragma warning restore PUB0001 // Pubternal type in public API
14+
{
15+
private readonly IAuthorizeData[] selfAsAuthorizeData;
16+
17+
/// <summary>
18+
/// Constructs an instance of <see cref="AuthorizeView"/>.
19+
/// </summary>
20+
public AuthorizeView()
21+
{
22+
selfAsAuthorizeData = new[] { new AuthorizeDataAdapter(this) };
23+
}
24+
25+
/// <summary>
26+
/// The policy name that determines whether the content can be displayed.
27+
/// </summary>
28+
[Parameter] public string Policy { get; private set; }
29+
30+
/// <summary>
31+
/// A comma delimited list of roles that are allowed to display the content.
32+
/// </summary>
33+
[Parameter] public string Roles { get; private set; }
34+
35+
/// <summary>
36+
/// Gets the data used for authorization.
37+
/// </summary>
38+
protected override IAuthorizeData[] AuthorizeData => selfAsAuthorizeData;
39+
}
40+
}

src/Components/Components/src/Auth/AuthorizeView.razor renamed to src/Components/Components/src/Auth/AuthorizeViewCore.razor

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
@namespace Microsoft.AspNetCore.Components
1+
@*
2+
Ideally, this would be an internal (and probably abstract) class. So temporarily
3+
this is in an "internal" namespace and should become actually internal in the future.
4+
*@
5+
@namespace Microsoft.AspNetCore.Components.Internal
26
@using System.Security.Claims
37
@using Microsoft.AspNetCore.Authorization
48
@inject IAuthorizationService AuthorizationService
@@ -18,7 +22,6 @@ else
1822
}
1923

2024
@functions {
21-
private IAuthorizeData[] selfAsAuthorizeData;
2225
private AuthenticationState currentAuthenticationState;
2326
private bool isAuthorized;
2427

@@ -45,29 +48,11 @@ else
4548
/// </summary>
4649
[Parameter] public RenderFragment Authorizing { get; private set; }
4750

48-
/// <summary>
49-
/// The policy name that determines whether the content can be displayed.
50-
/// </summary>
51-
[Parameter] public string Policy { get; private set; }
52-
53-
/// <summary>
54-
/// A comma delimited list of roles that are allowed to display the content.
55-
/// </summary>
56-
[Parameter] public string Roles { get; private set; }
57-
5851
/// <summary>
5952
/// The resource to which access is being controlled.
6053
/// </summary>
6154
[Parameter] public object Resource { get; private set; }
6255

63-
protected override void OnInit()
64-
{
65-
selfAsAuthorizeData = new[]
66-
{
67-
new AuthorizeDataAdapter((AuthorizeView)(object)this)
68-
};
69-
}
70-
7156
protected override async Task OnParametersSetAsync()
7257
{
7358
// We allow 'ChildContent' for convenience in basic cases, and 'Authorized' for symmetry
@@ -89,10 +74,16 @@ else
8974
isAuthorized = await IsAuthorizedAsync(currentAuthenticationState.User);
9075
}
9176

77+
// TODO: Make abstract instead
78+
protected virtual IAuthorizeData[] AuthorizeData
79+
{
80+
get => throw new NotImplementedException($"{nameof(AuthorizeData)} must be overridden and supplied by a derived class.");
81+
}
82+
9283
private async Task<bool> IsAuthorizedAsync(ClaimsPrincipal user)
9384
{
9485
var policy = await AuthorizationPolicy.CombineAsync(
95-
AuthorizationPolicyProvider, selfAsAuthorizeData);
86+
AuthorizationPolicyProvider, AuthorizeData);
9687
var result = await AuthorizationService.AuthorizeAsync(user, Resource, policy);
9788
return result.Succeeded;
9889
}

0 commit comments

Comments
 (0)