Fix dominio parametrico x creazione utente in rete

This commit is contained in:
Samuele Locatelli
2022-03-22 11:54:22 +01:00
parent e01104dba3
commit ca2828958d
2 changed files with 82 additions and 74 deletions
@@ -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
/// <summary>
/// Singleton gestione
/// </summary>
public static DbController man = new DbController();
/// <summary>
/// Singleton gestione
/// </summary>
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;
}
/// <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);
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<int>("CALL stp_ResetDb()");
answ = true;
try
{
adbCtx
.Database
.SqlQuery<int>("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
}
}
+2 -2
View File
@@ -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