1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

fix: migration to create root roles for users with permissions (#816)

* fix: migration to create root roles for users with permissions

needed to migrate enterprise users to rbac.

* fix: callback
This commit is contained in:
Ivar Conradi Østhus 2021-04-28 11:48:21 +02:00 committed by GitHub
parent 8c4a6a1e18
commit 517f3e2170
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -0,0 +1,39 @@
'use strict';
const async = require('async');
function resolveRoleName(permissions = []) {
if (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;`, (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();
};