zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
commit | author | age
921de2 1 using Bootstrap.Admin.Pages.Extensions;
Z 2 using Bootstrap.Security.Authorization;
3 using Microsoft.AspNetCore.Components;
4 using Microsoft.AspNetCore.Components.Authorization;
5 using Microsoft.AspNetCore.Components.Rendering;
6 using System;
7 using System.Threading.Tasks;
8
9 namespace Bootstrap.Admin.Pages.Components
10 {
11     /// <summary>
12     /// 授权组件
13     /// </summary>
14     public class AuthorizateComponent : ComponentBase
15     {
16         /// <summary>
17         /// 授权键值
18         /// </summary>
19         [Parameter]
20         public string Key { get; set; } = "";
21
22         /// <summary>
23         /// 
24         /// </summary>
25         [Parameter]
26         public RenderFragment? ChildContent { get; set; }
27
28         /// <summary>
29         /// 
30         /// </summary>
31         [Inject]
32         protected IButtonAuthorization? AuthorizationServices { get; set; }
33
34         /// <summary>
35         ///
36         /// </summary>
37         [Inject]
38         protected AuthenticationStateProvider? AuthenticationStateProvider { get; set; }
39
40         /// <summary>
41         ///
42         /// </summary>
43         [Inject]
44         protected NavigationManager? NavigationManager { get; set; }
45
46         private bool authorizated;
47
48         /// <summary>
49         /// 
50         /// </summary>
51         protected override async Task OnParametersSetAsync()
52         {
53             if (AuthenticationStateProvider != null)
54             {
55                 var user = await AuthenticationStateProvider.GetAuthenticationStateAsync();
56                 var url = new UriBuilder(NavigationManager?.Uri ?? "").Path;
57                 authorizated = AuthorizationServices != null && AuthorizationServices.Authorizate(user.User, url.ToMvcMenuUrl(), Key);
58             }
59         }
60
61         /// <summary>
62         /// 
63         /// </summary>
64         /// <param name="builder"></param>
65         protected override void BuildRenderTree(RenderTreeBuilder builder)
66         {
67             if (authorizated) builder.AddContent(0, ChildContent);
68         }
69     }
70 }