Skip to content

Commit

Permalink
feat: Validation now works when updating a role
Browse files Browse the repository at this point in the history
  • Loading branch information
sighphyre authored and ivarconr committed Jan 11, 2022
1 parent 84f4a90 commit 4e3f7e5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
12 changes: 11 additions & 1 deletion src/lib/db/role-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -23,7 +24,7 @@ interface IRoleRow {
type: string;
}

export default class RoleStore {
export default class RoleStore implements IRoleStore {
private logger: Logger;

private eventBus: EventEmitter;
Expand Down Expand Up @@ -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`,
Expand Down
16 changes: 11 additions & 5 deletions src/lib/services/access-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}`,
Expand All @@ -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...
}
}
2 changes: 1 addition & 1 deletion src/lib/types/stores/role-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 1 addition & 1 deletion src/test/fixtures/fake-role-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

Expand Down

0 comments on commit 4e3f7e5

Please sign in to comment.