|
| 1 | +import {Request} from "../Request.js"; |
| 2 | +import {Response, ThrowableResponse} from "../response/index.js"; |
| 3 | +import {ServerErrorRegistry} from "../ServerErrorRegistry.js"; |
1 | 4 | import {Authorisation} from "./Authorisation.js"; |
2 | 5 | import {Permissible} from "./Permissible.js"; |
3 | 6 | import {Permission} from "./Permission.js"; |
4 | | -import {Request} from "../Request.js"; |
5 | 7 |
|
6 | 8 | /** |
7 | 9 | * A request with available {@link Authorisation}. |
@@ -32,4 +34,36 @@ export class AuthenticatedRequest<A> extends Request<A> implements Permissible { |
32 | 34 | public has(permission: Permission): boolean { |
33 | 35 | return this.authorisation.has(permission); |
34 | 36 | } |
| 37 | + |
| 38 | + /** |
| 39 | + * Require the request to have all the specified permissions. |
| 40 | + * @param permissions The required permission. |
| 41 | + * @param [response] Throw this response if the request does not have the permission. Defaults to 403 from |
| 42 | + * {@link ServerErrorRegistry}. |
| 43 | + * @throws {@link ThrowableResponse} If the request does not have the permission. |
| 44 | + */ |
| 45 | + public require(permissions: Iterable<Permission>, response?: Response<A>): void; |
| 46 | + |
| 47 | + /** |
| 48 | + * Require the request to have the specified permission. |
| 49 | + * @param permission The required permission. |
| 50 | + * @param [response] Throw this response if the request does not have the permission. Defaults to 403 from |
| 51 | + * {@link ServerErrorRegistry}. |
| 52 | + * @throws {@link ThrowableResponse} If the request does not have the permission. |
| 53 | + */ |
| 54 | + public require(permission: Permission, response?: Response<A>): void; |
| 55 | + public require(required: Permission | Iterable<Permission>, response?: Response<A>): void { |
| 56 | + if (required instanceof Permission) { |
| 57 | + if (!this.has(required)) |
| 58 | + throw new ThrowableResponse( |
| 59 | + response ?? this.server.errors._get(ServerErrorRegistry.ErrorCodes.NO_PERMISSION, this) |
| 60 | + ); |
| 61 | + } |
| 62 | + else for (const permission of required) { |
| 63 | + if (!this.has(permission)) |
| 64 | + throw new ThrowableResponse( |
| 65 | + response ?? this.server.errors._get(ServerErrorRegistry.ErrorCodes.NO_PERMISSION, this) |
| 66 | + ); |
| 67 | + } |
| 68 | + } |
35 | 69 | } |
0 commit comments