Files
2022-03-17 12:32:14 +01:00

87 lines
2.3 KiB
C#

using Microsoft.EntityFrameworkCore;
using NLog;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace MP.MONO.Data
{
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)
{
bool answ = false;
using (AdminContext adbCtx = new AdminContext())
{
// ricerca utente...
var 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}'@'%' IDENTIFIED BY '{pwd}';";
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'%';";
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
sqlCommand = "FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
}
}
return answ;
}
public static async Task<bool> migrateDbIdentity()
{
bool answ = false;
using (UserIdentityDbContext dbCtx = new UserIdentityDbContext())
{
await dbCtx.Database.MigrateAsync();
answ = true;
}
return answ;
}
public static async Task<bool> migrateDbMain()
{
bool answ = false;
using (MapoMonoContext dbCtx = new MapoMonoContext())
{
await dbCtx.Database.MigrateAsync();
answ = true;
}
return answ;
}
public void Dispose()
{
}
#endregion Public Methods
}
}