Skip to content

Commit d4fc9e0

Browse files
committed
Fix json store update.
1 parent dfe5422 commit d4fc9e0

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

src/DynamicAuthorization.Mvc.Core/IRoleAccessStore.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

src/DynamicAuthorization.Mvc.JsonStore/Extensions/DynamicAuthorizationJsonBuilderExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using JsonFlatFileDataStore;
33
using Microsoft.Extensions.DependencyInjection;
44
using System;
5+
using System.IO;
56

67
namespace 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
}

src/DynamicAuthorization.Mvc.JsonStore/JsonOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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
}

src/DynamicAuthorization.Mvc.JsonStore/RoleAccessStore.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff 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())

0 commit comments

Comments
 (0)