PlanTempusApp/Database/RolesPermissionSystem/Setup.cs

100 lines
2.4 KiB
C#
Raw Normal View History

2025-01-28 14:51:09 +01:00
using System.Data;
using Database.Common;
2025-01-28 14:51:09 +01:00
using Insight.Database;
namespace Database.RolesPermissionSystem
2025-01-28 14:51:09 +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 Setup
{
IDbConnection _db;
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +01:00
public Setup(IDbConnection db)
{
_db = db;
}
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +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()
{
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +01:00
//if (!Validations.IsValidSchemaName(_schema))
// throw new ArgumentException("Invalid schema name", _schema);
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +01:00
using (var transaction = _db.BeginTransaction())
{
try
{
CreateRolesTable();
CreatePermissionsTable();
CreatePermissionTypesTable();
CreateRolePermissionsTable();
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +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-11 17:07:01 +01:00
private void ExecuteSql(string sql)
{
_db.ExecuteSql(sql);
}
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +01:00
private void CreatePermissionTypesTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS permission_types (
2025-01-28 14:51:09 +01:00
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
)";
2025-02-11 17:07:01 +01:00
ExecuteSql(sql);
}
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +01:00
private void CreatePermissionsTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS permissions (
2025-01-28 14:51:09 +01:00
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE,
type_id INTEGER NOT NULL,
2025-02-11 17:07:01 +01:00
FOREIGN KEY (type_id) REFERENCES permission_types(id)
2025-01-28 14:51:09 +01:00
)";
2025-02-11 17:07:01 +01:00
ExecuteSql(sql);
}
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +01:00
private void CreateRolesTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS roles (
2025-01-28 14:51:09 +01:00
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL UNIQUE
)";
2025-02-11 17:07:01 +01:00
ExecuteSql(sql);
}
2025-01-28 14:51:09 +01:00
2025-02-11 17:07:01 +01:00
private void CreateRolePermissionsTable()
{
var sql = $@"
CREATE TABLE IF NOT EXISTS role_permissions (
2025-01-28 14:51:09 +01:00
role_id INTEGER NOT NULL,
permission_id INTEGER NOT NULL,
PRIMARY KEY (role_id, permission_id),
2025-02-11 17:07:01 +01:00
FOREIGN KEY (role_id) REFERENCES roles(id),
FOREIGN KEY (permission_id) REFERENCES permissions(id)
2025-01-28 14:51:09 +01:00
)";
2025-02-11 17:07:01 +01:00
ExecuteSql(sql);
}
}
2025-01-28 14:51:09 +01:00
}