Files
egtbeamwall/EgtBEAMWALL.DataLayer/Controllers/DbController.cs
T
Samuele Locatelli 0945aaf9d0 Fix procedura di LOG
- aggiunta file NLog.config
- aggiorna gestione scrittura dir
- aggiunto log caso x caso su file
2022-06-27 15:51:34 +02:00

114 lines
3.6 KiB
C#

using System;
using System.Linq;
using System.Threading;
namespace EgtBEAMWALL.DataLayer.Controllers
{
public class DbController : IDisposable
{
#region Public Fields
/// <summary>
/// Singleton gestione
/// </summary>
public static DbController man = new DbController();
#endregion Public Fields
#region Public Constructors
public DbController()
{
// Initialize database context for ADMIN
adbCtx = new AdminContext(DbConfig.ADMIN_CONNECTION_STRING);
}
#endregion Public Constructors
#region Public Methods
/// <summary>
/// Verifica necessità creazione utente (locale o in rete)
/// </summary>
/// <param name="username"></param>
/// <param name="pwd"></param>
/// <param name="isNetwork"></param>
/// <returns></returns>
public bool checkCreateUser(string username, string pwd, bool isNetwork)
{
bool answ = false;
string domain = isNetwork ? "%" : "localhost";
// 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;";
//string sqlCommand = $"CREATE USER '{username}'@'{domain}' IDENTIFIED BY '{pwd}'; GRANT ALL ON *.* TO '{username}'@'localhost'; FLUSH PRIVILEGES;";
//string sqlCommand = $"CREATE USER '{username}'@'{domain}' IDENTIFIED BY '{pwd}'; GRANT ALL ON *.* TO '{username}'@'localhost'; FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
sqlCommand = $"CREATE USER '{username}'@'{domain}' IDENTIFIED BY '{pwd}';";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'{domain}';";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
sqlCommand = "FLUSH PRIVILEGES;";
adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand);
Thread.Sleep(100);
}
return answ;
}
public void Dispose()
{
// Clear database context
adbCtx.Dispose();
}
public bool ResetDb()
{
bool answ = false;
try
{
adbCtx
.Database
.SqlQuery<int>("CALL stp_ResetDb()");
answ = true;
adbCtx.SaveChanges();
}
catch (Exception exc)
{
string errMessage = $"EXCEPTION on DbController.ResetDb: {Environment.NewLine}{exc}";
Console.WriteLine(errMessage);
Log.Error(errMessage);
}
return answ;
}
#endregion Public Methods
#region Private Fields
private AdminContext adbCtx;
/// <summary>
/// Istanza logger
/// </summary>
private NLog.Logger Log;
#endregion Private Fields
}
}