forked from casbin-net/casbin-aspnetcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support generic request transformer
Signed-off-by: sagilio <[email protected]>
- Loading branch information
Showing
17 changed files
with
157 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
src/Casbin.AspNetCore.Abstractions/RequestTransformersCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
using System.Collections.Generic; | ||
using System; | ||
using System.Collections.Generic; | ||
using Casbin.Model; | ||
|
||
namespace Casbin.AspNetCore.Authorization | ||
{ | ||
public interface IRequestTransformersCache | ||
public interface IRequestTransformersCache<TRequest> where TRequest : IRequestValues | ||
{ | ||
public IEnumerable<IRequestTransformer>? Transformers { get; set; } | ||
public IReadOnlyList<IRequestTransformer<TRequest>> Transformers { get; } | ||
public bool TryGetTransformer(Type type, out IRequestTransformer<TRequest>? transformer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,36 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Casbin.Model; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Casbin.AspNetCore.Authorization | ||
{ | ||
public class RequestTransformersCache : IRequestTransformersCache | ||
public class RequestTransformersCache<TRequest> : IRequestTransformersCache<TRequest> | ||
where TRequest : IRequestValues | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly IDictionary<Type, IRequestTransformer<TRequest>> _cache = new Dictionary<Type, IRequestTransformer<TRequest>>(); | ||
private readonly List<IRequestTransformer<TRequest>> _transformers; | ||
|
||
public IReadOnlyList<IRequestTransformer<TRequest>> Transformers => _transformers; | ||
|
||
public RequestTransformersCache(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
Initial(); | ||
IEnumerable<IRequestTransformer<TRequest>?>? transformers = serviceProvider.GetServices<IRequestTransformer<TRequest>>(); | ||
foreach (var transformer in transformers) | ||
{ | ||
if (transformer is null) | ||
{ | ||
continue; | ||
} | ||
_cache[transformer.GetType()] = transformer; | ||
} | ||
_transformers = _cache.Values.ToList(); | ||
} | ||
|
||
public IEnumerable<IRequestTransformer>? Transformers { get; set; } | ||
|
||
private void Initial() | ||
public bool TryGetTransformer(Type type, out IRequestTransformer<TRequest>? transformer) | ||
{ | ||
Transformers ??= _serviceProvider.GetServices<IRequestTransformer>().ToArray(); | ||
return _cache.TryGetValue(type, out transformer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.