2021-04-30 13:04:25 +02:00
const async = require ( 'async' ) ;
const DESCRIPTION = {
2021-08-12 15:04:37 +02:00
OWNER : 'Users with this role have full control over the project, and can add and manage other users within the project context, manage feature toggles within the project, and control advanced project features like archiving and deleting the project.' ,
MEMBER : 'Users with this role within a project are allowed to view, create and update feature toggles, but have limited permissions in regards to managing the projects user access and can not archive or delete the project.' ,
2021-04-30 13:04:25 +02:00
} ;
2021-08-12 15:04:37 +02:00
exports . up = function ( db , cb ) {
2021-04-30 13:04:25 +02:00
db . runSql (
2021-08-12 15:04:37 +02:00
'SELECT id AS name from projects WHERE id NOT IN (SELECT DISTINCT project FROM roles WHERE project IS NOT null)' ,
2021-04-30 13:04:25 +02:00
( err , results ) => {
if ( results && results . rowCount > 0 ) {
const projects = results . rows ;
2021-08-12 15:04:37 +02:00
const createProjectRoles = projects . map ( ( p ) =>
2021-04-30 13:04:25 +02:00
db . runSql . bind (
db ,
`
WITH project _owner AS (
INSERT into roles ( name , description , type , project )
VALUES ( 'Owner' , '${DESCRIPTION.OWNER}' , 'project' , '${p.name}' )
RETURNING id role _id
)
INSERT INTO role _permission ( role _id , project , permission ) VALUES
( ( SELECT role _id FROM project _owner ) , '${p.name}' , 'UPDATE_PROJECT' ) ,
( ( SELECT role _id FROM project _owner ) , '${p.name}' , 'DELETE_PROJECT' ) ,
( ( SELECT role _id FROM project _owner ) , '${p.name}' , 'CREATE_FEATURE' ) ,
( ( SELECT role _id FROM project _owner ) , '${p.name}' , 'UPDATE_FEATURE' ) ,
( ( SELECT role _id FROM project _owner ) , '${p.name}' , 'DELETE_FEATURE' ) ;
WITH project _member AS (
INSERT into roles ( name , description , type , project )
VALUES ( 'Member' , '${DESCRIPTION.MEMBER}' , 'project' , '${p.name}' )
RETURNING id role _id
)
INSERT INTO role _permission ( role _id , project , permission ) VALUES
( ( SELECT role _id from project _member ) , '${p.name}' , 'CREATE_FEATURE' ) ,
( ( SELECT role _id from project _member ) , '${p.name}' , 'UPDATE_FEATURE' ) ,
( ( SELECT role _id from project _member ) , '${p.name}' , 'DELETE_FEATURE' ) ;
WITH owner _id AS (
SELECT id FROM roles WHERE type = 'project' AND project = '${p.name}' AND name = 'Owner'
)
INSERT INTO role _user ( role _id , user _id ) SELECT o . id , u . id FROM owner _id o , users u ON CONFLICT DO NOTHING ;
` ,
) ,
) ;
async . series ( createProjectRoles , cb ) ;
} else {
cb ( ) ;
}
} ,
) ;
} ;
2021-08-12 15:04:37 +02:00
exports . down = function ( db , cb ) {
2021-04-30 13:04:25 +02:00
cb ( ) ; // Can't really roll this back since more roles could have been added afterwards
} ;