Working on roles, users and create tenant
This commit is contained in:
parent
269bf50c78
commit
fcffb57ac6
21 changed files with 483 additions and 56 deletions
127
Database/Tenants/TenantSetupService.cs
Normal file
127
Database/Tenants/TenantSetupService.cs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using Insight.Database;
|
||||
namespace Database.Tenants
|
||||
{
|
||||
|
||||
public class TenantSetupService
|
||||
{
|
||||
private readonly IDbConnection _db;
|
||||
|
||||
public TenantSetupService(IDbConnection db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
public async Task CreateTenant(string schema)
|
||||
{
|
||||
if (!Regex.IsMatch(schema, "^[a-zA-Z0-9_]+$"))
|
||||
{
|
||||
throw new ArgumentException("Invalid schema name");
|
||||
}
|
||||
|
||||
await CreateSchema(schema);
|
||||
await CreateRolesTable(schema);
|
||||
await CreatePermissionsTable(schema);
|
||||
await CreateRolePermissionsTable(schema);
|
||||
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);
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private async Task CreateRolesTable(string schema)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.roles (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL UNIQUE
|
||||
)";
|
||||
await _db.ExecuteAsync(sql);
|
||||
}
|
||||
|
||||
private async Task CreatePermissionsTable(string schema)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.permissions (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR(100) NOT NULL UNIQUE
|
||||
)";
|
||||
await _db.ExecuteAsync(sql);
|
||||
}
|
||||
|
||||
private async Task CreateRolePermissionsTable(string schema)
|
||||
{
|
||||
var sql = $@"
|
||||
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)
|
||||
)";
|
||||
await _db.ExecuteAsync(sql);
|
||||
}
|
||||
|
||||
private async Task CreateNavigationLinkTemplatesTable(string schema)
|
||||
{
|
||||
var sql = $@"
|
||||
CREATE TABLE IF NOT EXISTS {schema}.navigation_link_templates (
|
||||
id SERIAL PRIMARY KEY,
|
||||
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)
|
||||
)";
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue