2025-02-10 18:41:51 +01:00
|
|
|
|
using System.Data;
|
|
|
|
|
|
using Database.Common;
|
|
|
|
|
|
using Insight.Database;
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
namespace Database.Core.DCL
|
2025-02-10 18:41:51 +01:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Only a superadmin or similar can create Application Users
|
|
|
|
|
|
/// </summary>
|
2025-02-11 17:07:01 +01:00
|
|
|
|
public class SetupApplicationUser : IDbConfigure<SetupApplicationUser.Command>
|
2025-02-10 18:41:51 +01:00
|
|
|
|
{
|
2025-02-11 17:07:01 +01:00
|
|
|
|
public class Command
|
|
|
|
|
|
{
|
|
|
|
|
|
public required string Schema { get; init; }
|
|
|
|
|
|
public required string User { get; init; }
|
|
|
|
|
|
public required string Password { get; init; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-10 18:41:51 +01:00
|
|
|
|
|
|
|
|
|
|
IDbConnection _db;
|
2025-02-11 17:07:01 +01:00
|
|
|
|
Command _command;
|
2025-02-10 18:41:51 +01:00
|
|
|
|
|
|
|
|
|
|
public SetupApplicationUser(IDbConnection db)
|
|
|
|
|
|
{
|
|
|
|
|
|
_db = db;
|
|
|
|
|
|
}
|
2025-02-12 17:38:01 +01:00
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
public void Setup(string schema = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
public void With(Command command)
|
2025-02-10 18:41:51 +01:00
|
|
|
|
{
|
2025-02-11 17:07:01 +01:00
|
|
|
|
_command = command;
|
2025-02-10 18:41:51 +01:00
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
if (!Validations.IsValidSchemaName(_command.Schema))
|
|
|
|
|
|
throw new ArgumentException("Invalid schema name", _command.Schema);
|
2025-02-10 18:41:51 +01:00
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
using (var transaction = _db.OpenWithTransaction())
|
2025-02-10 18:41:51 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateSchema();
|
|
|
|
|
|
CreateRole();
|
|
|
|
|
|
GrantSchemaRights();
|
|
|
|
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
|
throw new InvalidOperationException("Failed to SetupApplicationUser in Database", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private void ExecuteSql(string sql)
|
|
|
|
|
|
{
|
|
|
|
|
|
_db.ExecuteSql(sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CreateSchema()
|
|
|
|
|
|
{
|
2025-02-11 17:07:01 +01:00
|
|
|
|
var sql = $"CREATE SCHEMA IF NOT EXISTS {_command.Schema}";
|
2025-02-10 18:41:51 +01:00
|
|
|
|
ExecuteSql(sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void CreateRole()
|
|
|
|
|
|
{
|
2025-02-11 17:07:01 +01:00
|
|
|
|
var sql = $@"
|
|
|
|
|
|
DO $$
|
|
|
|
|
|
BEGIN
|
|
|
|
|
|
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '{_command.User}') THEN
|
|
|
|
|
|
CREATE ROLE {_command.User} WITH CREATEDB CREATEROLE LOGIN PASSWORD '{_command.Password}';
|
|
|
|
|
|
END IF;
|
|
|
|
|
|
END $$;";
|
2025-02-10 18:41:51 +01:00
|
|
|
|
ExecuteSql(sql);
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
var sql1 = $"ALTER ROLE {_command.User} SET search_path='{_command.Schema}';";
|
2025-02-10 18:41:51 +01:00
|
|
|
|
ExecuteSql(sql1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void GrantSchemaRights()
|
|
|
|
|
|
{
|
2025-02-11 17:07:01 +01:00
|
|
|
|
// Grant USAGE og alle CREATE rettigheder på schema niveau
|
|
|
|
|
|
var sql = $@"
|
|
|
|
|
|
GRANT USAGE ON SCHEMA {_command.Schema} TO {_command.User};
|
|
|
|
|
|
GRANT ALL ON SCHEMA {_command.Schema} TO {_command.User};";
|
2025-02-10 18:41:51 +01:00
|
|
|
|
ExecuteSql(sql);
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
// Grant rettigheder på eksisterende og fremtidige tabeller
|
|
|
|
|
|
var sql1 = $"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA {_command.Schema} TO {_command.User};";
|
2025-02-10 18:41:51 +01:00
|
|
|
|
ExecuteSql(sql1);
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
var sql2 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT ALL PRIVILEGES ON TABLES TO {_command.User};";
|
2025-02-10 18:41:51 +01:00
|
|
|
|
ExecuteSql(sql2);
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
// Grant sequence rettigheder
|
|
|
|
|
|
var sql3 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {_command.Schema} TO {_command.User};";
|
|
|
|
|
|
ExecuteSql(sql3);
|
|
|
|
|
|
|
|
|
|
|
|
// Grant execute på functions
|
|
|
|
|
|
var sql4 = $"GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA {_command.Schema} TO {_command.User};";
|
|
|
|
|
|
ExecuteSql(sql4);
|
|
|
|
|
|
|
|
|
|
|
|
// Grant for fremtidige functions
|
|
|
|
|
|
var sql5 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT EXECUTE ON FUNCTIONS TO {_command.User};";
|
|
|
|
|
|
ExecuteSql(sql5);
|
|
|
|
|
|
|
|
|
|
|
|
// Grant for fremtidige sequences
|
|
|
|
|
|
var sql6 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} GRANT USAGE ON SEQUENCES TO {_command.User};";
|
|
|
|
|
|
ExecuteSql(sql6);
|
2025-02-10 18:41:51 +01:00
|
|
|
|
}
|
2025-02-11 17:07:01 +01:00
|
|
|
|
|
2025-02-10 18:41:51 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|