98 lines
2.7 KiB
C#
98 lines
2.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using NLog;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GWMS.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}'@'localhost' IDENTIFIED BY '{pwd}';";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'localhost';";
|
|
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 (GWMSContext dbCtx = new GWMSContext())
|
|
{
|
|
await dbCtx.Database.MigrateAsync();
|
|
answ = true;
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public static bool resetPlantLogTable()
|
|
{
|
|
bool answ = false;
|
|
using (GWMSContext dbCtx = new GWMSContext())
|
|
{
|
|
string sqlCommand = "TRUNCATE TABLE PlantLog;";
|
|
dbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
answ = true;
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |