2025-02-10 18:41:51 +01:00
|
|
|
|
using System.Data;
|
|
|
|
|
|
using Insight.Database;
|
2025-03-10 15:56:22 +01:00
|
|
|
|
using PlanTempus.Core.Database.ConnectionFactory;
|
2025-02-20 00:23:13 +01:00
|
|
|
|
using PlanTempus.Database.Common;
|
|
|
|
|
|
using PlanTempus.Database.Core;
|
2025-02-10 18:41:51 +01:00
|
|
|
|
|
2025-02-20 00:23:13 +01:00
|
|
|
|
namespace PlanTempus.Database.Core.DCL
|
2025-02-10 18:41:51 +01:00
|
|
|
|
{
|
2025-03-03 17:40:16 +01:00
|
|
|
|
public class SetupOrganization : IDbConfigure<SetupOrganization.Command>
|
2025-02-20 00:23:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
public class Command
|
|
|
|
|
|
{
|
|
|
|
|
|
public required string Schema { get; init; }
|
|
|
|
|
|
public required string User { get; init; }
|
|
|
|
|
|
public required string Password { get; init; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Command _command;
|
2025-02-21 23:34:06 +01:00
|
|
|
|
private readonly IDbConnectionFactory _connectionFactory;
|
2025-02-20 00:23:13 +01:00
|
|
|
|
|
2025-02-21 23:34:06 +01:00
|
|
|
|
|
|
|
|
|
|
public SetupOrganization(IDbConnectionFactory connectionFactory)
|
2025-02-20 00:23:13 +01:00
|
|
|
|
{
|
2025-02-21 23:34:06 +01:00
|
|
|
|
_connectionFactory = connectionFactory;
|
2025-02-20 00:23:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-21 23:34:06 +01:00
|
|
|
|
public void With(Command command, ConnectionStringParameters parameters = null)
|
2025-02-20 00:23:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
_command = command;
|
|
|
|
|
|
|
|
|
|
|
|
if (!Validations.IsValidSchemaName(_command.Schema))
|
|
|
|
|
|
throw new ArgumentException("Invalid schema name", _command.Schema);
|
|
|
|
|
|
|
2025-02-21 23:34:06 +01:00
|
|
|
|
using var conn = parameters is null ? _connectionFactory.Create() : _connectionFactory.Create(parameters);
|
|
|
|
|
|
using var transaction = conn.OpenWithTransaction();
|
|
|
|
|
|
try
|
2025-02-20 00:23:13 +01:00
|
|
|
|
{
|
2025-02-21 23:34:06 +01:00
|
|
|
|
CreateSchema(conn);
|
|
|
|
|
|
CreateRole(conn);
|
|
|
|
|
|
GrantSchemaRights(conn);
|
|
|
|
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
|
throw new InvalidOperationException("Failed to SetupOrganization in Database", ex);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-02-21 23:34:06 +01:00
|
|
|
|
|
|
|
|
|
|
private void CreateSchema(IDbConnection db)
|
2025-02-20 00:23:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sql = $"CREATE SCHEMA IF NOT EXISTS {_command.Schema}";
|
2025-02-21 23:34:06 +01:00
|
|
|
|
db.ExecuteSql(sql);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-21 23:34:06 +01:00
|
|
|
|
private void CreateRole(IDbConnection db)
|
2025-02-20 00:23:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sql = $"CREATE ROLE {_command.User} LOGIN PASSWORD '{_command.Password}';";
|
2025-02-21 23:34:06 +01:00
|
|
|
|
db.ExecuteSql(sql);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
|
|
|
|
|
|
var sql1 = $"ALTER ROLE {_command.User} SET search_path='{_command.Schema}';";
|
2025-02-21 23:34:06 +01:00
|
|
|
|
db.ExecuteSql(sql1);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-02-21 23:34:06 +01:00
|
|
|
|
private void GrantSchemaRights(IDbConnection db)
|
2025-02-20 00:23:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sql = $"GRANT USAGE ON SCHEMA {_command.Schema} TO {_command.User};";
|
2025-02-21 23:34:06 +01:00
|
|
|
|
db.ExecuteSql(sql);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
|
|
|
|
|
|
var sql1 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {_command.Schema} " +
|
|
|
|
|
|
$"GRANT INSERT, SELECT, UPDATE PRIVILEGES ON TABLES TO {_command.User};";
|
2025-02-21 23:34:06 +01:00
|
|
|
|
db.ExecuteSql(sql1);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
|
|
|
|
|
|
var sql2 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {_command.Schema} TO {_command.User};";
|
2025-02-21 23:34:06 +01:00
|
|
|
|
db.ExecuteSql(sql2);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
|
|
|
|
|
|
var sql3 = $"GRANT CREATE TABLE ON SCHEMA {_command.Schema} TO {_command.User};";
|
2025-02-21 23:34:06 +01:00
|
|
|
|
db.ExecuteSql(sql3);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
}
|
2025-02-21 23:34:06 +01:00
|
|
|
|
public void RevokeCreateTable(IDbConnection db)
|
2025-02-20 00:23:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sql = $"REVOKE CREATE TABLE ON SCHEMA {_command.Schema} FROM {_command.User};";
|
2025-02-21 23:34:06 +01:00
|
|
|
|
db.ExecuteSql(sql);
|
2025-02-20 00:23:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-10 18:41:51 +01:00
|
|
|
|
}
|