2023-01-19 07:27:01 -05:00
|
|
|
/* init_order = 1 */
|
|
|
|
|
2023-09-10 22:27:49 -05:00
|
|
|
create type role_type as enum ('User', 'MachineToMachine');
|
|
|
|
|
2022-01-26 21:48:55 -05:00
|
|
|
create table roles (
|
2023-01-19 07:27:01 -05:00
|
|
|
tenant_id varchar(21) not null
|
|
|
|
references tenants (id) on update cascade on delete cascade,
|
2022-11-23 22:09:03 -05:00
|
|
|
id varchar(21) not null,
|
|
|
|
name varchar(128) not null,
|
|
|
|
description varchar(128) not null,
|
2023-09-10 22:27:49 -05:00
|
|
|
type role_type not null default 'User',
|
2024-05-17 03:02:05 -05:00
|
|
|
/** If the role is the default role for a new user. Should be ignored for `MachineToMachine` roles. */
|
|
|
|
is_default boolean not null default false,
|
2023-01-19 07:27:01 -05:00
|
|
|
primary key (id),
|
|
|
|
constraint roles__name
|
|
|
|
unique (tenant_id, name)
|
2022-11-23 22:09:03 -05:00
|
|
|
);
|
|
|
|
|
2023-01-19 07:27:01 -05:00
|
|
|
create index roles__id
|
|
|
|
on roles (tenant_id, id);
|
2023-09-10 22:27:49 -05:00
|
|
|
|
2023-09-11 03:58:17 -05:00
|
|
|
create function public.check_role_type(role_id varchar(21), target_type role_type) returns boolean as
|
2023-09-10 22:27:49 -05:00
|
|
|
$$ begin
|
2023-09-11 03:58:17 -05:00
|
|
|
return (select type from public.roles where id = role_id) = target_type;
|
2023-09-10 22:27:49 -05:00
|
|
|
end; $$ language plpgsql;
|