Setup db automation
This commit is contained in:
parent
1540f9f655
commit
72544d62e2
12 changed files with 473 additions and 337 deletions
|
|
@ -8,8 +8,9 @@ namespace Core.ModuleRegistry
|
||||||
public required string ConnectionString { get; set; }
|
public required string ConnectionString { get; set; }
|
||||||
protected override void Load(ContainerBuilder builder)
|
protected override void Load(ContainerBuilder builder)
|
||||||
{
|
{
|
||||||
|
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
||||||
|
|
||||||
builder.Register(c =>
|
builder.Register(c =>
|
||||||
{
|
{
|
||||||
IDbConnection connection = new NpgsqlConnection(ConnectionString);
|
IDbConnection connection = new NpgsqlConnection(ConnectionString);
|
||||||
return connection;
|
return connection;
|
||||||
|
|
|
||||||
148
Database/Core/SetupIdentitySystem.cs
Normal file
148
Database/Core/SetupIdentitySystem.cs
Normal file
|
|
@ -0,0 +1,148 @@
|
||||||
|
using Insight.Database;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
|
namespace Database.IdentitySystem
|
||||||
|
{
|
||||||
|
public interface IDbSetup
|
||||||
|
{
|
||||||
|
void CreateSystem(string schema = null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This is by purpose not async await
|
||||||
|
/// </summary>
|
||||||
|
public class DbSetup : IDbSetup
|
||||||
|
{
|
||||||
|
readonly IDbConnection _db;
|
||||||
|
IDbTransaction _transaction = null;
|
||||||
|
string _schema;
|
||||||
|
|
||||||
|
public DbSetup(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 CreateSystem(string schema = null)
|
||||||
|
{
|
||||||
|
|
||||||
|
using (_transaction = _db.BeginTransaction())
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
CreateUsersTable();
|
||||||
|
CreateTenantsTable();
|
||||||
|
CreateUserTenantsTable();
|
||||||
|
SetupRLS();
|
||||||
|
|
||||||
|
_transaction.Commit();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_transaction.Rollback();
|
||||||
|
throw new InvalidOperationException("Failed to create system tables.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private void ExecuteSql(string sql)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(sql))
|
||||||
|
throw new ArgumentNullException(nameof(sql));
|
||||||
|
|
||||||
|
_db.ExecuteSql(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates the users table
|
||||||
|
/// </summary>
|
||||||
|
public void CreateUsersTable()
|
||||||
|
{
|
||||||
|
var sql = @"
|
||||||
|
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
|
||||||
|
);";
|
||||||
|
|
||||||
|
ExecuteSql(sql);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates the tenants table
|
||||||
|
/// </summary>
|
||||||
|
public void CreateTenantsTable()
|
||||||
|
{
|
||||||
|
var sql = @"
|
||||||
|
CREATE TABLE IF NOT EXISTS tenants (
|
||||||
|
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
|
||||||
|
);";
|
||||||
|
|
||||||
|
ExecuteSql(sql);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates the user_tenants table
|
||||||
|
/// </summary>
|
||||||
|
public void CreateUserTenantsTable()
|
||||||
|
{
|
||||||
|
var sql = @"
|
||||||
|
CREATE TABLE IF NOT EXISTS user_tenants (
|
||||||
|
user_id INTEGER NOT NULL REFERENCES users(id),
|
||||||
|
tenant_id INTEGER NOT NULL REFERENCES tenants(id),
|
||||||
|
pin_code VARCHAR(10) NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (user_id, tenant_id)
|
||||||
|
);";
|
||||||
|
|
||||||
|
ExecuteSql(sql);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sets up Row Level Security (RLS) for the tenants and user_tenants tables.
|
||||||
|
/// </summary>
|
||||||
|
public void SetupRLS()
|
||||||
|
{
|
||||||
|
var sql = new[]
|
||||||
|
{
|
||||||
|
"ALTER TABLE tenants ENABLE ROW LEVEL SECURITY;",
|
||||||
|
"ALTER TABLE user_tenants ENABLE ROW LEVEL SECURITY;",
|
||||||
|
"DROP POLICY IF EXISTS tenant_access ON tenants;",
|
||||||
|
@"CREATE POLICY tenant_access ON tenants
|
||||||
|
USING (id IN (
|
||||||
|
SELECT tenant_id
|
||||||
|
FROM user_tenants
|
||||||
|
WHERE user_id = current_setting('app.user_id', TRUE)::INTEGER
|
||||||
|
));",
|
||||||
|
"DROP POLICY IF EXISTS user_tenant_access ON user_tenants;",
|
||||||
|
@"CREATE POLICY user_tenant_access ON user_tenants
|
||||||
|
USING (user_id = current_setting('app.user_id', TRUE)::INTEGER);"
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var statement in sql)
|
||||||
|
{
|
||||||
|
ExecuteSql(statement);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
62
Database/Core/SetupUser.cs
Normal file
62
Database/Core/SetupUser.cs
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
using System;
|
||||||
|
using System.Data;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
using Insight.Database;
|
||||||
|
namespace Database.Core
|
||||||
|
{
|
||||||
|
|
||||||
|
public class SetupUser
|
||||||
|
{
|
||||||
|
private readonly IDbConnection _db;
|
||||||
|
|
||||||
|
public SetupUser(IDbConnection db)
|
||||||
|
{
|
||||||
|
_db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task CreateTenantInDatabase(string schema, string user, string password)
|
||||||
|
{
|
||||||
|
if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$"))
|
||||||
|
throw new ArgumentException("Invalid schema name");
|
||||||
|
|
||||||
|
|
||||||
|
await CreateUser(user, password);
|
||||||
|
await CreateSchema(schema);
|
||||||
|
await GrantSchemaRights(schema, user);
|
||||||
|
|
||||||
|
await CreateNavigationLinkTemplatesTable(schema);
|
||||||
|
await CreateNavigationLinkTemplateTranslationsTable(schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task CreateSchema(string schema)
|
||||||
|
{
|
||||||
|
var sql = $"CREATE SCHEMA IF NOT EXISTS {schema}";
|
||||||
|
await _db.ExecuteAsync(sql);
|
||||||
|
}
|
||||||
|
private async Task CreateUser(string user, string password)
|
||||||
|
{
|
||||||
|
var sql = $"CREATE USER {user} WITH PASSWORD '{password}';";
|
||||||
|
await _db.ExecuteAsync(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task GrantSchemaRights(string schema, string user)
|
||||||
|
{
|
||||||
|
var sql = $"GRANT USAGE ON SCHEMA {schema} TO {user};";
|
||||||
|
await _db.ExecuteAsync(sql);
|
||||||
|
|
||||||
|
var sql1 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {schema} " +
|
||||||
|
$"GRANT ALL PRIVILEGES ON TABLES TO {user};";
|
||||||
|
await _db.ExecuteAsync(sql1);
|
||||||
|
|
||||||
|
var sql2 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {schema} TO {user};";
|
||||||
|
await _db.ExecuteAsync(sql2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,147 +0,0 @@
|
||||||
using Database.Common;
|
|
||||||
using Insight.Database;
|
|
||||||
using System;
|
|
||||||
using System.Data;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Database.IdentitySystem
|
|
||||||
{
|
|
||||||
public class DbSetup
|
|
||||||
{
|
|
||||||
readonly IDbConnection _db;
|
|
||||||
IDbTransaction _transaction = null;
|
|
||||||
string _schema;
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates the users table in the ptmain schema.
|
|
||||||
/// </summary>
|
|
||||||
public async Task CreateUsersTable()
|
|
||||||
{
|
|
||||||
var sql = @"
|
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates the tenants table in the ptmain schema.
|
|
||||||
/// </summary>
|
|
||||||
public async Task CreateTenantsTable()
|
|
||||||
{
|
|
||||||
var sql = @"
|
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates the user_tenants table in the ptmain schema.
|
|
||||||
/// </summary>
|
|
||||||
public async Task CreateUserTenantsTable()
|
|
||||||
{
|
|
||||||
var sql = @"
|
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sets up Row Level Security (RLS) for the tenants and user_tenants tables.
|
|
||||||
/// </summary>
|
|
||||||
public async Task SetupRLS()
|
|
||||||
{
|
|
||||||
var sql = new[]
|
|
||||||
{
|
|
||||||
"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)
|
|
||||||
{
|
|
||||||
await ExecuteSqlAsync(statement).ConfigureAwait(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
49
Database/RolesPermissionSystem/NavigationSystem.cs
Normal file
49
Database/RolesPermissionSystem/NavigationSystem.cs
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
using Insight.Database;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
|
namespace Database.RolesPermissionSystem
|
||||||
|
{
|
||||||
|
internal class NavigationSystem
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly IDbConnection _db;
|
||||||
|
|
||||||
|
public NavigationSystem(IDbConnection db)
|
||||||
|
{
|
||||||
|
_db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private async Task CreateNavigationLinkTemplatesTable(string schema)
|
||||||
|
{
|
||||||
|
var sql = $@"
|
||||||
|
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_templates (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
parent_id INTEGER NULL,
|
||||||
|
url VARCHAR(500) NOT NULL,
|
||||||
|
permission_id INTEGER NULL,
|
||||||
|
icon VARCHAR(100) NULL,
|
||||||
|
default_order INTEGER NOT NULL,
|
||||||
|
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id),
|
||||||
|
FOREIGN KEY (parent_id) REFERENCES {schema}.navigation_link_templates(id)
|
||||||
|
)";
|
||||||
|
await _db.ExecuteAsync(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task CreateNavigationLinkTemplateTranslationsTable(string schema)
|
||||||
|
{
|
||||||
|
var sql = $@"
|
||||||
|
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_template_translations (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
template_id INTEGER NOT NULL,
|
||||||
|
language VARCHAR(10) NOT NULL,
|
||||||
|
display_name VARCHAR(100) NOT NULL,
|
||||||
|
FOREIGN KEY (template_id) REFERENCES {schema}.navigation_link_templates(id)
|
||||||
|
)";
|
||||||
|
await _db.ExecuteAsync(sql);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,87 +5,87 @@ using Insight.Database;
|
||||||
|
|
||||||
namespace Database.RolesPermissionSystem
|
namespace Database.RolesPermissionSystem
|
||||||
{
|
{
|
||||||
public class Setup
|
public class Setup
|
||||||
{
|
{
|
||||||
private readonly IDbConnection _db;
|
private readonly IDbConnection _db;
|
||||||
|
|
||||||
public Setup(IDbConnection db)
|
public Setup(IDbConnection db)
|
||||||
{
|
{
|
||||||
_db = db ?? throw new ArgumentNullException(nameof(db));
|
_db = db ?? throw new ArgumentNullException(nameof(db));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates the system tables in the specified schema within a transaction.
|
/// Creates the system tables in the specified schema within a transaction.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="schema">The schema name where the tables will be created.</param>
|
/// <param name="schema">The schema name where the tables will be created.</param>
|
||||||
public async Task CreateSystem(string schema)
|
public void CreateSystem(string schema)
|
||||||
{
|
{
|
||||||
if (!Validations.IsValidSchemaName(schema))
|
if (!Validations.IsValidSchemaName(schema))
|
||||||
throw new ArgumentException("Invalid schema name", nameof(schema));
|
throw new ArgumentException("Invalid schema name", nameof(schema));
|
||||||
|
|
||||||
using (var transaction = _db.BeginTransaction())
|
using (var transaction = _db.BeginTransaction())
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await CreateRolesTable(schema, transaction).ConfigureAwait(false);
|
CreateRolesTable(schema, transaction);
|
||||||
await CreatePermissionsTable(schema, transaction).ConfigureAwait(false);
|
CreatePermissionsTable(schema, transaction);
|
||||||
await CreatePermissionTypesTable(schema, transaction).ConfigureAwait(false);
|
CreatePermissionTypesTable(schema, transaction);
|
||||||
await CreateRolePermissionsTable(schema, transaction).ConfigureAwait(false);
|
CreateRolePermissionsTable(schema, transaction);
|
||||||
|
|
||||||
transaction.Commit();
|
transaction.Commit();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
transaction.Rollback();
|
transaction.Rollback();
|
||||||
throw new InvalidOperationException("Failed to create system tables.", ex);
|
throw new InvalidOperationException("Failed to create system tables.", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private async Task ExecuteSqlAsync(string sql, IDbTransaction transaction)
|
private void ExecuteSql(string sql, IDbTransaction transaction)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(sql))
|
if (string.IsNullOrEmpty(sql))
|
||||||
throw new ArgumentNullException(nameof(sql));
|
throw new ArgumentNullException(nameof(sql));
|
||||||
|
|
||||||
await _db.ExecuteAsync(sql, transaction: transaction).ConfigureAwait(false);
|
_db.Execute(sql, transaction: transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CreatePermissionTypesTable(string schema, IDbTransaction transaction)
|
private void CreatePermissionTypesTable(string schema, IDbTransaction transaction)
|
||||||
{
|
{
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.permission_types (
|
CREATE TABLE IF NOT EXISTS {schema}.permission_types (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(100) NOT NULL UNIQUE
|
name VARCHAR(100) NOT NULL UNIQUE
|
||||||
)";
|
)";
|
||||||
await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false);
|
ExecuteSql(sql, transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CreatePermissionsTable(string schema, IDbTransaction transaction)
|
private void CreatePermissionsTable(string schema, IDbTransaction transaction)
|
||||||
{
|
{
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.permissions (
|
CREATE TABLE IF NOT EXISTS {schema}.permissions (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(100) NOT NULL UNIQUE,
|
name VARCHAR(100) NOT NULL UNIQUE,
|
||||||
type_id INTEGER NOT NULL,
|
type_id INTEGER NOT NULL,
|
||||||
FOREIGN KEY (type_id) REFERENCES {schema}.permission_types(id)
|
FOREIGN KEY (type_id) REFERENCES {schema}.permission_types(id)
|
||||||
)";
|
)";
|
||||||
await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false);
|
ExecuteSql(sql, transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CreateRolesTable(string schema, IDbTransaction transaction)
|
private void CreateRolesTable(string schema, IDbTransaction transaction)
|
||||||
{
|
{
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.roles (
|
CREATE TABLE IF NOT EXISTS {schema}.roles (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(100) NOT NULL UNIQUE
|
name VARCHAR(100) NOT NULL UNIQUE
|
||||||
)";
|
)";
|
||||||
await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false);
|
ExecuteSql(sql, transaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CreateRolePermissionsTable(string schema, IDbTransaction transaction)
|
private void CreateRolePermissionsTable(string schema, IDbTransaction transaction)
|
||||||
{
|
{
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.role_permissions (
|
CREATE TABLE IF NOT EXISTS {schema}.role_permissions (
|
||||||
role_id INTEGER NOT NULL,
|
role_id INTEGER NOT NULL,
|
||||||
permission_id INTEGER NOT NULL,
|
permission_id INTEGER NOT NULL,
|
||||||
|
|
@ -93,7 +93,7 @@ namespace Database.RolesPermissionSystem
|
||||||
FOREIGN KEY (role_id) REFERENCES {schema}.roles(id),
|
FOREIGN KEY (role_id) REFERENCES {schema}.roles(id),
|
||||||
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id)
|
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id)
|
||||||
)";
|
)";
|
||||||
await ExecuteSqlAsync(sql, transaction).ConfigureAwait(false);
|
ExecuteSql(sql, transaction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -5,105 +5,103 @@ using Insight.Database;
|
||||||
namespace Database.Tenants
|
namespace Database.Tenants
|
||||||
{
|
{
|
||||||
|
|
||||||
public class TenantSetupService
|
public class Setup
|
||||||
{
|
{
|
||||||
private readonly IDbConnection _db;
|
private readonly IDbConnection _db;
|
||||||
|
|
||||||
public TenantSetupService(IDbConnection db)
|
public Setup(IDbConnection db)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task CreateTenantInDatabase(string schema, string user, string password)
|
public async Task CreateTenantInDatabase(string schema, string user, string password)
|
||||||
{
|
{
|
||||||
if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$"))
|
if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$"))
|
||||||
throw new ArgumentException("Invalid schema name");
|
throw new ArgumentException("Invalid schema name");
|
||||||
|
|
||||||
|
|
||||||
await CreateUser(user, password);
|
await CreateUser(user, password);
|
||||||
await CreateSchema(schema);
|
await CreateSchema(schema);
|
||||||
await CreateRolesTable(schema);
|
await GrantSchemaRights(schema, user);
|
||||||
await CreatePermissionsTable(schema);
|
|
||||||
await CreateRolePermissionsTable(schema);
|
|
||||||
await CreateNavigationLinkTemplatesTable(schema);
|
|
||||||
await CreateNavigationLinkTemplateTranslationsTable(schema);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task CreateSchema(string schema)
|
await CreateNavigationLinkTemplatesTable(schema);
|
||||||
{
|
await CreateNavigationLinkTemplateTranslationsTable(schema);
|
||||||
var sql = $"CREATE SCHEMA IF NOT EXISTS {schema}";
|
}
|
||||||
await _db.ExecuteAsync(sql);
|
|
||||||
}
|
|
||||||
private async Task CreateUser(string user, string password)
|
|
||||||
{
|
|
||||||
var sql = $"CREATE USER {user} WITH PASSWORD '{password}';";
|
|
||||||
await _db.ExecuteAsync(sql);
|
|
||||||
}
|
|
||||||
|
|
||||||
private async Task GrantSchemaRights(string schema, string user)
|
private async Task CreateSchema(string schema)
|
||||||
{
|
{
|
||||||
var sql = $"GRANT USAGE ON SCHEMA {schema} TO {user};";
|
var sql = $"CREATE SCHEMA IF NOT EXISTS {schema}";
|
||||||
await _db.ExecuteAsync(sql);
|
await _db.ExecuteAsync(sql);
|
||||||
|
}
|
||||||
|
private async Task CreateUser(string user, string password)
|
||||||
|
{
|
||||||
|
var sql = $"CREATE USER {user} WITH PASSWORD '{password}';";
|
||||||
|
await _db.ExecuteAsync(sql);
|
||||||
|
}
|
||||||
|
|
||||||
var sql1 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {schema} " +
|
private async Task GrantSchemaRights(string schema, string user)
|
||||||
$"GRANT ALL PRIVILEGES ON TABLES TO {user};";
|
{
|
||||||
await _db.ExecuteAsync(sql1);
|
var sql = $"GRANT USAGE ON SCHEMA {schema} TO {user};";
|
||||||
|
await _db.ExecuteAsync(sql);
|
||||||
|
|
||||||
|
var sql1 = $"ALTER DEFAULT PRIVILEGES IN SCHEMA {schema} " +
|
||||||
|
$"GRANT ALL PRIVILEGES ON TABLES TO {user};";
|
||||||
|
await _db.ExecuteAsync(sql1);
|
||||||
|
|
||||||
var sql2 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {schema} TO {user};";
|
var sql2 = $"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA {schema} TO {user};";
|
||||||
await _db.ExecuteAsync(sql2);
|
await _db.ExecuteAsync(sql2);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
private async Task CreatePermissionTypesTable(string schema)
|
//private async Task CreatePermissionTypesTable(string schema)
|
||||||
{
|
//{
|
||||||
var sql = $@"
|
// var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.permission_types (
|
// CREATE TABLE IF NOT EXISTS {schema}.permission_types (
|
||||||
id SERIAL PRIMARY KEY,
|
// id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(100) NOT NULL UNIQUE
|
// name VARCHAR(100) NOT NULL UNIQUE
|
||||||
)";
|
// )";
|
||||||
await _db.ExecuteAsync(sql);
|
// await _db.ExecuteAsync(sql);
|
||||||
}
|
//}
|
||||||
|
|
||||||
private async Task CreatePermissionsTable(string schema)
|
//private async Task CreatePermissionsTable(string schema)
|
||||||
{
|
//{
|
||||||
var sql = $@"
|
// var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.permissions (
|
// CREATE TABLE IF NOT EXISTS {schema}.permissions (
|
||||||
id SERIAL PRIMARY KEY,
|
// id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(100) NOT NULL UNIQUE,
|
// name VARCHAR(100) NOT NULL UNIQUE,
|
||||||
type_id INTEGER NOT NULL,
|
// type_id INTEGER NOT NULL,
|
||||||
FOREIGN KEY (type_id) REFERENCES {schema}.permission_types(id)
|
// FOREIGN KEY (type_id) REFERENCES {schema}.permission_types(id)
|
||||||
)";
|
// )";
|
||||||
await _db.ExecuteAsync(sql);
|
// await _db.ExecuteAsync(sql);
|
||||||
}
|
//}
|
||||||
|
|
||||||
private async Task CreateRolesTable(string schema)
|
//private async Task CreateRolesTable(string schema)
|
||||||
{
|
//{
|
||||||
var sql = $@"
|
// var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.roles (
|
// CREATE TABLE IF NOT EXISTS {schema}.roles (
|
||||||
id SERIAL PRIMARY KEY,
|
// id SERIAL PRIMARY KEY,
|
||||||
name VARCHAR(100) NOT NULL UNIQUE
|
// name VARCHAR(100) NOT NULL UNIQUE
|
||||||
)";
|
// )";
|
||||||
await _db.ExecuteAsync(sql);
|
// await _db.ExecuteAsync(sql);
|
||||||
}
|
//}
|
||||||
|
|
||||||
private async Task CreateRolePermissionsTable(string schema)
|
//private async Task CreateRolePermissionsTable(string schema)
|
||||||
{
|
//{
|
||||||
var sql = $@"
|
// var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.role_permissions (
|
// CREATE TABLE IF NOT EXISTS {schema}.role_permissions (
|
||||||
role_id INTEGER NOT NULL,
|
// role_id INTEGER NOT NULL,
|
||||||
permission_id INTEGER NOT NULL,
|
// permission_id INTEGER NOT NULL,
|
||||||
PRIMARY KEY (role_id, permission_id),
|
// PRIMARY KEY (role_id, permission_id),
|
||||||
FOREIGN KEY (role_id) REFERENCES {schema}.roles(id),
|
// FOREIGN KEY (role_id) REFERENCES {schema}.roles(id),
|
||||||
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id)
|
// FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id)
|
||||||
)";
|
// )";
|
||||||
await _db.ExecuteAsync(sql);
|
// await _db.ExecuteAsync(sql);
|
||||||
}
|
//}
|
||||||
|
|
||||||
private async Task CreateNavigationLinkTemplatesTable(string schema)
|
private async Task CreateNavigationLinkTemplatesTable(string schema)
|
||||||
{
|
{
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_templates (
|
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_templates (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
parent_id INTEGER NULL,
|
parent_id INTEGER NULL,
|
||||||
|
|
@ -114,12 +112,12 @@ namespace Database.Tenants
|
||||||
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id),
|
FOREIGN KEY (permission_id) REFERENCES {schema}.permissions(id),
|
||||||
FOREIGN KEY (parent_id) REFERENCES {schema}.navigation_link_templates(id)
|
FOREIGN KEY (parent_id) REFERENCES {schema}.navigation_link_templates(id)
|
||||||
)";
|
)";
|
||||||
await _db.ExecuteAsync(sql);
|
await _db.ExecuteAsync(sql);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task CreateNavigationLinkTemplateTranslationsTable(string schema)
|
private async Task CreateNavigationLinkTemplateTranslationsTable(string schema)
|
||||||
{
|
{
|
||||||
var sql = $@"
|
var sql = $@"
|
||||||
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_template_translations (
|
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_template_translations (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
template_id INTEGER NOT NULL,
|
template_id INTEGER NOT NULL,
|
||||||
|
|
@ -127,9 +125,9 @@ namespace Database.Tenants
|
||||||
display_name VARCHAR(100) NOT NULL,
|
display_name VARCHAR(100) NOT NULL,
|
||||||
FOREIGN KEY (template_id) REFERENCES {schema}.navigation_link_templates(id)
|
FOREIGN KEY (template_id) REFERENCES {schema}.navigation_link_templates(id)
|
||||||
)";
|
)";
|
||||||
await _db.ExecuteAsync(sql);
|
await _db.ExecuteAsync(sql);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,47 +1,33 @@
|
||||||
using Microsoft.ApplicationInsights;
|
using Autofac;
|
||||||
using Microsoft.ApplicationInsights.Channel;
|
|
||||||
using Microsoft.ApplicationInsights.Extensibility;
|
|
||||||
using Microsoft.ApplicationInsights.WindowsServer.TelemetryChannel;
|
|
||||||
|
|
||||||
namespace SetupInfrastructure
|
namespace SetupInfrastructure
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// SETUP APPLICATION USER NAMED sathumper
|
/// SETUP APPLICATION USER NAMED sathumper
|
||||||
///
|
///
|
||||||
/// This should be handled on the Postgresql db server with a superadmin or similar.
|
/// This should be handled on the Postgresql db server with a superadmin or similar.
|
||||||
///
|
///
|
||||||
/// Execute SQL CreateRole.txt
|
/// Execute SQL CreateRole.txt
|
||||||
///
|
///
|
||||||
/// After that is executed it is time for running this main program
|
/// After that is executed it is time for running this main program
|
||||||
/// Remember to use the newly created sathumper
|
/// Remember to use the newly created sathumper
|
||||||
/// "ConnectionStrings": {
|
/// "ConnectionStrings": {
|
||||||
/// "ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=<secret>;"
|
/// "ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=<secret>;"
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class Program
|
internal class Program
|
||||||
{
|
{
|
||||||
static async Task Main(string[] args)
|
static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
|
var container = new Startup().ConfigureContainer();
|
||||||
|
|
||||||
var telemetryChannel = new ServerTelemetryChannel();
|
|
||||||
var configuration = Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.CreateDefault();
|
container.Resolve
|
||||||
configuration.ConnectionString = "InstrumentationKey=2d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/";
|
|
||||||
configuration.TelemetryChannel = telemetryChannel;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
telemetryChannel.Initialize(configuration);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var log = new TelemetryClient(configuration);
|
|
||||||
log.TrackTrace("Console log med kanal 2");
|
|
||||||
|
|
||||||
log.Flush();
|
|
||||||
|
|
||||||
Console.WriteLine("Hello, World!");
|
|
||||||
await Task.Delay(5000);
|
|
||||||
|
|
||||||
Console.Read();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
39
SetupInfrastructure/Startup.cs
Normal file
39
SetupInfrastructure/Startup.cs
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
using Autofac;
|
||||||
|
using Core.Configurations;
|
||||||
|
using Core.Configurations.JsonConfigProvider;
|
||||||
|
|
||||||
|
|
||||||
|
namespace SetupInfrastructure
|
||||||
|
{
|
||||||
|
public class Startup
|
||||||
|
{
|
||||||
|
public virtual IConfigurationRoot Configuration()
|
||||||
|
{
|
||||||
|
var configuration = new ConfigurationBuilder()
|
||||||
|
.AddJsonFile("appconfiguration.dev.json")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public IContainer ConfigureContainer()
|
||||||
|
{
|
||||||
|
var builder = new ContainerBuilder();
|
||||||
|
var configuration = Configuration();
|
||||||
|
|
||||||
|
|
||||||
|
builder.RegisterModule(new Core.ModuleRegistry.DbPostgreSqlModule
|
||||||
|
{
|
||||||
|
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.RegisterModule(new Core.ModuleRegistry.TelemetryModule
|
||||||
|
{
|
||||||
|
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<Core.ModuleRegistry.TelemetryConfig>()
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
return builder.Build();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"AllowedHosts": "*",
|
"AllowedHosts": "*",
|
||||||
"ConnectionStrings": {
|
"ConnectionStrings": {
|
||||||
"ptdb": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=3911;"
|
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptdb01;User Id=sathumper;Password=3911;"
|
||||||
},
|
},
|
||||||
"ApplicationInsights": {
|
"ApplicationInsights": {
|
||||||
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/"
|
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/"
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ namespace Tests
|
||||||
{
|
{
|
||||||
CreateContainerBuilder();
|
CreateContainerBuilder();
|
||||||
Container = ContainerBuilder.Build();
|
Container = ContainerBuilder.Build();
|
||||||
Insight.Database.Providers.PostgreSQL.PostgreSQLInsightDbProvider.RegisterProvider();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue