Introduce global scope (#168)
Introduce the global scope, which holds some resources that don't necessarily belong at either the org or project level.
This commit is contained in:
1 parent
6c1b45f7f5
commit
732c07126d
15 files changed
+353
-79
No files matched your search
@@ -1,12 +1,13 @@
|
||||
begin;
|
||||
|
||||
create table iam_scope_type_enm (
|
||||
string text not null primary key check(string in ('unknown', 'organization', 'project'))
|
||||
string text not null primary key check(string in ('unknown', 'global', 'organization', 'project'))
|
||||
);
|
||||
|
||||
insert into iam_scope_type_enm (string)
|
||||
values
|
||||
('unknown'),
|
||||
('global'),
|
||||
('organization'),
|
||||
('project');
|
||||
|
||||
@@ -29,32 +30,55 @@ is
|
||||
'function used in before update triggers to make scope_id column is immutable';
|
||||
|
||||
create table iam_scope (
|
||||
public_id wt_public_id primary key,
|
||||
public_id wt_scope_id primary key,
|
||||
create_time wt_timestamp,
|
||||
update_time wt_timestamp,
|
||||
name text,
|
||||
type text not null references iam_scope_type_enm(string) check(
|
||||
(
|
||||
type = 'global'
|
||||
and parent_id is null
|
||||
)
|
||||
or (
|
||||
type = 'organization'
|
||||
and parent_id = null
|
||||
and parent_id = 'global'
|
||||
)
|
||||
or (
|
||||
type = 'project'
|
||||
and parent_id is not null
|
||||
and parent_id != 'global'
|
||||
)
|
||||
),
|
||||
description text,
|
||||
parent_id text references iam_scope(public_id) on delete cascade on update cascade
|
||||
);
|
||||
|
||||
create table iam_scope_global (
|
||||
scope_id wt_scope_id primary key
|
||||
references iam_scope(public_id)
|
||||
on delete cascade
|
||||
on update cascade
|
||||
check(
|
||||
scope_id = 'global'
|
||||
),
|
||||
name text unique
|
||||
);
|
||||
|
||||
create table iam_scope_organization (
|
||||
scope_id wt_public_id not null unique references iam_scope(public_id) on delete cascade on update cascade,
|
||||
name text unique,
|
||||
primary key(scope_id)
|
||||
);
|
||||
scope_id wt_scope_id primary key
|
||||
references iam_scope(public_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
parent_id wt_scope_id not null
|
||||
references iam_scope_global(scope_id)
|
||||
on delete cascade
|
||||
on update cascade,
|
||||
name text,
|
||||
unique (parent_id, name)
|
||||
);
|
||||
|
||||
create table iam_scope_project (
|
||||
scope_id wt_public_id not null references iam_scope(public_id) on delete cascade on update cascade,
|
||||
scope_id wt_scope_id not null references iam_scope(public_id) on delete cascade on update cascade,
|
||||
parent_id wt_public_id not null references iam_scope_organization(scope_id) on delete cascade on update cascade,
|
||||
name text,
|
||||
unique(parent_id, name),
|
||||
@@ -66,13 +90,19 @@ create or replace function
|
||||
returns trigger
|
||||
as $$
|
||||
declare parent_type int;
|
||||
begin
|
||||
if new.type = 'organization' then
|
||||
insert into iam_scope_organization (scope_id, name)
|
||||
begin
|
||||
if new.type = 'global' then
|
||||
insert into iam_scope_global (scope_id, name)
|
||||
values
|
||||
(new.public_id, new.name);
|
||||
return new;
|
||||
end if;
|
||||
if new.type = 'organization' then
|
||||
insert into iam_scope_organization (scope_id, parent_id, name)
|
||||
values
|
||||
(new.public_id, new.parent_id, new.name);
|
||||
return new;
|
||||
end if;
|
||||
if new.type = 'project' then
|
||||
insert into iam_scope_project (scope_id, parent_id, name)
|
||||
values
|
||||
@@ -83,13 +113,29 @@ begin
|
||||
end;
|
||||
$$ language plpgsql;
|
||||
|
||||
|
||||
create trigger
|
||||
iam_scope_insert
|
||||
after
|
||||
insert on iam_scope
|
||||
for each row execute procedure iam_sub_scopes_func();
|
||||
|
||||
create or replace function
|
||||
disallow_global_scope_deletion()
|
||||
returns trigger
|
||||
as $$
|
||||
begin
|
||||
if old.type = 'global' then
|
||||
raise exception 'deletion of global scope not allowed';
|
||||
end if;
|
||||
return old;
|
||||
end;
|
||||
$$ language plpgsql;
|
||||
|
||||
create trigger
|
||||
iam_scope_disallow_global_deletion
|
||||
before
|
||||
delete on iam_scope
|
||||
for each row execute procedure disallow_global_scope_deletion();
|
||||
|
||||
create or replace function
|
||||
iam_immutable_scope_type_func()
|
||||
@@ -135,8 +181,12 @@ create or replace function
|
||||
iam_sub_names()
|
||||
returns trigger
|
||||
as $$
|
||||
begin
|
||||
begin
|
||||
if new.name != old.name then
|
||||
if new.type = 'global' then
|
||||
update iam_scope_global set name = new.name where scope_id = old.public_id;
|
||||
return new;
|
||||
end if;
|
||||
if new.type = 'organization' then
|
||||
update iam_scope_organization set name = new.name where scope_id = old.public_id;
|
||||
return new;
|
||||
@@ -157,13 +207,17 @@ before
|
||||
update on iam_scope
|
||||
for each row execute procedure iam_sub_names();
|
||||
|
||||
insert into iam_scope (public_id, name, type, description)
|
||||
values ('global', 'global', 'global', 'Global Scope');
|
||||
|
||||
|
||||
create table iam_user (
|
||||
public_id wt_public_id not null primary key,
|
||||
create_time wt_timestamp,
|
||||
update_time wt_timestamp,
|
||||
name text,
|
||||
description text,
|
||||
scope_id wt_public_id not null references iam_scope_organization(scope_id) on delete cascade on update cascade,
|
||||
scope_id wt_scope_id not null references iam_scope(public_id) on delete cascade on update cascade,
|
||||
unique(name, scope_id),
|
||||
disabled boolean not null default false,
|
||||
|
||||
@@ -173,6 +227,25 @@ create table iam_user (
|
||||
unique(scope_id, public_id)
|
||||
);
|
||||
|
||||
create or replace function
|
||||
user_scope_id_valid()
|
||||
returns trigger
|
||||
as $$
|
||||
begin
|
||||
perform from iam_scope where public_id = new.scope_id and type in ('global', 'organization');
|
||||
if not found then
|
||||
raise exception 'invalid scope type for user creation';
|
||||
end if;
|
||||
return new;
|
||||
end;
|
||||
$$ language plpgsql;
|
||||
|
||||
create trigger
|
||||
ensure_user_scope_id_valid
|
||||
before
|
||||
insert or update on iam_user
|
||||
for each row execute procedure user_scope_id_valid();
|
||||
|
||||
create trigger
|
||||
update_time_column
|
||||
before update on iam_user
|
||||
@@ -201,7 +274,7 @@ create table iam_role (
|
||||
update_time wt_timestamp,
|
||||
name text,
|
||||
description text,
|
||||
scope_id wt_public_id not null references iam_scope(public_id) on delete cascade on update cascade,
|
||||
scope_id wt_scope_id not null references iam_scope(public_id) on delete cascade on update cascade,
|
||||
unique(name, scope_id),
|
||||
disabled boolean not null default false,
|
||||
-- version allows optimistic locking of the role when modifying the role
|
||||
@@ -243,7 +316,7 @@ create table iam_group (
|
||||
update_time wt_timestamp,
|
||||
name text,
|
||||
description text,
|
||||
scope_id wt_public_id not null references iam_scope(public_id) on delete cascade on update cascade,
|
||||
scope_id wt_scope_id not null references iam_scope(public_id) on delete cascade on update cascade,
|
||||
unique(name, scope_id),
|
||||
disabled boolean not null default false,
|
||||
-- add unique index so a composite fk can be declared.
|
||||
@@ -280,7 +353,7 @@ update on iam_group
|
||||
-- with a before update trigger using iam_immutable_role().
|
||||
create table iam_user_role (
|
||||
create_time wt_timestamp,
|
||||
scope_id wt_public_id not null,
|
||||
scope_id wt_scope_id not null,
|
||||
role_id wt_public_id not null,
|
||||
principal_id wt_public_id not null references iam_user(public_id) on delete cascade on update cascade,
|
||||
primary key (role_id, principal_id),
|
||||
@@ -297,7 +370,7 @@ create table iam_user_role (
|
||||
-- update trigger using iam_immutable_role().
|
||||
create table iam_group_role (
|
||||
create_time wt_timestamp,
|
||||
scope_id wt_public_id not null,
|
||||
scope_id wt_scope_id not null,
|
||||
role_id wt_public_id not null,
|
||||
principal_id wt_public_id not null,
|
||||
primary key (role_id, principal_id),
|
||||
|
||||
Reference in new issue
Block a user