Skip to content

Commit

Permalink
Dynamic table name
Browse files Browse the repository at this point in the history
  • Loading branch information
thabart committed Oct 26, 2024
1 parent 27c0618 commit 68e538c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/IdServer/SimpleIdServer.IdServer.Startup/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) SimpleIdServer. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using Community.Microsoft.Extensions.Caching.PostgreSql;
using FirebaseAdmin.Messaging;
using MassTransit;
using Microsoft.AspNetCore.Authentication.Certificate;
using Microsoft.AspNetCore.Builder;
Expand Down Expand Up @@ -43,6 +44,7 @@
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

const string SQLServerCreateTableFormat = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='DistributedCache' and xtype='U') " +
"CREATE TABLE [dbo].[DistributedCache] (" +
Expand Down Expand Up @@ -231,6 +233,13 @@ void ConfigureIdServer(IServiceCollection services)
{
o.IsFederationEnabled = true;
});
services.AddDbContext<CustomDataContext>(a =>
{
a.UseSqlServer("Data Source=.;Initial Catalog=IdServer;Integrated Security=True;TrustServerCertificate=True", o =>
{
o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery);
});
}, ServiceLifetime.Scoped);
var isRealmEnabled = identityServerConfiguration.IsRealmEnabled;
if (isRealmEnabled) idServerBuilder.UseRealm();
services.AddDidKey();
Expand Down Expand Up @@ -392,6 +401,7 @@ async void SeedData(WebApplication application, string scimBaseUrl)
{
using (var dbContext = scope.ServiceProvider.GetService<StoreDbContext>())
{
Test(scope.ServiceProvider.GetRequiredService<ICasbinPolicyRepository>());
var isInMemory = dbContext.Database.IsInMemory();
if (!isInMemory) dbContext.Database.Migrate();

Expand Down Expand Up @@ -790,4 +800,12 @@ void MigrateUsers(StoreDbContext dbContext, Group adminGroup, Group adminRoGroup
}
}
}
}

void Test(ICasbinPolicyRepository repository)
{
var r = repository.GetAll("casbin", CancellationToken.None).Result;
r.First().PolicyName = "coucou";
repository.SaveChanges("casbin", CancellationToken.None).Wait();
string ss = "";
}
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));
}
}
}
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);
}
}
}
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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private static void RegisterDepedencies(IServiceCollection services)
services.AddTransient<IMessageBusErrorStore, MessageBusErrorStore>();
services.AddTransient<ITransactionBuilder, EFTransactionBuilder>();
services.AddTransient<IFederationEntityStore, FederationEntityStore>();
services.AddTransient<ICasbinPolicyRepository, CasbinPolicyRepository>();
}
}
}

0 comments on commit 68e538c

Please sign in to comment.