diff --git a/EgtBEAMWALL.DataLayer/Controllers/DbController.cs b/EgtBEAMWALL.DataLayer/Controllers/DbController.cs index e5861783..8f47e261 100644 --- a/EgtBEAMWALL.DataLayer/Controllers/DbController.cs +++ b/EgtBEAMWALL.DataLayer/Controllers/DbController.cs @@ -6,83 +6,92 @@ using EgtBEAMWALL.DataLayer.DatabaseModels; namespace EgtBEAMWALL.DataLayer.Controllers { - public class DbController : IDisposable - { - #region Private Fields + public class DbController : IDisposable + { + #region Private Fields - private AdminContext adbCtx; + private AdminContext adbCtx; - #endregion Private Fields + #endregion Private Fields - #region Public Fields + #region Public Fields - /// - /// Singleton gestione - /// - public static DbController man = new DbController(); + /// + /// Singleton gestione + /// + public static DbController man = new DbController(); - #endregion Public Fields + #endregion Public Fields - #region Public Constructors + #region Public Constructors - public DbController() - { - // Initialize database context for ADMIN - adbCtx = new AdminContext(DbConfig.ADMIN_CONNECTION_STRING); - } + public DbController() + { + // Initialize database context for ADMIN + adbCtx = new AdminContext(DbConfig.ADMIN_CONNECTION_STRING); + } - #endregion Public Constructors + #endregion Public Constructors - #region Public Methods + #region Public Methods - public bool checkCreateUser(string username, string pwd) - { - bool answ = false; - // ricerca utente... - var foundUser = adbCtx - .UserList - .Where(x => x.User == username) - .ToList(); - if (foundUser != null && foundUser.Count > 0) - { - answ = true; - } - if (!answ) - { - // creo utente - string sqlCommand = $"ALTER USER '{username}'@'localhost' IDENTIFIED BY '{pwd}'; FLUSH PRIVILEGES;"; - adbCtx.Database.ExecuteSqlCommand(sqlCommand); - } - return answ; - } + public bool checkCreateUser(string username, string pwd) + { + bool answ = false; + // 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}'@'localhost' IDENTIFIED BY '{pwd}'; GRANT ALL ON *.* TO '{username}'@'localhost'; FLUSH PRIVILEGES;"; + //string sqlCommand = $"CREATE USER '{username}'@'localhost' IDENTIFIED BY '{pwd}'; GRANT ALL ON *.* TO '{username}'@'localhost'; FLUSH PRIVILEGES;"; + adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand); + sqlCommand = $"CREATE USER '{username}'@'localhost' IDENTIFIED BY '{pwd}';"; + adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand); + sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'localhost';"; + adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand); + sqlCommand = "FLUSH PRIVILEGES;"; + adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand); + } + return answ; + } - public void Dispose() - { - // Clear database context - adbCtx.Dispose(); - } + public void Dispose() + { + // Clear database context + adbCtx.Dispose(); + } - public bool ResetDb() - { - bool answ = false; + public bool ResetDb() + { + bool answ = false; - try - { - adbCtx - .Database - .SqlQuery("CALL stp_ResetDb()"); - answ = true; + try + { + adbCtx + .Database + .SqlQuery("CALL stp_ResetDb()"); + answ = true; - adbCtx.SaveChanges(); - } - catch (Exception exc) - { - Console.WriteLine($"EXCEPTION on ResetDb: {exc}"); - } + adbCtx.SaveChanges(); + } + catch (Exception exc) + { + Console.WriteLine($"EXCEPTION on ResetDb: {exc}"); + } - return answ; - } + return answ; + } - #endregion Public Methods - } + #endregion Public Methods + } } \ No newline at end of file diff --git a/EgtBEAMWALL.DataLayer/DatabaseModels/UserPrivModel.cs b/EgtBEAMWALL.DataLayer/DatabaseModels/UserPrivModel.cs index b844f9d4..3e96f3f1 100644 --- a/EgtBEAMWALL.DataLayer/DatabaseModels/UserPrivModel.cs +++ b/EgtBEAMWALL.DataLayer/DatabaseModels/UserPrivModel.cs @@ -15,10 +15,10 @@ namespace EgtBEAMWALL.DataLayer.DatabaseModels { #region Public Properties - [Column("Host")] + [Column("Host", Order = 1), Key] public string Host { get; set; } = ""; - [Column("User")] + [Column("User", Order = 2), Key] public string User { get; set; } = ""; #endregion Public Properties diff --git a/EgtBEAMWALL.DataLayer/DbConfig.cs b/EgtBEAMWALL.DataLayer/DbConfig.cs index 2bc4fbdf..ba6ebd88 100644 --- a/EgtBEAMWALL.DataLayer/DbConfig.cs +++ b/EgtBEAMWALL.DataLayer/DbConfig.cs @@ -46,10 +46,14 @@ namespace EgtBEAMWALL.DataLayer CONNECTION_STRING = $"server={DATABASE_SERV};port=3306;database={DATABASE_NAME};uid={DATABASE_USER};pwd={DATABASE_PWD};sslmode=None"; // stringa admin con utente root egalware... ADMIN_CONNECTION_STRING = $"server={DATABASE_SERV};port=3306;database=mysql;uid=root;pwd=Egalware_24068!;sslmode=None"; - // esecuzione script di install locale - bool userOk = Controllers.DbController.man.checkCreateUser(DATABASE_USER, DATABASE_PWD); } - #endregion Public Methods - } + public static bool CheckUser(string nKey, string sKey) + { + // esecuzione script di install locale + return Controllers.DbController.man.checkCreateUser(DATABASE_USER, DATABASE_PWD); + } + + #endregion Public Methods + } } \ No newline at end of file