46 lines
1.2 KiB
JavaScript
46 lines
1.2 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = {
|
|
async up(queryInterface) {
|
|
const sequelize = queryInterface.sequelize;
|
|
const [roles] = await sequelize.query(
|
|
`SELECT "id" FROM "roles" WHERE "name" = 'Administrator' LIMIT 1;`,
|
|
);
|
|
|
|
if (!roles.length) {
|
|
return;
|
|
}
|
|
|
|
const [permissions] = await sequelize.query(
|
|
`SELECT "id", "name" FROM "permissions" WHERE "name" IN ('READ_TENANTS', 'READ_ORGANIZATIONS');`,
|
|
);
|
|
|
|
const now = new Date();
|
|
|
|
for (const permission of permissions) {
|
|
await sequelize.query(
|
|
`INSERT INTO "rolesPermissionsPermissions" ("createdAt", "updatedAt", "roles_permissionsId", "permissionId")
|
|
SELECT :createdAt, :updatedAt, :roleId, :permissionId
|
|
WHERE NOT EXISTS (
|
|
SELECT 1
|
|
FROM "rolesPermissionsPermissions"
|
|
WHERE "roles_permissionsId" = :roleId
|
|
AND "permissionId" = :permissionId
|
|
);`,
|
|
{
|
|
replacements: {
|
|
createdAt: now,
|
|
updatedAt: now,
|
|
roleId: roles[0].id,
|
|
permissionId: permission.id,
|
|
},
|
|
},
|
|
);
|
|
}
|
|
},
|
|
|
|
async down() {
|
|
// Intentionally left blank. This protects live permission assignments.
|
|
},
|
|
};
|