mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-12 13:48:35 +02:00
fix: Improve responses from role resolution - getting a non existant role will throw a NotFound error
This commit is contained in:
parent
40bdab773c
commit
f032445d3d
@ -10,6 +10,7 @@ import {
|
|||||||
} from '../types/stores/access-store';
|
} from '../types/stores/access-store';
|
||||||
import { IPermission } from 'lib/types/model';
|
import { IPermission } from 'lib/types/model';
|
||||||
import { roundToNearestMinutesWithOptions } from 'date-fns/fp';
|
import { roundToNearestMinutesWithOptions } from 'date-fns/fp';
|
||||||
|
import NotFoundError from '../error/notfound-error';
|
||||||
|
|
||||||
const T = {
|
const T = {
|
||||||
ROLE_USER: 'role_user',
|
ROLE_USER: 'role_user',
|
||||||
@ -67,11 +68,17 @@ export class AccessStore implements IAccessStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async get(key: number): Promise<IRole> {
|
async get(key: number): Promise<IRole> {
|
||||||
return this.db
|
const role = await this.db
|
||||||
.select(['id', 'name', 'type', 'description'])
|
.select(['id', 'name', 'type', 'description'])
|
||||||
.where('id', key)
|
.where('id', key)
|
||||||
.first()
|
.first()
|
||||||
.from<IRole>(T.ROLES);
|
.from<IRole>(T.ROLES);
|
||||||
|
|
||||||
|
if (!role) {
|
||||||
|
throw new NotFoundError(`Could not find role with id: ${key}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return role;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAll(): Promise<IRole[]> {
|
async getAll(): Promise<IRole[]> {
|
||||||
@ -85,42 +92,6 @@ export class AccessStore implements IAccessStore {
|
|||||||
.orWhere('type', 'environment')
|
.orWhere('type', 'environment')
|
||||||
.from(`${T.PERMISSIONS} as p`);
|
.from(`${T.PERMISSIONS} as p`);
|
||||||
return rows.map(this.mapPermission);
|
return rows.map(this.mapPermission);
|
||||||
// .map(mapPermission)
|
|
||||||
// const rows = await this.db
|
|
||||||
// .select(['p.id', 'p.permission', 'p.environment', 'p.display_name'])
|
|
||||||
// .join(`${T.ROLE_PERMISSION} AS rp`, 'rp.permission_id', 'p.id')
|
|
||||||
// .where('pt.type', 'project')
|
|
||||||
// .orWhere('pt.type', 'environment')
|
|
||||||
// .from(`${T.PERMISSIONS} as p`);
|
|
||||||
|
|
||||||
// let projectPermissions: IPermission[] = [];
|
|
||||||
// let rawEnvironments = new Map<string, IPermissionRow[]>();
|
|
||||||
|
|
||||||
// for (let permission of rows) {
|
|
||||||
// if (!permission.environment) {
|
|
||||||
// projectPermissions.push(this.mapPermission(permission));
|
|
||||||
// } else {
|
|
||||||
// if (!rawEnvironments.get(permission.environment)) {
|
|
||||||
// rawEnvironments.set(permission.environment, []);
|
|
||||||
// }
|
|
||||||
// rawEnvironments.get(permission.environment).push(permission);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// let allEnvironmentPermissions: Array<IEnvironmentPermission> =
|
|
||||||
// Array.from(rawEnvironments).map(
|
|
||||||
// ([environmentName, environmentPermissions]) => {
|
|
||||||
// return {
|
|
||||||
// name: environmentName,
|
|
||||||
// permissions: environmentPermissions.map(
|
|
||||||
// this.mapPermission,
|
|
||||||
// ),
|
|
||||||
// };
|
|
||||||
// },
|
|
||||||
// );
|
|
||||||
// return {
|
|
||||||
// project: projectPermissions,
|
|
||||||
// environments: allEnvironmentPermissions,
|
|
||||||
// };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mapPermission(permission: IPermissionRow): IPermission {
|
mapPermission(permission: IPermissionRow): IPermission {
|
||||||
|
@ -63,6 +63,9 @@ export default class RoleStore implements IRoleStore {
|
|||||||
|
|
||||||
async get(id: number): Promise<ICustomRole> {
|
async get(id: number): Promise<ICustomRole> {
|
||||||
const rows = await this.db.select(COLUMNS).from(T.ROLES).where({ id });
|
const rows = await this.db.select(COLUMNS).from(T.ROLES).where({ id });
|
||||||
|
if (rows.length === 0) {
|
||||||
|
throw new NotFoundError(`Could not find role with id: ${id}`);
|
||||||
|
}
|
||||||
return this.mapRow(rows[0]);
|
return this.mapRow(rows[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user