From ca2828958d82d9268a6b98cdfe9f30f5409f4728 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 22 Mar 2022 11:54:22 +0100 Subject: [PATCH] Fix dominio parametrico x creazione utente in rete --- .../Controllers/DbController.cs | 152 +++++++++--------- EgtBEAMWALL.DataLayer/DbConfig.cs | 4 +- 2 files changed, 82 insertions(+), 74 deletions(-) diff --git a/EgtBEAMWALL.DataLayer/Controllers/DbController.cs b/EgtBEAMWALL.DataLayer/Controllers/DbController.cs index 8f47e261..b9334a82 100644 --- a/EgtBEAMWALL.DataLayer/Controllers/DbController.cs +++ b/EgtBEAMWALL.DataLayer/Controllers/DbController.cs @@ -6,92 +6,100 @@ 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 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; - } + /// + /// 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); + sqlCommand = $"CREATE USER '{username}'@'{domain}' IDENTIFIED BY '{pwd}';"; + adbCtx.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, sqlCommand); + sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'{domain}';"; + 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/DbConfig.cs b/EgtBEAMWALL.DataLayer/DbConfig.cs index 64da932e..200c75d2 100644 --- a/EgtBEAMWALL.DataLayer/DbConfig.cs +++ b/EgtBEAMWALL.DataLayer/DbConfig.cs @@ -76,10 +76,10 @@ namespace EgtBEAMWALL.DataLayer ADMIN_CONNECTION_STRING = $"server={DATABASE_SERV};port=3306;database=mysql;uid=root;pwd=Egalware_24068!;sslmode=None;CHARSET=utf8"; } - public static bool CheckUser(string nKey, string sKey) + public static bool CheckUser(string nKey, string sKey, bool isNetwork) { // esecuzione script di install locale - return Controllers.DbController.man.checkCreateUser(DATABASE_USER, DATABASE_PWD); + return Controllers.DbController.man.checkCreateUser(DATABASE_USER, DATABASE_PWD, isNetwork); } #endregion Public Methods