1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/src/migrations/20210428062103-user-permission-to-rbac.js
Christopher Kolstad ff7be7696c
fix: Stores as typescript and with interfaces. (#902)
Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
2021-08-12 15:04:37 +02:00

43 lines
1.2 KiB
JavaScript

'use strict';
const async = require('async');
function resolveRoleName(permissions) {
if (!permissions || permissions.length === 0) {
return 'Viewer';
}
if (permissions.includes('ADMIN')) {
return 'Admin';
}
return 'Editor';
}
exports.up = function (db, cb) {
db.runSql(
'SELECT id, permissions from users WHERE id NOT IN (select user_id from role_user);',
(err, results) => {
if (results.rowCount > 0) {
const users = results.rows;
const insertRootRole = users.map((u) => {
const roleName = resolveRoleName(u.permissions);
return db.runSql.bind(
db,
`INSERT INTO role_user (role_id, user_id)
SELECT id, '${u.id}'
FROM roles
WHERE name = '${roleName}' AND type = 'root';`,
);
});
async.series(insertRootRole, cb);
} else {
cb();
}
},
);
};
exports.down = function (db, cb) {
// We can't just remove roles for users as we don't know if there has been any manual additions.
cb();
};