1+ using DynamicAuthorization . Mvc . Core . Models ;
2+ using Microsoft . AspNetCore . Identity ;
3+ using Microsoft . AspNetCore . Identity . EntityFrameworkCore ;
4+ using Microsoft . AspNetCore . Mvc . Rendering ;
5+ using Microsoft . AspNetCore . Mvc . ViewFeatures ;
6+ using Microsoft . AspNetCore . Razor . TagHelpers ;
7+ using Microsoft . EntityFrameworkCore ;
8+ using System ;
9+ using System . Linq ;
10+ using System . Threading . Tasks ;
11+
12+ namespace DynamicAuthorization . Mvc . Core
13+ {
14+ public abstract class SecureContentTagHelper < TDbContext >
15+ : SecureContentTagHelper < TDbContext , IdentityUser , IdentityRole , string >
16+ where TDbContext : IdentityDbContext
17+ {
18+ public SecureContentTagHelper (
19+ TDbContext dbContext ,
20+ DynamicAuthorizationOptions authorizationOptions ,
21+ IRoleAccessStore roleAccessStore
22+ ) : base ( dbContext , authorizationOptions , roleAccessStore )
23+ {
24+ }
25+ }
26+
27+ public abstract class SecureContentTagHelper < TDbContext , TUser >
28+ : SecureContentTagHelper < TDbContext , TUser , IdentityRole , string >
29+ where TDbContext : IdentityDbContext < TUser >
30+ where TUser : IdentityUser
31+ {
32+ public SecureContentTagHelper (
33+ TDbContext dbContext ,
34+ DynamicAuthorizationOptions authorizationOptions ,
35+ IRoleAccessStore roleAccessStore
36+ ) : base ( dbContext , authorizationOptions , roleAccessStore )
37+ {
38+ }
39+ }
40+
41+ public abstract class SecureContentTagHelper < TDbContext , TUser , TRole , TKey > : TagHelper
42+ where TDbContext : IdentityDbContext < TUser , TRole , TKey >
43+ where TUser : IdentityUser < TKey >
44+ where TRole : IdentityRole < TKey >
45+ where TKey : IEquatable < TKey >
46+ {
47+ private readonly TDbContext _dbContext ;
48+ private readonly DynamicAuthorizationOptions _authorizationOptions ;
49+ private readonly IRoleAccessStore _roleAccessStore ;
50+
51+ public SecureContentTagHelper (
52+ TDbContext dbContext ,
53+ DynamicAuthorizationOptions authorizationOptions ,
54+ IRoleAccessStore roleAccessStore
55+ )
56+ {
57+ _dbContext = dbContext ;
58+ _authorizationOptions = authorizationOptions ;
59+ _roleAccessStore = roleAccessStore ;
60+ }
61+
62+ [ HtmlAttributeName ( "asp-area" ) ]
63+ public string Area { get ; set ; }
64+
65+ [ HtmlAttributeName ( "asp-controller" ) ]
66+ public string Controller { get ; set ; }
67+
68+ [ HtmlAttributeName ( "asp-action" ) ]
69+ public string Action { get ; set ; }
70+
71+ [ ViewContext , HtmlAttributeNotBound ]
72+ public ViewContext ViewContext { get ; set ; }
73+
74+ public override async Task ProcessAsync ( TagHelperContext context , TagHelperOutput output )
75+ {
76+ output . TagName = null ;
77+ var user = ViewContext . HttpContext . User ;
78+
79+ if ( ! user . Identity . IsAuthenticated )
80+ {
81+ output . SuppressOutput ( ) ;
82+ return ;
83+ }
84+
85+ if ( user . Identity . Name . Equals ( _authorizationOptions . DefaultAdminUser , StringComparison . CurrentCultureIgnoreCase ) )
86+ return ;
87+
88+ var actionId = $ "{ Area } :{ Controller } :{ Action } ";
89+
90+ var roles = await (
91+ from usr in _dbContext . Users
92+ join userRole in _dbContext . UserRoles on usr . Id equals userRole . UserId
93+ join role in _dbContext . Roles on userRole . RoleId equals role . Id
94+ where usr . UserName == user . Identity . Name
95+ select role . Id . ToString ( )
96+ ) . ToArrayAsync ( ) ;
97+
98+ if ( await _roleAccessStore . HasAccessToActionAsync ( actionId , roles ) )
99+ return ;
100+
101+ output . SuppressOutput ( ) ;
102+ }
103+ }
104+ }
0 commit comments