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

fix: define root role by setting the name of the role (#823)

This commit is contained in:
Ivar Conradi Østhus 2021-04-30 13:25:24 +02:00 committed by GitHub
parent dd8e9207ad
commit 0efc238fdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 11 deletions

View File

@ -159,9 +159,11 @@ export class AccessService {
return this.store.addUserToRole(userId, roleId); return this.store.addUserToRole(userId, roleId);
} }
async setUserRootRole(userId: number, roleId: number): Promise<void> { async setUserRootRole(
const roles = await this.getRootRoles(); userId: number,
const newRootRole = roles.find(r => r.id === roleId); role: number | RoleName,
): Promise<void> {
const newRootRole = await this.resolveRootRole(role);
if (newRootRole) { if (newRootRole) {
try { try {
@ -176,7 +178,7 @@ export class AccessService {
); );
} }
} else { } else {
throw new Error(`Could not find rootRole with id=${roleId}`); throw new Error(`Could not find rootRole=${role}`);
} }
} }
@ -316,6 +318,17 @@ export class AccessService {
return this.store.getRootRoles(); return this.store.getRootRoles();
} }
private async resolveRootRole(rootRole: number | RoleName): Promise<IRole> {
const rootRoles = await this.getRootRoles();
let role: IRole;
if (typeof rootRole === 'number') {
role = rootRoles.find(r => r.id === rootRole);
} else {
role = rootRoles.find(r => r.name === rootRole);
}
return role;
}
async getRootRole(roleName: RoleName): Promise<IRole> { async getRootRole(roleName: RoleName): Promise<IRole> {
const roles = await this.store.getRootRoles(); const roles = await this.store.getRootRoles();
return roles.find(r => r.name === roleName); return roles.find(r => r.name === roleName);

View File

@ -21,6 +21,7 @@ import { IUnleashStores } from '../types/stores';
import PasswordUndefinedError from '../error/password-undefined'; import PasswordUndefinedError from '../error/password-undefined';
import EventStore from '../db/event-store'; import EventStore from '../db/event-store';
import { USER_UPDATED, USER_CREATED, USER_DELETED } from '../types/events'; import { USER_UPDATED, USER_CREATED, USER_DELETED } from '../types/events';
import { IRole } from '../db/access-store';
const systemUser = new User({ id: -1, username: 'system' }); const systemUser = new User({ id: -1, username: 'system' });
@ -29,14 +30,14 @@ export interface ICreateUser {
email?: string; email?: string;
username?: string; username?: string;
password?: string; password?: string;
rootRole: number; rootRole: number | RoleName;
} }
export interface IUpdateUser { export interface IUpdateUser {
id: number; id: number;
name?: string; name?: string;
email?: string; email?: string;
rootRole?: number; rootRole?: number | RoleName;
} }
interface IUserWithRole extends IUser { interface IUserWithRole extends IUser {
@ -128,11 +129,7 @@ class UserService {
const passwordHash = await bcrypt.hash(pwd, saltRounds); const passwordHash = await bcrypt.hash(pwd, saltRounds);
await this.store.setPasswordHash(user.id, passwordHash); await this.store.setPasswordHash(user.id, passwordHash);
const rootRoles = await this.accessService.getRootRoles(); await this.accessService.setUserRootRole(user.id, RoleName.ADMIN);
const adminRole = rootRoles.find(
r => r.name === RoleName.ADMIN,
);
await this.accessService.setUserRootRole(user.id, adminRole.id);
} catch (e) { } catch (e) {
this.logger.error('Unable to create default user "admin"'); this.logger.error('Unable to create default user "admin"');
} }

View File

@ -97,6 +97,19 @@ test.serial('should get user with root role', async t => {
t.is(user.rootRole, adminRole.id); t.is(user.rootRole, adminRole.id);
}); });
test.serial('should get user with root role by name', async t => {
const email = 'some2@test.com';
const u = await userService.createUser({
email,
password: 'A very strange P4ssw0rd_',
rootRole: RoleName.ADMIN,
});
const user = await userService.getUser(u.id);
t.is(user.email, email);
t.is(user.id, u.id);
t.is(user.rootRole, adminRole.id);
});
test.serial(`deleting a user should delete the user's sessions`, async t => { test.serial(`deleting a user should delete the user's sessions`, async t => {
const email = 'some@test.com'; const email = 'some@test.com';
const user = await userService.createUser({ const user = await userService.createUser({