library-ts/node/users/requirements/user/UserHasPermission.ts

31 lines
788 B
TypeScript
Raw Normal View History

2025-11-10 17:41:48 +00:00
import { FastifyRequest, FastifyReply } from 'fastify';
import { RequestRequirement } from '../RequestRequirement';
2025-11-11 21:46:18 +00:00
import { Message } from '../../../../browser/messages/Message';
2025-11-10 17:41:48 +00:00
export class UserHasPermission extends RequestRequirement
{
private _permissionID:string = "";
constructor( permissionID:string )
{
super();
this._permissionID = permissionID;
}
2025-11-11 21:46:18 +00:00
async handle( request:FastifyRequest, reply:FastifyReply ):Promise<Message[]>
2025-11-10 17:41:48 +00:00
{
2025-11-15 18:58:30 +00:00
let user = await this.ums.getUser( request );
2025-11-10 17:41:48 +00:00
2025-11-15 18:58:30 +00:00
let userDB = this.ums.userDB;
2025-11-10 17:41:48 +00:00
2025-11-11 21:46:18 +00:00
let hasPermission = await userDB.hasPermission( user, this._permissionID );
if ( ! hasPermission )
{
2025-11-15 18:58:30 +00:00
return this.sendError( "User has no permission for " + this._permissionID );
2025-11-11 21:46:18 +00:00
}
2025-11-15 18:58:30 +00:00
return this.giveOK();
2025-11-10 17:41:48 +00:00
}
}