using System; using System.Linq; using System.Threading; namespace EgtBEAMWALL.DataLayer.Controllers { public class DbController : IDisposable { #region Public Fields /// /// Singleton gestione /// 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 /// /// Verifica necessità creazione utente (locale o in rete) /// /// /// /// /// 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; } /// /// Migrazione esplicita del DB da parte dell'admin controller /// /// public bool checkMigrateDb() { return adbCtx.SetUpDbConnectionAndDbConfig(); } public void Dispose() { // Clear database context adbCtx.Dispose(); } /// /// ATTENZIONE!!!! Procedura di Reset del DB /// /// public bool ResetDb() { bool answ = false; try { adbCtx .Database .SqlQuery("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; /// /// Istanza logger /// private NLog.Logger Log; #endregion Private Fields } }