2025-01-28 14:51:09 +01:00
|
|
|
|
using System.Data;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
2025-02-02 23:13:17 +01:00
|
|
|
|
using Database.Common;
|
2025-01-28 14:51:09 +01:00
|
|
|
|
using Insight.Database;
|
|
|
|
|
|
|
2025-02-02 23:13:17 +01:00
|
|
|
|
namespace Database.RolesPermissionSystem
|
2025-01-28 14:51:09 +01:00
|
|
|
|
{
|
2025-02-06 16:58:13 +01:00
|
|
|
|
public class Setup
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IDbConnection _db;
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
public Setup(IDbConnection db)
|
|
|
|
|
|
{
|
|
|
|
|
|
_db = db ?? throw new ArgumentNullException(nameof(db));
|
|
|
|
|
|
}
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
/// <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 CreateSystem(string schema)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Validations.IsValidSchemaName(schema))
|
|
|
|
|
|
throw new ArgumentException("Invalid schema name", nameof(schema));
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
using (var transaction = _db.BeginTransaction())
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateRolesTable(schema, transaction);
|
|
|
|
|
|
CreatePermissionsTable(schema, transaction);
|
|
|
|
|
|
CreatePermissionTypesTable(schema, transaction);
|
|
|
|
|
|
CreateRolePermissionsTable(schema, transaction);
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
|
throw new InvalidOperationException("Failed to create system tables.", ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
private void ExecuteSql(string sql, IDbTransaction transaction)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(sql))
|
|
|
|
|
|
throw new ArgumentNullException(nameof(sql));
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
_db.Execute(sql, transaction: transaction);
|
|
|
|
|
|
}
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
private void CreatePermissionTypesTable(string schema, IDbTransaction transaction)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sql = $@"
|
2025-01-28 14:51:09 +01:00
|
|
|
|
CREATE TABLE IF NOT EXISTS {schema}.permission_types (
|
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
|
name VARCHAR(100) NOT NULL UNIQUE
|
|
|
|
|
|
)";
|
2025-02-06 16:58:13 +01:00
|
|
|
|
ExecuteSql(sql, transaction);
|
|
|
|
|
|
}
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
private void CreatePermissionsTable(string schema, IDbTransaction transaction)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sql = $@"
|
2025-01-28 14:51:09 +01:00
|
|
|
|
CREATE TABLE IF NOT EXISTS {schema}.permissions (
|
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
|
name VARCHAR(100) NOT NULL UNIQUE,
|
|
|
|
|
|
type_id INTEGER NOT NULL,
|
|
|
|
|
|
FOREIGN KEY (type_id) REFERENCES {schema}.permission_types(id)
|
|
|
|
|
|
)";
|
2025-02-06 16:58:13 +01:00
|
|
|
|
ExecuteSql(sql, transaction);
|
|
|
|
|
|
}
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
private void CreateRolesTable(string schema, IDbTransaction transaction)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sql = $@"
|
2025-01-28 14:51:09 +01:00
|
|
|
|
CREATE TABLE IF NOT EXISTS {schema}.roles (
|
|
|
|
|
|
id SERIAL PRIMARY KEY,
|
|
|
|
|
|
name VARCHAR(100) NOT NULL UNIQUE
|
|
|
|
|
|
)";
|
2025-02-06 16:58:13 +01:00
|
|
|
|
ExecuteSql(sql, transaction);
|
|
|
|
|
|
}
|
2025-01-28 14:51:09 +01:00
|
|
|
|
|
2025-02-06 16:58:13 +01:00
|
|
|
|
private void CreateRolePermissionsTable(string schema, IDbTransaction transaction)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sql = $@"
|
2025-01-28 14:51:09 +01:00
|
|
|
|
CREATE TABLE IF NOT EXISTS {schema}.role_permissions (
|
|
|
|
|
|
role_id INTEGER NOT NULL,
|
|
|
|
|
|
permission_id INTEGER NOT NULL,
|
|
|
|
|
|
PRIMARY KEY (role_id, permission_id),
|
|
|
|
|
|
FOREIGN KEY (role_id) REFERENCES {schema}.roles(id),
|
|
|
|
|
|
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id)
|
|
|
|
|
|
)";
|
2025-02-06 16:58:13 +01:00
|
|
|
|
ExecuteSql(sql, transaction);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-28 14:51:09 +01:00
|
|
|
|
}
|