1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-15 01:16:22 +02:00

fix: all users have a root role and warning if not

This commit is contained in:
Gastón Fournier 2025-03-20 12:22:14 +01:00
parent 038c10f612
commit 606bd87e94
No known key found for this signature in database
GPG Key ID: AF45428626E17A8E
2 changed files with 22 additions and 2 deletions

View File

@ -457,8 +457,9 @@ export class AccessService {
async getRootRoleForUser(userId: number): Promise<IRole> {
const rootRole = await this.store.getRootRoleForUser(userId);
if (!rootRole) {
const defaultRole = await this.getPredefinedRole(RoleName.VIEWER);
return defaultRole;
// this should never happen, but before breaking we want to know if it does.
this.logger.warn(`Could not find root role for user=${userId}.`);
return this.getPredefinedRole(RoleName.VIEWER);
}
return rootRole;
}

View File

@ -0,0 +1,19 @@
exports.up = function (db, cb) {
// add root role Viewer (id 3) to all users who don't have a root role
db.runSql(
`INSERT INTO role_user(role_id, user_id, project) SELECT 3, u.id, 'default'
FROM users u
WHERE u.id > 0 AND u.deleted_at IS NULL AND NOT EXISTS (
SELECT 1
FROM role_user ru
JOIN roles r ON ru.role_id = r.id
WHERE ru.user_id = u.id
AND r.type IN ('root', 'root-custom')
);`,
cb,
);
};
exports.down = function (db, cb) {
// No rollback
};