11using DynamicAuthorization . Mvc . Core ;
2+ using Microsoft . Data . SqlClient ;
3+ using Microsoft . Extensions . Logging ;
4+ using Newtonsoft . Json ;
5+ using System ;
6+ using System . Collections . Generic ;
7+ using System . Data ;
28using System . Threading . Tasks ;
39
410namespace DynamicAuthorization . Mvc . MsSqlServerStore
511{
612 public class RoleAccessStore : IRoleAccessStore
713 {
8- public Task < bool > AddRoleAccessAsync ( RoleAccess roleAccess )
14+ private readonly SqlOptions _options ;
15+ private readonly ILogger < RoleAccessStore > _logger ;
16+
17+ public RoleAccessStore ( SqlOptions options , ILogger < RoleAccessStore > logger )
918 {
10- throw new System . NotImplementedException ( ) ;
19+ _options = options ;
20+ _logger = logger ;
21+ }
22+
23+ public async Task < bool > AddRoleAccessAsync ( RoleAccess roleAccess )
24+ {
25+ try
26+ {
27+ using ( var conn = new SqlConnection ( _options . ConnectionString ) )
28+ {
29+ const string insertCommand = "INSERT INTO RoleAccess VALUES(@RoleId, @Access)" ;
30+ using ( var cmd = new SqlCommand ( insertCommand , conn ) )
31+ {
32+ cmd . CommandType = CommandType . Text ;
33+ cmd . Parameters . AddWithValue ( "@RoleId" , roleAccess . RoleId ) ;
34+
35+ var access = JsonConvert . SerializeObject ( roleAccess . Controllers ) ;
36+ cmd . Parameters . AddWithValue ( "@Access" , access ) ;
37+
38+ conn . Open ( ) ;
39+ var affectedRows = await cmd . ExecuteNonQueryAsync ( ) ;
40+ return affectedRows > 0 ;
41+ }
42+ }
43+ }
44+ catch ( Exception ex )
45+ {
46+ _logger . LogError ( ex , "An error has occurred while inserting access into RoleAccess table" ) ;
47+ return false ;
48+ }
1149 }
1250
1351 public Task < bool > EditRoleAccessAsync ( RoleAccess roleAccess )
@@ -20,9 +58,37 @@ public Task<bool> RemoveRoleAccessAsync(string roleId)
2058 throw new System . NotImplementedException ( ) ;
2159 }
2260
23- public Task < RoleAccess > GetRoleAccessAsync ( string roleId )
61+ public async Task < RoleAccess > GetRoleAccessAsync ( string roleId )
2462 {
25- throw new System . NotImplementedException ( ) ;
63+ try
64+ {
65+ using ( var conn = new SqlConnection ( _options . ConnectionString ) )
66+ {
67+ const string query = "SELECT [Id], [RoleId], [Access] FROM [RoleAccess] WHERE [RoleId] = @RoleId" ;
68+ using ( var cmd = new SqlCommand ( query , conn ) )
69+ {
70+ cmd . CommandType = CommandType . Text ;
71+ cmd . Parameters . AddWithValue ( "@RoleId" , roleId ) ;
72+ conn . Open ( ) ;
73+ var reader = await cmd . ExecuteReaderAsync ( ) ;
74+ if ( ! reader . Read ( ) )
75+ return null ;
76+
77+ var roleAccess = new RoleAccess ( ) ;
78+ roleAccess . Id = int . Parse ( reader [ 0 ] . ToString ( ) ) ;
79+ roleAccess . RoleId = reader [ 1 ] . ToString ( ) ;
80+ var json = reader [ 2 ] . ToString ( ) ;
81+ roleAccess . Controllers = JsonConvert . DeserializeObject < IEnumerable < MvcControllerInfo > > ( json ) ;
82+
83+ return roleAccess ;
84+ }
85+ }
86+ }
87+ catch ( Exception ex )
88+ {
89+ _logger . LogError ( ex , "An error has occurred while inserting access into RoleAccess table" ) ;
90+ return null ;
91+ }
2692 }
2793
2894 public Task < bool > HasAccessToActionAsync ( string actionId , params string [ ] roles )
0 commit comments