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

feat: Validation now works when updating a role

This commit is contained in:
sighphyre 2022-01-04 13:10:45 +02:00 committed by Ivar Conradi Østhus
parent 84f4a9083f
commit 4e3f7e5330
No known key found for this signature in database
GPG Key ID: 31AC596886B0BD09
4 changed files with 24 additions and 8 deletions

View File

@ -6,6 +6,7 @@ import { ICustomRole } from 'lib/types/model';
import { import {
ICustomRoleInsert, ICustomRoleInsert,
ICustomRoleUpdate, ICustomRoleUpdate,
IRoleStore,
} from 'lib/types/stores/role-store'; } from 'lib/types/stores/role-store';
import { IRole, IUserRole } from 'lib/types/stores/access-store'; import { IRole, IUserRole } from 'lib/types/stores/access-store';
@ -23,7 +24,7 @@ interface IRoleRow {
type: string; type: string;
} }
export default class RoleStore { export default class RoleStore implements IRoleStore {
private logger: Logger; private logger: Logger;
private eventBus: EventEmitter; private eventBus: EventEmitter;
@ -88,6 +89,15 @@ export default class RoleStore {
return present; return present;
} }
async nameInUse(name: string, existingId?: number): Promise<boolean> {
let query = this.db(T.ROLES).where({ name }).returning('id');
if (existingId) {
query = query.andWhereNot({ id: existingId });
}
const result = await query;
return result.length > 0;
}
async roleExists(name: string): Promise<boolean> { async roleExists(name: string): Promise<boolean> {
const result = await this.db.raw( const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${T.ROLES} WHERE name = ?) AS present`, `SELECT EXISTS (SELECT 1 FROM ${T.ROLES} WHERE name = ?) AS present`,

View File

@ -409,7 +409,7 @@ export class AccessService {
} }
async updateRole(role: IRoleUpdate): Promise<ICustomRole> { async updateRole(role: IRoleUpdate): Promise<ICustomRole> {
// await this.validateRole(role); await this.validateRole(role, role.id);
const baseRole = { const baseRole = {
id: role.id, id: role.id,
name: role.name, name: role.name,
@ -432,8 +432,11 @@ export class AccessService {
return this.roleStore.delete(id); return this.roleStore.delete(id);
} }
async validateRoleIsUnique(roleName: string): Promise<void> { async validateRoleIsUnique(
const exists = await this.roleStore.roleExists(roleName); roleName: string,
existingId?: number,
): Promise<void> {
const exists = await this.roleStore.nameInUse(roleName, existingId);
if (exists) { if (exists) {
throw new NameExistsError( throw new NameExistsError(
`There already exists a role with the name ${roleName}`, `There already exists a role with the name ${roleName}`,
@ -442,8 +445,11 @@ export class AccessService {
return Promise.resolve(); return Promise.resolve();
} }
async validateRole(role: IRoleCreation): Promise<void> { async validateRole(
await this.validateRoleIsUnique(role.name); role: IRoleCreation,
existingId?: number,
): Promise<void> {
await this.validateRoleIsUnique(role.name, existingId);
//Handle schema validation here... //Handle schema validation here...
} }
} }

View File

@ -27,5 +27,5 @@ export interface IRoleStore extends Store<ICustomRole, number> {
getProjectRoles(): Promise<IRole[]>; getProjectRoles(): Promise<IRole[]>;
getRootRoles(): Promise<IRole[]>; getRootRoles(): Promise<IRole[]>;
getRootRoleForAllUsers(): Promise<IUserRole[]>; getRootRoleForAllUsers(): Promise<IUserRole[]>;
roleExists(name: string): Promise<boolean>; nameInUse(name: string, existingId: number): Promise<boolean>;
} }

View File

@ -8,7 +8,7 @@ import {
} from 'lib/types/stores/role-store'; } from 'lib/types/stores/role-store';
export default class FakeRoleStore implements IRoleStore { export default class FakeRoleStore implements IRoleStore {
roleExists(name: string): Promise<boolean> { nameInUse(name: string, existingId: number): Promise<boolean> {
throw new Error('Method not implemented.'); throw new Error('Method not implemented.');
} }