2025-02-06 16:58:13 +01:00
|
|
|
|
using Insight.Database;
|
|
|
|
|
|
using System.Data;
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
namespace Database.Core.DDL
|
2025-02-06 16:58:13 +01:00
|
|
|
|
{
|
2025-02-11 17:07:01 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// This is by purpose not async await
|
|
|
|
|
|
/// It is intended that this is created with the correct Application User, which is why the schema name is omitted.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class SetupIdentitySystem : IDbConfigure<SetupIdentitySystem.Command>
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Command { }
|
|
|
|
|
|
|
|
|
|
|
|
readonly IDbConnection _db;
|
|
|
|
|
|
IDbTransaction _transaction = null;
|
|
|
|
|
|
string _schema;
|
|
|
|
|
|
|
|
|
|
|
|
public SetupIdentitySystem(IDbConnection db)
|
|
|
|
|
|
{
|
|
|
|
|
|
_db = 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 void With(Command emptyByIntention)
|
|
|
|
|
|
{
|
2025-02-12 17:38:01 +01:00
|
|
|
|
using (_transaction = _db.OpenWithTransaction())
|
2025-02-11 17:07:01 +01:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateUsersTable();
|
2025-02-14 20:14:01 +01:00
|
|
|
|
CreateOrganizationsTable();
|
|
|
|
|
|
CreateUserOrganizationsTable();
|
2025-02-11 17:07:01 +01:00
|
|
|
|
SetupRLS();
|
|
|
|
|
|
|
|
|
|
|
|
_transaction.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
_transaction.Rollback();
|
|
|
|
|
|
throw new InvalidOperationException("Failed to SetupIdentitySystem. Transaction is rolled back", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void ExecuteSql(string sql)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(sql))
|
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
|
|
|
|
|
|
|
|
|
|
|
_db.ExecuteSql(sql);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates the users table
|
|
|
|
|
|
/// </summary>
|
2025-02-12 17:38:01 +01:00
|
|
|
|
void CreateUsersTable()
|
2025-02-11 17:07:01 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sql = @"
|
2025-02-06 16:58:13 +01:00
|
|
|
|
CREATE TABLE IF NOT EXISTS 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
|
|
|
|
|
|
);";
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
ExecuteSql(sql);
|
2025-02-06 16:58:13 +01:00
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
}
|
2025-02-06 16:58:13 +01:00
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
/// <summary>
|
2025-02-14 20:14:01 +01:00
|
|
|
|
/// Creates the organizations table
|
2025-02-11 17:07:01 +01:00
|
|
|
|
/// </summary>
|
2025-02-14 20:14:01 +01:00
|
|
|
|
void CreateOrganizationsTable()
|
2025-02-11 17:07:01 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sql = @"
|
2025-02-14 20:14:01 +01:00
|
|
|
|
CREATE TABLE IF NOT EXISTS organizations (
|
2025-02-06 16:58:13 +01:00
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
|
connection_string VARCHAR(500) NOT NULL,
|
|
|
|
|
|
is_active BOOLEAN NOT NULL DEFAULT TRUE,
|
|
|
|
|
|
created_by INTEGER NOT NULL REFERENCES users(id),
|
|
|
|
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
|
|
|
|
|
);";
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
ExecuteSql(sql);
|
2025-02-06 16:58:13 +01:00
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
}
|
2025-02-06 16:58:13 +01:00
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
/// <summary>
|
2025-02-14 20:14:01 +01:00
|
|
|
|
/// Creates the user_organizations table
|
2025-02-11 17:07:01 +01:00
|
|
|
|
/// </summary>
|
2025-02-14 20:14:01 +01:00
|
|
|
|
void CreateUserOrganizationsTable()
|
2025-02-11 17:07:01 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sql = @"
|
2025-02-14 20:14:01 +01:00
|
|
|
|
CREATE TABLE IF NOT EXISTS user_organizations (
|
2025-02-06 16:58:13 +01:00
|
|
|
|
user_id INTEGER NOT NULL REFERENCES users(id),
|
2025-02-14 20:14:01 +01:00
|
|
|
|
organization_id INTEGER NOT NULL REFERENCES organizations(id),
|
2025-02-06 16:58:13 +01:00
|
|
|
|
pin_code VARCHAR(10) NULL,
|
|
|
|
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
2025-02-14 20:14:01 +01:00
|
|
|
|
PRIMARY KEY (user_id, organization_id)
|
2025-02-06 16:58:13 +01:00
|
|
|
|
);";
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
ExecuteSql(sql);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-14 20:14:01 +01:00
|
|
|
|
/// Sets up Row Level Security (RLS) for the organizations and user_organizations tables.
|
2025-02-11 17:07:01 +01:00
|
|
|
|
/// </summary>
|
2025-02-12 17:38:01 +01:00
|
|
|
|
void SetupRLS()
|
2025-02-11 17:07:01 +01:00
|
|
|
|
{
|
|
|
|
|
|
var sql = new[]
|
|
|
|
|
|
{
|
2025-02-14 20:14:01 +01:00
|
|
|
|
"ALTER TABLE organizations ENABLE ROW LEVEL SECURITY;",
|
|
|
|
|
|
"ALTER TABLE user_organizations ENABLE ROW LEVEL SECURITY;",
|
|
|
|
|
|
"DROP POLICY IF EXISTS organization_access ON organizations;",
|
|
|
|
|
|
@"CREATE POLICY organization_access ON organizations
|
2025-02-06 16:58:13 +01:00
|
|
|
|
USING (id IN (
|
2025-02-14 20:14:01 +01:00
|
|
|
|
SELECT organization_id
|
|
|
|
|
|
FROM user_organizations
|
2025-02-06 16:58:13 +01:00
|
|
|
|
WHERE user_id = current_setting('app.user_id', TRUE)::INTEGER
|
|
|
|
|
|
));",
|
2025-02-14 20:14:01 +01:00
|
|
|
|
"DROP POLICY IF EXISTS user_organization_access ON user_organizations;",
|
|
|
|
|
|
@"CREATE POLICY user_organization_access ON user_organizations
|
2025-02-06 16:58:13 +01:00
|
|
|
|
USING (user_id = current_setting('app.user_id', TRUE)::INTEGER);"
|
2025-02-11 17:07:01 +01:00
|
|
|
|
};
|
2025-02-06 16:58:13 +01:00
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
foreach (var statement in sql)
|
|
|
|
|
|
{
|
|
|
|
|
|
ExecuteSql(statement);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-02-06 16:58:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
2025-02-11 17:07:01 +01:00
|
|
|
|
}
|
2025-02-06 16:58:13 +01:00
|
|
|
|
}
|