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:
parent
8c4a6a1e18
commit
517f3e2170
39
src/migrations/20210428062103-user-permission-to-rbac.js
Normal file
39
src/migrations/20210428062103-user-permission-to-rbac.js
Normal 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();
|
||||
};
|
Loading…
Reference in New Issue
Block a user