Renaming from tenant to organization

This commit is contained in:
Janus C. H. Knudsen 2025-02-14 20:14:01 +01:00
parent bf50563ab7
commit 087f8ce0e9
16 changed files with 129 additions and 243 deletions

View file

@ -31,8 +31,8 @@ namespace Database.Core.DDL
try
{
CreateUsersTable();
CreateTenantsTable();
CreateUserTenantsTable();
CreateOrganizationsTable();
CreateUserOrganizationsTable();
SetupRLS();
_transaction.Commit();
@ -78,12 +78,12 @@ namespace Database.Core.DDL
}
/// <summary>
/// Creates the tenants table
/// Creates the organizations table
/// </summary>
void CreateTenantsTable()
void CreateOrganizationsTable()
{
var sql = @"
CREATE TABLE IF NOT EXISTS tenants (
CREATE TABLE IF NOT EXISTS organizations (
id SERIAL PRIMARY KEY,
connection_string VARCHAR(500) NOT NULL,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
@ -96,17 +96,17 @@ namespace Database.Core.DDL
}
/// <summary>
/// Creates the user_tenants table
/// Creates the user_organizations table
/// </summary>
void CreateUserTenantsTable()
void CreateUserOrganizationsTable()
{
var sql = @"
CREATE TABLE IF NOT EXISTS user_tenants (
CREATE TABLE IF NOT EXISTS user_organizations (
user_id INTEGER NOT NULL REFERENCES users(id),
tenant_id INTEGER NOT NULL REFERENCES tenants(id),
organization_id INTEGER NOT NULL REFERENCES organizations(id),
pin_code VARCHAR(10) NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, tenant_id)
PRIMARY KEY (user_id, organization_id)
);";
ExecuteSql(sql);
@ -114,23 +114,23 @@ namespace Database.Core.DDL
}
/// <summary>
/// Sets up Row Level Security (RLS) for the tenants and user_tenants tables.
/// Sets up Row Level Security (RLS) for the organizations and user_organizations tables.
/// </summary>
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
"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
USING (id IN (
SELECT tenant_id
FROM user_tenants
SELECT organization_id
FROM user_organizations
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
"DROP POLICY IF EXISTS user_organization_access ON user_organizations;",
@"CREATE POLICY user_organization_access ON user_organizations
USING (user_id = current_setting('app.user_id', TRUE)::INTEGER);"
};