public function beforeExecuteRoute(Event $event, Dispatcher $dispatcher) { try { // read class annotation $class_annotation = $this->annotations->get($dispatcher->getHandlerClass())->getClassAnnotations(); $api_annotation = $class_annotation->get("Api"); // read method annotation $method_annotation = $this->annotations->getMethod($dispatcher->getHandlerClass(), $dispatcher->getActiveMethod()); $engine = new SecurityEngine(); // check API key $key = $engine->checkKeyLevel($this->request->getHeader("HTTP_X_API_KEY"), $api_annotation); // check authentication if exist $engine->checkAuth($method_annotation); // check IP whitelist $engine->checkWhitelist($method_annotation); $hasLimit = $api_annotation->hasNamedArgument("limits") || $method_annotation->has("Limit"); // check limit if (!$key->getIgnoreLimit() && $hasLimit) { $engine->checkKeyLimitOnClass($key, $api_annotation->getNamedArgument("limits")); $engine->checkMethodLimitByKey($key, $method_annotation->get("Limit")->getArguments()); } // write logs to db $engine->log($key->getApiKeyId(), $this->request->getClientAddress(), $this->request->getMethod(), $this->request->get("_url")); } catch (PhalconException $e) { $this->apiResponse->withError($e->getMessage(), $e->getCode()); return false; } return true; }
/** * This event is executed before every route is executed in the dispatcher. * * @param Event $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return bool */ public function beforeExecuteRoute($event, $dispatcher) { // Parse the annotations in the method currently executed. $annotations = $this->annotations->getMethod($dispatcher->getActiveController(), $dispatcher->getActiveMethod()); // Check if the method has an annotation 'Cache'. if ($annotations->has('Cache')) { // The method has the annotation 'Cache'. /** @var \Phalcon\Annotations\Annotation $annotation */ $annotation = $annotations->get('Cache'); // Get the lifetime. $lifetime = $annotation->getNamedArgument('lifetime'); $options = ['lifetime' => $lifetime]; // Check if there is a user defined cache key. if ($annotation->hasNamedArgument('key')) { $options['key'] = $annotation->getNamedArgument('key'); } // Enable the cache for the current method. $this->view->cache($options); } return !$event->isStopped(); }