-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 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
54 changes: 54 additions & 0 deletions
54
src/IdServer/SimpleIdServer.IdServer.Store.EF/CasbinPolicyRepository.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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using SimpleIdServer.IdServer.Store.EF.Configurations; | ||
using SimpleIdServer.IdServer.Store.EF.Models; | ||
|
||
namespace SimpleIdServer.IdServer.Store.EF | ||
{ | ||
public interface ICasbinPolicyRepository | ||
{ | ||
Task<List<CasbinPolicy>> GetAll(string tableName, CancellationToken cancellationToken); | ||
Task<int> SaveChanges(string tableName,CancellationToken cancellationToken); | ||
} | ||
|
||
public class CasbinPolicyRepository : ICasbinPolicyRepository | ||
{ | ||
private readonly DbContextOptions<CustomDataContext> _options; | ||
private readonly Dictionary<string, CustomDataContext> _dic = new Dictionary<string, CustomDataContext>(); | ||
|
||
public CasbinPolicyRepository(DbContextOptions<CustomDataContext> options) | ||
{ | ||
_options = options; | ||
} | ||
|
||
public async Task<List<CasbinPolicy>> GetAll(string tableName, CancellationToken cancellationToken) | ||
{ | ||
var ctx = new CustomDataContext(_options); | ||
ctx.TableName = tableName; | ||
_dic.Add(tableName, ctx); | ||
return await ctx.CasbinPolices.ToListAsync(cancellationToken); | ||
} | ||
|
||
public async Task<int> SaveChanges(string tableName, CancellationToken cancellationToken) | ||
{ | ||
var ctx = _dic[tableName]; | ||
return await ctx.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
|
||
public class CustomDataContext : DbContext | ||
{ | ||
|
||
public CustomDataContext(DbContextOptions<CustomDataContext> options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<CasbinPolicy> CasbinPolices { get; set; } | ||
public string TableName { get; set; } | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
base.OnModelCreating(modelBuilder); | ||
modelBuilder.ApplyConfiguration(new CastbinConfiguration(TableName)); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/IdServer/SimpleIdServer.IdServer.Store.EF/Configurations/CastbinConfiguration.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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using SimpleIdServer.IdServer.Store.EF.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace SimpleIdServer.IdServer.Store.EF.Configurations | ||
{ | ||
public class CastbinConfiguration : IEntityTypeConfiguration<CasbinPolicy> | ||
{ | ||
private static string _tableName; | ||
|
||
public CastbinConfiguration(string tableName) | ||
{ | ||
_tableName = tableName; | ||
} | ||
|
||
public void Configure(EntityTypeBuilder<CasbinPolicy> builder) | ||
{ | ||
builder.ToTable(_tableName); | ||
builder.HasKey(x => x.Id); | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/IdServer/SimpleIdServer.IdServer.Store.EF/Models/CasbinPolicy.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SimpleIdServer.IdServer.Store.EF.Models; | ||
|
||
public class CasbinPolicy | ||
{ | ||
public string Id { get; set; } | ||
public string PolicyName { get; set; } | ||
} |
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