UNPKG

@webda/core

Version:

Expose API with Lambda

65 lines 1.8 kB
import { UuidModel } from "./coremodel.js"; import { ModelLink } from "./relations.js"; import { User } from "./user.js"; /** * @WebdaModel */ export class OwnerModel extends UuidModel { /** * Set object owner * @param uuid */ setOwner(uuid) { this._user ?? (this._user = new ModelLink(uuid, User, this)); this._user.set(uuid); } /** * @override */ validate(ctx, updates, ignoreRequired) { updates._user ?? (updates._user = this._user?.toString()); updates.uuid ?? (updates.uuid = this.generateUid()); return super.validate(ctx, updates, ignoreRequired); } /** * Return the owner of the object * * Only the owner can do update to the object * @returns */ getOwner() { return this._user; } async canAct(ctx, action) { // Object is public if (this.public && (action === "get" || action === "get_binary")) { return true; } else if (!ctx.getCurrentUserId()) { return "You need to be logged in to access this object"; } else if (!this.getOwner() && action !== "create") { return "Object does not have an owner"; } if (action === "create") { this.setOwner(ctx.getCurrentUserId()); } return ctx.getCurrentUserId() === this.getOwner()?.toString(); } /** * Return a query to filter OwnerModel * * @param context * @returns */ static getPermissionQuery(context) { if (!context) { return null; } return { query: `_user = '${context.getCurrentUserId()}' OR public = TRUE`, partial: false }; } } //# sourceMappingURL=ownermodel.js.map