FatFractal customer forums



Author Topic: Event Handler Questions  (Read 1490 times)

metakite

  • Jr. Member
  • **
  • Posts: 54
    • View Profile
Event Handler Questions
« on: March 11, 2014, 09:39:38 AM »
I have a couple questions about event handlers.

1. Can I create an event handler for a retrieve operation? The documentation at http://fatfractal.com/prod/docs/custom-code/#event-handlers seems to indicate that CRUD event handlers are only supported for create, update, and delete operations. The documentation at http://fatfractal.com/prod/docs/reference/#ffdl, though, seems to indicate that retrieve handlers are permitted as well. Can you clarify which is correct?

2. I want to create a create an event handler that fires before an update is performed to check if the user has permission to modify the record (basically checking if the user's subscription is current). If the user should not be allowed to make the change, how do I exit the handler in a way that the update does not complete? Just throw an exception? Should a particular object be thrown?

jonnycools

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Event Handler Questions
« Reply #1 on: March 11, 2014, 09:59:28 AM »
!. The documentation isn't quite clear about this, I suspect one portion is outdated. However from experience retrieve handlers are supported, but are only POST. Meaning after it has been retrieved but before its returned the user

Code: [Select]

CREATE HANDLER handlerName POST ON /<Collection> Retrieve AS javascript:require('scripts/<fileNameWithoutTheDotjs>').functionName();


2. I believe you just through an exception

PS. This should be a PRE handler, not sure if POST would work here as the server does have a rollback feature for failed handlers

Code: [Select]

exports.updateCollection = function()
{

        var authorized = ...;

if (!authorized)
{

throw {statusCode:405, statusMessage:"user not allowed to edit object."};

}

}

« Last Edit: March 11, 2014, 10:11:22 AM by jonnycools »

gkc

  • Administrator
  • *****
  • Posts: 375
    • View Profile
Re: Event Handler Questions
« Reply #2 on: March 11, 2014, 02:19:51 PM »
1) You can indeed create a retrieve event handler. However, BEWARE. Currently that event handler will fire EVERY time you retrieve from the collection, for any reason whatsoever. There is a fix in one of my working branches which makes it fire only for requests which originate from the web.

2) Exactly as @jonnycools says except I would throw with statusCode 403 rather than 405 - see http://en.wikipedia.org/wiki/HTTP_403

 

Copyright © FatFractal customer forums