2021-08-12 15:04:37 +02:00
exports . up = function ( db , cb ) {
2021-03-11 22:51:58 +01:00
db . runSql (
` CREATE TABLE IF NOT EXISTS roles
(
id SERIAL PRIMARY KEY ,
name text not null ,
description text ,
type text not null default 'custom' ,
project text ,
created _at TIMESTAMP WITH TIME ZONE DEFAULT now ( )
) ;
CREATE TABLE IF NOT EXISTS role _user
(
role _id integer not null references roles ( id ) ON DELETE CASCADE ,
user _id integer not null references users ( id ) ON DELETE CASCADE ,
created _at TIMESTAMP WITH TIME ZONE DEFAULT now ( ) ,
PRIMARY KEY ( role _id , user _id )
) ;
CREATE TABLE IF NOT EXISTS role _permission
(
role _id integer not null references roles ( id ) ON DELETE CASCADE ,
project text ,
permission text not null ,
created _at TIMESTAMP WITH TIME ZONE DEFAULT now ( )
) ;
WITH admin AS (
INSERT INTO roles ( name , description , type )
VALUES ( 'Admin' , 'Users with the global admin role have superuser access to Unleash and can perform any operation within the unleash platform.' , 'root' )
RETURNING id role _id
)
INSERT INTO role _permission ( role _id , permission )
SELECT role _id , 'ADMIN' from admin ;
WITH regular AS (
INSERT INTO roles ( name , description , type )
VALUES ( 'Regular' , 'Users with the global regular role have access most features in Unleash, but can not manage users and roles in the global scope. If a user with a global regular role creates a project, they will become a project admin and receive superuser rights within the context of that project.' , 'root' )
RETURNING id role _id
)
INSERT INTO role _permission ( role _id , project , permission )
VALUES
( ( SELECT role _id from regular ) , '' , 'CREATE_STRATEGY' ) ,
( ( SELECT role _id from regular ) , '' , 'UPDATE_STRATEGY' ) ,
( ( SELECT role _id from regular ) , '' , 'DELETE_STRATEGY' ) ,
( ( SELECT role _id from regular ) , '' , 'UPDATE_APPLICATION' ) ,
( ( SELECT role _id from regular ) , '' , 'CREATE_CONTEXT_FIELD' ) ,
( ( SELECT role _id from regular ) , '' , 'UPDATE_CONTEXT_FIELD' ) ,
( ( SELECT role _id from regular ) , '' , 'DELETE_CONTEXT_FIELD' ) ,
( ( SELECT role _id from regular ) , '' , 'CREATE_PROJECT' ) ,
( ( SELECT role _id from regular ) , '' , 'CREATE_ADDON' ) ,
( ( SELECT role _id from regular ) , '' , 'UPDATE_ADDON' ) ,
( ( SELECT role _id from regular ) , '' , 'DELETE_ADDON' ) ,
( ( SELECT role _id from regular ) , 'default' , 'UPDATE_PROJECT' ) ,
( ( SELECT role _id from regular ) , 'default' , 'DELETE_PROJECT' ) ,
( ( SELECT role _id from regular ) , 'default' , 'CREATE_FEATURE' ) ,
( ( SELECT role _id from regular ) , 'default' , 'UPDATE_FEATURE' ) ,
( ( SELECT role _id from regular ) , 'default' , 'DELETE_FEATURE' ) ;
INSERT INTO roles ( name , description , type )
VALUES ( 'Read' , 'Users with this role can only read root resources in Unleash. They may be added as collaborator to specific projects.' , 'root' ) ;
` ,
cb ,
) ;
} ;
2021-08-12 15:04:37 +02:00
exports . down = function ( db , cb ) {
2021-03-11 22:51:58 +01:00
db . runSql (
`
DROP TABLE role _user ;
DROP TABLE role _permission ;
DROP TABLE roles ;
` ,
cb ,
) ;
} ;