public function authCheck(Event $event) { if (isset($this->earlyAuthTest)) { if ($this->_config['checkAuthIn'] !== $event->name()) { return; } $this->authCheckCalledFrom = $event->name(); return; } return parent::authCheck($event); }
public function afterRules(Cake\Event\Event $event, Cat $entity, \ArrayObject $options, $result, $operation) { Log::write("debug", "afterRules"); Log::write("debug", $event->name()); Log::write("debug", "entity " . $entity); Log::write("debug", $options); Log::write("debug", "result " . $result); Log::write("debug", "operation " . $operation); }
/** * There is only one event handler, it can be configured to be called for any event * * @param \Cake\Event\Event $event Event instance. * @param \Cake\Datasource\EntityInterface $entity Entity instance. * @throws \UnexpectedValueException if a field's when value is misdefined * @return true (irrespective of the behavior logic, the save will not be prevented) * @throws \UnexpectedValueException When the value for an event is not 'always', 'new' or 'existing' */ public function handleEvent(Event $event, EntityInterface $entity) { $eventName = $event->name(); $events = $this->_config['events']; if ($events[$eventName] === true) { $this->_purify($entity); } return true; }
/** * Try and attach footprint listener to models. * * It also passing the user record to footprint listener after user is * identified by AuthComponent. * * @param \Cake\Event\Event $event Event. * @return void */ public function footprint(Event $event) { if (!$this->_listener) { $this->_listener = new FootprintListener($this->_getCurrentUser()); } if ($event->name() === 'Auth.afterIdentify') { $this->_listener->setUser($this->_getCurrentUser($event->data)); return; } $event->subject()->eventManager()->attach($this->_listener); }
/** * There is only one event handler, it can be configured to be called for any event * * @param \Cake\Event\Event $event Event instance. * @param \Cake\ORM\Entity $entity Entity instance. * @throws \UnexpectedValueException if a field's when value is misdefined * @return true (irrespective of the behavior logic, the save will not be prevented) * @throws \UnexpectedValueException When the value for an event is not 'always', 'new' or 'existing' */ public function handleEvent(Event $event, Entity $entity) { $eventName = $event->name(); $events = $this->config('events'); $new = $entity->isNew() !== false; foreach ($events[$eventName] as $field => $when) { if (!in_array($when, ['always', 'new', 'existing'])) { throw new \UnexpectedValueException(sprintf('When should be one of "always", "new" or "existing". The passed value "%s" is invalid', $when)); } if ($when === 'always' || $when === 'new' && $new || $when === 'existing' && !$new) { $this->_updateField($entity, $field); } } return true; }
/** * Injects configured field values into entity if those fields are not dirty. * * @param \Cake\Event\Event $event Event. * @param \Cake\ORM\Entity $entity Entity. * @param \ArrayObject $options Options. * @return void */ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) { $eventName = $event->name(); $events = $this->config('events'); $new = $entity->isNew() !== false; foreach ($events[$eventName] as $field => $when) { if (!in_array($when, ['always', 'new', 'existing'])) { throw new UnexpectedValueException(sprintf('When should be one of "always", "new" or "existing". The passed value "%s" is invalid', $when)); } if ($entity->dirty($field)) { continue; } if ($when === 'always' || $when === 'new' && $new || $when === 'existing' && !$new) { $entity->set($field, current(Hash::extract((array) $options, $this->config('propertiesMap.' . $field)))); } } }
/** * @param Event $event * @return string */ public function onView(Event $event) { return $event->name(); }
/** * Main execution method, handles initial authentication check and redirection * of invalid users. * * The auth check is done when event name is same as the one configured in * `checkAuthIn` config. * * @param \Cake\Event\Event $event Event instance. * @return \Cake\Network\Response|null */ public function authCheck(Event $event) { if ($this->_config['checkAuthIn'] !== $event->name()) { return null; } $controller = $event->subject(); $action = strtolower($controller->request->params['action']); if (!$controller->isAction($action)) { return null; } $this->_setDefaults(); if ($this->_isAllowed($controller)) { return null; } $isLoginAction = $this->_isLoginAction($controller); if (!$this->_getUser()) { if ($isLoginAction) { return null; } $result = $this->_unauthenticated($controller); if ($result instanceof Response) { $event->stopPropagation(); } return $result; } if ($isLoginAction || empty($this->_config['authorize']) || $this->isAuthorized($this->user())) { return null; } $event->stopPropagation(); return $this->_unauthorized($controller); }
/** * Dispatches a new event to all configured listeners * * @param string|\Cake\Event\Event $event the event key name or instance of Event * @return \Cake\Event\Event * @triggers $event */ public function dispatch($event) { if (is_string($event)) { $event = new Event($event); } $listeners = $this->listeners($event->name()); if (empty($listeners)) { return $event; } foreach ($listeners as $listener) { if ($event->isStopped()) { break; } $result = $this->_callListener($listener['callable'], $event); if ($result === false) { $event->stopPropagation(); } if ($result !== null) { $event->result = $result; } } return $event; }
/** * Handler method that applies conditions and resolves the correct method to call. * * @param \Cake\Event\Event $event The event instance. * @return mixed */ public function handle(Event $event) { $name = $event->name(); list(, $method) = explode('.', $name); if (empty($this->_config['for']) && empty($this->_config['when'])) { return $this->{$method}($event); } if ($this->matches($event)) { return $this->{$method}($event); } }
/** * Dispatches a new event to all configured listeners * * @param string|\Cake\Event\Event $event the event key name or instance of Event * @return \Cake\Event\Event * @triggers $event */ public function dispatch($event) { if (is_string($event)) { $event = new Event($event); } $listeners = $this->listeners($event->name()); if ($this->_trackEvents) { $this->addEventToList($event); } if (!$this->_isGlobal && static::instance()->isTrackingEvents()) { static::instance()->addEventToList($event); } if (empty($listeners)) { return $event; } foreach ($listeners as $listener) { if ($event->isStopped()) { break; } $result = $this->_callListener($listener['callable'], $event); if ($result === false) { $event->stopPropagation(); } if ($result !== null) { $event->result = $result; } } return $event; }
/** * Tests the name() method * * @return void * @triggers fake.event */ public function testName() { $event = new Event('fake.event'); $this->assertEquals('fake.event', $event->name()); }
/** * Helper function to test single method attaching for dispatcher filters * * @param \Cake\Event\Event $event * @return void */ public function filterTest($event) { $event->data['request']->params['eventName'] = $event->name(); }