File tree Expand file tree Collapse file tree 4 files changed +19
-5
lines changed
DynamicAuthorization.Mvc.Core
DynamicAuthorization.Mvc.JsonStore Expand file tree Collapse file tree 4 files changed +19
-5
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ public interface IRoleAccessStore
1010
1111 Task < bool > RemoveRoleAccessAsync ( string roleId ) ;
1212
13+ Task < RoleAccess > GetRoleAccessAsync ( string roleId ) ;
14+
1315 Task < bool > HasAccessToActionAsync ( string actionId , params string [ ] roles ) ;
1416 }
1517}
Original file line number Diff line number Diff line change 22using JsonFlatFileDataStore ;
33using Microsoft . Extensions . DependencyInjection ;
44using System ;
5+ using System . IO ;
56
67namespace DynamicAuthorization . Mvc . JsonStore . Extensions
78{
@@ -28,8 +29,8 @@ public static IDynamicAuthorizationBuilder AddJsonStore(this IDynamicAuthorizati
2829 var jsonOptions = new JsonOptions ( ) ;
2930 options . Invoke ( jsonOptions ) ;
3031
31- if ( jsonOptions . FileName == null )
32- throw new NullReferenceException ( nameof ( jsonOptions . FileName ) ) ;
32+ if ( jsonOptions . FilePath == null )
33+ throw new NullReferenceException ( nameof ( jsonOptions . FilePath ) ) ;
3334
3435 AddRequiredServices ( builder . Services , jsonOptions ) ;
3536
@@ -38,8 +39,11 @@ public static IDynamicAuthorizationBuilder AddJsonStore(this IDynamicAuthorizati
3839
3940 private static void AddRequiredServices ( IServiceCollection services , JsonOptions jsonOptions )
4041 {
42+ if ( jsonOptions . FilePath == "RoleAccess.json" )
43+ jsonOptions . FilePath = $ "{ Directory . GetCurrentDirectory ( ) } \\ { jsonOptions . FilePath } ";
44+
4145 services . AddSingleton ( jsonOptions ) ;
42- services . AddSingleton ( new DataStore ( jsonOptions . FileName ) ) ;
46+ services . AddSingleton ( provider => new DataStore ( jsonOptions . FilePath , keyProperty : "roleId" ) ) ;
4347 services . AddScoped < IRoleAccessStore , RoleAccessStore > ( ) ;
4448 }
4549 }
Original file line number Diff line number Diff line change 22{
33 public class JsonOptions
44 {
5- public string FileName { get ; set ; } = "RoleAccess.json" ;
5+ public string FilePath { get ; set ; } = "RoleAccess.json" ;
66
77 public bool UseMemoryCache { get ; set ; } = true ;
88 }
Original file line number Diff line number Diff line change @@ -28,15 +28,23 @@ public Task<bool> EditRoleAccessAsync(RoleAccess roleAccess)
2828 {
2929 var collection = _store . GetCollection < RoleAccess > ( ) ;
3030
31- return collection . UpdateOneAsync ( roleAccess . RoleId , roleAccess ) ;
31+ return collection . ReplaceOneAsync ( roleAccess . RoleId , roleAccess ) ;
3232 }
3333
3434 public Task < bool > RemoveRoleAccessAsync ( string roleId )
3535 {
3636 var collection = _store . GetCollection < RoleAccess > ( ) ;
37+
3738 return collection . DeleteOneAsync ( roleId ) ;
3839 }
3940
41+ public Task < RoleAccess > GetRoleAccessAsync ( string roleId )
42+ {
43+ var collection = _store . GetCollection < RoleAccess > ( ) ;
44+
45+ return Task . FromResult ( collection . AsQueryable ( ) . FirstOrDefault ( ra => ra . RoleId == roleId ) ) ;
46+ }
47+
4048 public Task < bool > HasAccessToActionAsync ( string actionId , params string [ ] roles )
4149 {
4250 if ( roles == null || ! roles . Any ( ) )
You can’t perform that action at this time.
0 commit comments