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:
parent
84f4a9083f
commit
4e3f7e5330
@ -6,6 +6,7 @@ import { ICustomRole } from 'lib/types/model';
|
||||
import {
|
||||
ICustomRoleInsert,
|
||||
ICustomRoleUpdate,
|
||||
IRoleStore,
|
||||
} from 'lib/types/stores/role-store';
|
||||
import { IRole, IUserRole } from 'lib/types/stores/access-store';
|
||||
|
||||
@ -23,7 +24,7 @@ interface IRoleRow {
|
||||
type: string;
|
||||
}
|
||||
|
||||
export default class RoleStore {
|
||||
export default class RoleStore implements IRoleStore {
|
||||
private logger: Logger;
|
||||
|
||||
private eventBus: EventEmitter;
|
||||
@ -88,6 +89,15 @@ export default class RoleStore {
|
||||
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> {
|
||||
const result = await this.db.raw(
|
||||
`SELECT EXISTS (SELECT 1 FROM ${T.ROLES} WHERE name = ?) AS present`,
|
||||
|
@ -409,7 +409,7 @@ export class AccessService {
|
||||
}
|
||||
|
||||
async updateRole(role: IRoleUpdate): Promise<ICustomRole> {
|
||||
// await this.validateRole(role);
|
||||
await this.validateRole(role, role.id);
|
||||
const baseRole = {
|
||||
id: role.id,
|
||||
name: role.name,
|
||||
@ -432,8 +432,11 @@ export class AccessService {
|
||||
return this.roleStore.delete(id);
|
||||
}
|
||||
|
||||
async validateRoleIsUnique(roleName: string): Promise<void> {
|
||||
const exists = await this.roleStore.roleExists(roleName);
|
||||
async validateRoleIsUnique(
|
||||
roleName: string,
|
||||
existingId?: number,
|
||||
): Promise<void> {
|
||||
const exists = await this.roleStore.nameInUse(roleName, existingId);
|
||||
if (exists) {
|
||||
throw new NameExistsError(
|
||||
`There already exists a role with the name ${roleName}`,
|
||||
@ -442,8 +445,11 @@ export class AccessService {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
async validateRole(role: IRoleCreation): Promise<void> {
|
||||
await this.validateRoleIsUnique(role.name);
|
||||
async validateRole(
|
||||
role: IRoleCreation,
|
||||
existingId?: number,
|
||||
): Promise<void> {
|
||||
await this.validateRoleIsUnique(role.name, existingId);
|
||||
//Handle schema validation here...
|
||||
}
|
||||
}
|
||||
|
@ -27,5 +27,5 @@ export interface IRoleStore extends Store<ICustomRole, number> {
|
||||
getProjectRoles(): Promise<IRole[]>;
|
||||
getRootRoles(): Promise<IRole[]>;
|
||||
getRootRoleForAllUsers(): Promise<IUserRole[]>;
|
||||
roleExists(name: string): Promise<boolean>;
|
||||
nameInUse(name: string, existingId: number): Promise<boolean>;
|
||||
}
|
||||
|
2
src/test/fixtures/fake-role-store.ts
vendored
2
src/test/fixtures/fake-role-store.ts
vendored
@ -8,7 +8,7 @@ import {
|
||||
} from 'lib/types/stores/role-store';
|
||||
|
||||
export default class FakeRoleStore implements IRoleStore {
|
||||
roleExists(name: string): Promise<boolean> {
|
||||
nameInUse(name: string, existingId: number): Promise<boolean> {
|
||||
throw new Error('Method not implemented.');
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user