124 lines
3.8 KiB
C#
124 lines
3.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using NLog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagMan.Data.Admin
|
|
{
|
|
/// <summary>
|
|
/// Classe amministrazione Db principale:
|
|
/// - creazione utenti sul Server MySql
|
|
/// - gestione DB/Tabele x identity & gestione multi-tenant
|
|
/// </summary>
|
|
public class DbAdmin : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public DbAdmin()
|
|
{
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
public static bool CheckCreateUser(string username, string pwd, string dbName)
|
|
{
|
|
bool answ = false;
|
|
int numUser = 0;
|
|
using (ServerAdminContext adbCtx = new ServerAdminContext())
|
|
{
|
|
// ricerca utente...
|
|
numUser = adbCtx
|
|
.UserList
|
|
.Where(x => x.User == username)
|
|
.ToList()
|
|
.Count;
|
|
if (numUser > 0)
|
|
{
|
|
answ = true;
|
|
}
|
|
if (!answ)
|
|
{
|
|
// creo utente
|
|
string sqlCommand = "FLUSH PRIVILEGES;";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
sqlCommand = $"CREATE USER '{username}'@'localhost' IDENTIFIED BY '{pwd}';";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
sqlCommand = $"GRANT ALL PRIVILEGES ON {dbName}.* TO '{username}'@'localhost';";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
sqlCommand = "FLUSH PRIVILEGES;";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public static bool CheckCreateCustDb(int nKeyMain)
|
|
{
|
|
bool answ = false;
|
|
string dbName = $"MagMan_{nKeyMain:000000}";
|
|
using (ServerAdminContext adbCtx = new ServerAdminContext())
|
|
{
|
|
// ricerca DB...
|
|
string sqlCommand = $"SHOW DATABASES;";
|
|
var searchDb = adbCtx
|
|
.DbList
|
|
.FromSqlRaw(sqlCommand)
|
|
.ToList();
|
|
// se trovato qualcosa procedo
|
|
if (searchDb != null)
|
|
{
|
|
var rigaDb = searchDb.Where(x => x.Database.Contains(dbName, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
|
|
answ = rigaDb != null;
|
|
}
|
|
// altrimenti crea
|
|
if (!answ)
|
|
{
|
|
// creo DB
|
|
sqlCommand = $"CREATE DATABASE IF NOT EXISTS {dbName};";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public static async Task<bool> MigrateDbIdentity()
|
|
{
|
|
bool answ = false;
|
|
using (IdentityContext dbCtx = new IdentityContext())
|
|
{
|
|
await dbCtx.Database.MigrateAsync();
|
|
answ = true;
|
|
}
|
|
return answ;
|
|
}
|
|
public static async Task<bool> MigrateDbMultiTenant()
|
|
{
|
|
bool answ = false;
|
|
using (MultiTenantContext dbCtx = new MultiTenantContext())
|
|
{
|
|
await dbCtx.Database.MigrateAsync();
|
|
answ = true;
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
}
|