PlanTempusApp/Database/IdentitySystem/Setup.cs

147 lines
5.2 KiB
C#
Raw Normal View History

using Database.Common;
using Insight.Database;
2025-01-28 17:14:35 +01:00
using System;
2025-01-28 14:51:09 +01:00
using System.Data;
using System.Threading.Tasks;
namespace Database.IdentitySystem
2025-01-28 14:51:09 +01:00
{
public class DbSetup
{
readonly IDbConnection _db;
IDbTransaction _transaction = null;
string _schema;
2025-01-28 14:51:09 +01:00
public DbSetup(IDbConnection db)
{
_db = db ?? throw new ArgumentNullException(nameof(db));
}
/// <summary>
/// Creates the system tables in the specified schema within a transaction.
/// </summary>
/// <param name="schema">The schema name where the tables will be created.</param>
public async Task CreateSystem(string schema)
{
_schema = schema;
if (!Validations.IsValidSchemaName(schema))
throw new ArgumentException("Invalid schema name", nameof(schema));
using (_transaction = _db.BeginTransaction())
{
try
{
await CreateUsersTable().ConfigureAwait(false);
await CreateTenantsTable().ConfigureAwait(false);
await CreateUserTenantsTable().ConfigureAwait(false);
await SetupRLS().ConfigureAwait(false);
_transaction.Commit();
}
catch (Exception ex)
{
_transaction.Rollback();
throw new InvalidOperationException("Failed to create system tables.", ex);
}
}
}
private async Task ExecuteSqlAsync(string sql)
{
if (string.IsNullOrEmpty(sql))
throw new ArgumentNullException(nameof(sql));
await _db.ExecuteAsync(sql).ConfigureAwait(false);
}
2025-01-28 14:51:09 +01:00
/// <summary>
/// Creates the users table in the ptmain schema.
/// </summary>
public async Task CreateUsersTable()
2025-01-28 14:51:09 +01:00
{
var sql = @"
2025-01-28 14:51:09 +01:00
CREATE TABLE IF NOT EXISTS ptmain.users (
id SERIAL PRIMARY KEY,
email VARCHAR(256) NOT NULL UNIQUE,
password_hash VARCHAR(256) NOT NULL,
security_stamp VARCHAR(36) NOT NULL,
email_confirmed BOOLEAN NOT NULL DEFAULT FALSE,
access_failed_count INTEGER NOT NULL DEFAULT 0,
lockout_enabled BOOLEAN NOT NULL DEFAULT TRUE,
lockout_end TIMESTAMPTZ NULL,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
last_login_at TIMESTAMPTZ NULL
);";
await ExecuteSqlAsync(sql).ConfigureAwait(false);
2025-01-28 14:51:09 +01:00
}
/// <summary>
/// Creates the tenants table in the ptmain schema.
/// </summary>
public async Task CreateTenantsTable()
2025-01-28 14:51:09 +01:00
{
var sql = @"
2025-01-28 14:51:09 +01:00
CREATE TABLE IF NOT EXISTS ptmain.tenants (
id SERIAL PRIMARY KEY,
connection_string VARCHAR(500) NOT NULL,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_by INTEGER NOT NULL REFERENCES ptmain.users(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
);";
await ExecuteSqlAsync(sql).ConfigureAwait(false);
2025-01-28 14:51:09 +01:00
}
/// <summary>
/// Creates the user_tenants table in the ptmain schema.
/// </summary>
public async Task CreateUserTenantsTable()
2025-01-28 14:51:09 +01:00
{
var sql = @"
2025-01-28 14:51:09 +01:00
CREATE TABLE IF NOT EXISTS ptmain.user_tenants (
user_id INTEGER NOT NULL REFERENCES ptmain.users(id),
tenant_id INTEGER NOT NULL REFERENCES ptmain.tenants(id),
pin_code VARCHAR(10) NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, tenant_id)
);";
await ExecuteSqlAsync(sql).ConfigureAwait(false);
2025-01-28 14:51:09 +01:00
}
/// <summary>
/// Sets up Row Level Security (RLS) for the tenants and user_tenants tables.
2025-01-28 14:51:09 +01:00
/// </summary>
public async Task SetupRLS()
2025-01-28 14:51:09 +01:00
{
var sql = new[]
2025-01-28 14:51:09 +01:00
{
"ALTER TABLE ptmain.tenants ENABLE ROW LEVEL SECURITY;",
"ALTER TABLE ptmain.user_tenants ENABLE ROW LEVEL SECURITY;",
"DROP POLICY IF EXISTS tenant_access ON ptmain.tenants;",
@"CREATE POLICY tenant_access ON ptmain.tenants
USING (id IN (
SELECT tenant_id
FROM ptmain.user_tenants
WHERE user_id = current_setting('app.user_id', TRUE)::INTEGER
));",
"DROP POLICY IF EXISTS user_tenant_access ON ptmain.user_tenants;",
@"CREATE POLICY user_tenant_access ON ptmain.user_tenants
USING (user_id = current_setting('app.user_id', TRUE)::INTEGER);"
};
foreach (var statement in sql)
2025-01-28 14:51:09 +01:00
{
await ExecuteSqlAsync(statement).ConfigureAwait(false);
2025-01-28 14:51:09 +01:00
}
}
2025-01-28 14:51:09 +01:00
}
}