[성현모] DBProvider추가. LoginContext 변경

This commit is contained in:
SHM
2025-07-17 11:54:42 +09:00
parent 7a12be392a
commit 3c5f27ce68
22 changed files with 265 additions and 323 deletions

View File

@ -0,0 +1,66 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using SystemX.Core.Config;
using SystemX.Core.Services;
namespace SystemX.Core.Controller
{
public class CommonController<T> : ControllerBase where T : WebCommonConfig
{
public readonly IServiceProvider _serviceProvider;
public readonly IHttpContextAccessor _httpContextAccessor;
public readonly ConfigService<T>? _configService;
protected static Guid guid { get; private set; } = Guid.NewGuid();
public CommonController(IServiceProvider serviceProvider, IHttpContextAccessor httpContextAccessor)
{
//provider
_serviceProvider = serviceProvider;
_httpContextAccessor = httpContextAccessor;
//service
_configService = _serviceProvider.GetRequiredService<ConfigService<T>>();
}
/// <summary>
/// Request 클라이언트 IP
/// </summary>
protected virtual string? GetClientIP()
{
return _httpContextAccessor?.HttpContext?.Connection?.RemoteIpAddress?.ToString();
}
/// <summary>
/// Request 클라이언트 Url
/// </summary>
protected virtual string? GetRequestUrl()
{
return _httpContextAccessor?.HttpContext?.Request?.Path;
}
/// <summary>
/// Request 클라이언트 method: [GET] or [POST]
/// </summary>
protected virtual string? GetRequestMethod()
{
return _httpContextAccessor?.HttpContext?.Request?.Method;
}
/// <summary>
/// 현재 Action(함수) 이름 가져오기
/// </summary>
protected virtual string GetMethodName([CallerMemberName] string callerMemberName = "")
{
return $"{callerMemberName}()";
}
}
}

View File

@ -1,84 +0,0 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using SystemX.Core.DB.DBContext.AccountDB.Tables;
namespace SystemX.Core.DB.DBContext.AccountDB.Context;
public partial class AccountDbContext : DbContext
{
public AccountDbContext()
{
}
public AccountDbContext(DbContextOptions<AccountDbContext> options)
: base(options)
{
}
public virtual DbSet<TRefreshToken> TRefreshTokens { get; set; }
public virtual DbSet<TRole> TRoles { get; set; }
public virtual DbSet<TUser> TUsers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see https://go.microsoft.com/fwlink/?LinkId=723263.
=> optionsBuilder.UseSqlServer("server=127.0.0.1; user id=SystemX; password=X; database=AccountDB; TrustServerCertificate=true;");
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<TRefreshToken>(entity =>
{
entity.HasKey(e => e.CAuid).HasName("PK__tRefresh__FBF0855465EB95AB");
entity.ToTable("tRefreshToken");
entity.Property(e => e.CAuid)
.HasMaxLength(250)
.HasColumnName("cAuid");
entity.Property(e => e.CRefreshToken)
.HasMaxLength(1000)
.HasColumnName("cRefreshToken");
});
modelBuilder.Entity<TRole>(entity =>
{
entity.HasKey(e => e.CAuid).HasName("PK__tRole__FBF085540BB887D7");
entity.ToTable("tRole");
entity.Property(e => e.CAuid)
.HasMaxLength(250)
.HasColumnName("cAuid");
entity.Property(e => e.CRoleId).HasColumnName("cRoleID");
entity.Property(e => e.CRoleName)
.HasMaxLength(20)
.HasColumnName("cRoleName");
});
modelBuilder.Entity<TUser>(entity =>
{
entity.HasKey(e => e.CUserId).HasName("PK__tUser__A75DC19A721265FF");
entity.ToTable("tUser");
entity.Property(e => e.CUserId)
.HasMaxLength(50)
.HasColumnName("cUserID");
entity.Property(e => e.CAuid)
.HasMaxLength(250)
.HasColumnName("cAuid");
entity.Property(e => e.CCreateDateTime).HasColumnName("cCreateDateTime");
entity.Property(e => e.CLastLoginDateTime).HasColumnName("cLastLoginDateTime");
entity.Property(e => e.CPasswordHashed)
.HasMaxLength(250)
.HasColumnName("cPasswordHashed");
entity.Property(e => e.CState).HasColumnName("cState");
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

View File

@ -1,11 +0,0 @@
using System;
using System.Collections.Generic;
namespace SystemX.Core.DB.DBContext.AccountDB.Tables;
public partial class TRefreshToken
{
public string CAuid { get; set; } = null!;
public string CRefreshToken { get; set; } = null!;
}

View File

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
namespace SystemX.Core.DB.DBContext.AccountDB.Tables;
public partial class TRole
{
public string CAuid { get; set; } = null!;
public byte CRoleId { get; set; }
public string CRoleName { get; set; } = null!;
}

View File

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
namespace SystemX.Core.DB.DBContext.AccountDB.Tables;
public partial class TUser
{
public string CUserId { get; set; } = null!;
public string CAuid { get; set; } = null!;
public string CPasswordHashed { get; set; } = null!;
public byte CState { get; set; }
public DateTime CCreateDateTime { get; set; }
public DateTime? CLastLoginDateTime { get; set; }
}

View File

@ -363,7 +363,7 @@ public static class Log4net
</mapping>
<mapping>
<level value=""CONTROLLER"" />
<foreColor value=""DarkGreen"" />
<foreColor value=""Magenta"" />
</mapping>
</appender>

View File

@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace SystemX.Core.Model.Auth
{
//로그인 요청 모델
public class LoginModel
public class Login
{
[Required]
public string? UserID { get; set; }
@ -19,7 +19,7 @@ namespace SystemX.Core.Model.Auth
}
//로그인 응답 모델
public class LoginResponseModel
public class LoginResponse
{
public string? UserID { get; set; }
public UserRole Role { get; set; }

View File

@ -8,13 +8,13 @@ using System.Threading.Tasks;
namespace SystemX.Core.Model.Auth
{
public class LogoutModel
public class Logout
{
[Required]
public string? UserID { get; set; }
}
public class LogoutResponseModel
public class LogoutResponse
{
public string? UserID { get; set; }

View File

@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace SystemX.Core.Model.Auth
{
//유저 등록 모델
public class RegisterModel
public class Register
{
[Required]
public string UserID { get; set; } = string.Empty;
@ -24,7 +24,7 @@ namespace SystemX.Core.Model.Auth
public UserRole Role { get; set; } = UserRole.User;
}
public class RegisterResponseModel
public class RegisterResponse
{
public string? UserID { get; set; }
public UserRole Role { get; set; }

View File

@ -1,14 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemX.Core.Model.Auth
{
public class UserModel
{
//public TUser? TUser { get; set; }
//public TRole? TRole { get; set; }
}
}

View File

@ -0,0 +1,45 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemX.Core.Config.Model;
namespace SystemX.Core.Services
{
public class DbContextProvider
{
private Dictionary<string, DataBase> DBDictionary = new Dictionary<string, DataBase>();
public void SetDBList(IList<DataBase>? DBList)
{
if (DBList?.Count > 0)
{
foreach (var db in DBList)
{
if (DBDictionary.ContainsKey($"{db.DBName}") == false)
DBDictionary.Add($"{db.DBName}", db);
else
Log4net.WriteLine($"Exist key DB Dictionary {db.DBName}", LogType.Error);
}
}
}
// 또는 context 직접 반환 (주의: caller가 Dispose 해야 함)
public TContext? GetDBContext<TContext>(string dbName) where TContext : DbContext
{
if(DBDictionary.TryGetValue(dbName, out var db) == true)
{
var optionsBuilder = new DbContextOptionsBuilder<TContext>();
optionsBuilder.UseSqlServer(db.ConvertToConnectionString());
return (TContext)Activator.CreateInstance(typeof(TContext), optionsBuilder.Options);
}
return null;
}
}
}

View File

@ -20,6 +20,7 @@
<ItemGroup>
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="log4net" Version="3.0.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.3.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@ -12,7 +12,7 @@ public static class DBUtils
/// </summary>
public static string ConvertToConnectionString(this DataBase dbConfig)
{
return $"server={dbConfig.IP},{dbConfig.Port}; user id={dbConfig.UserID}; password={dbConfig.Password}; database={dbConfig.DBName};";
return $"server={dbConfig.IP},{dbConfig.Port}; user id={dbConfig.UserID}; password={dbConfig.Password}; database={dbConfig.DBName}; TrustServerCertificate=True;";
}
}