75 lines
1.9 KiB
C#
75 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace GWMS.Data
|
|
{
|
|
public class DbAdmin : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private AdminContext adbCtx;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Fields
|
|
|
|
/// <summary>
|
|
/// Singleton gestione
|
|
/// </summary>
|
|
public static DbAdmin man = new DbAdmin();
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public DbAdmin()
|
|
{
|
|
// Initialize database context for ADMIN
|
|
adbCtx = new AdminContext();
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#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;";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
sqlCommand = $"CREATE USER '{username}'@'localhost' IDENTIFIED BY '{pwd}';";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'localhost';";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
sqlCommand = "FLUSH PRIVILEGES;";
|
|
adbCtx.Database.ExecuteSqlRaw(sqlCommand);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database context
|
|
adbCtx.Dispose();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |