/** * method for dispatching plugin events * * See {@link find()} for detailed explanation about $condition and $params. * @param string $sEventName event name to dispatch * @param array $criteria array containing attributes, conditions and params for the filter query * @return PluginEvent the dispatched event */ public function dispatchPluginModelEvent($sEventName, $criteria = null) { $oPluginEvent = new PluginEvent($sEventName, $this); $oPluginEvent->set('model', $this->owner); if (isset($criteria)) { $oPluginEvent->set('filterCriteria', $criteria); } return App()->getPluginManager()->dispatchEvent($oPluginEvent); }
public function authenticate() { // First initialize the result, we can later retieve it to get the exact error code/message $result = new LSAuthResult(self::ERROR_NONE); // Check if the ip is locked out if (FailedLoginAttempt::model()->isLockedOut()) { $message = sprintf(gT('You have exceeded the number of maximum login attempts. Please wait %d minutes before trying again.'), App()->getConfig('timeOutTime') / 60); $result->setError(self::ERROR_IP_LOCKED_OUT, $message); } // If still ok, continue if ($result->isValid()) { if (is_null($this->plugin)) { $result->setError(self::ERROR_UNKNOWN_HANDLER); } else { // Delegate actual authentication to plugin $authEvent = new PluginEvent('newUserSession', $this); $authEvent->set('identity', $this); App()->getPluginManager()->dispatchEvent($authEvent); $pluginResult = $authEvent->get('result'); if ($pluginResult instanceof LSAuthResult) { $result = $pluginResult; } else { $result->setError(self::ERROR_UNKNOWN_IDENTITY); } } } if ($result->isValid()) { // Perform postlogin $this->postLogin(); } else { // Log a failed attempt $userHostAddress = getIPAddress(); FailedLoginAttempt::model()->addAttempt($userHostAddress); App()->session->regenerateID(); // Handled on login by Yii } $this->errorCode = $result->getCode(); $this->errorMessage = $result->getMessage(); return $result->isValid(); }
/** * Show login screen and parse login data */ public function index() { $this->_redirectIfLoggedIn(); // Make sure after first run / update the authdb plugin is registered and active // it can not be deactivated if (!class_exists('Authdb', false)) { $plugin = Plugin::model()->findByAttributes(array('name' => 'Authdb')); if (!$plugin) { $plugin = new Plugin(); $plugin->name = 'Authdb'; $plugin->active = 1; $plugin->save(); App()->getPluginManager()->loadPlugin('Authdb', $plugin->id); } else { $plugin->active = 1; $plugin->save(); } } $beforeLogin = new PluginEvent('beforeLogin'); $beforeLogin->set('identity', new LSUserIdentity('', '')); App()->getPluginManager()->dispatchEvent($beforeLogin); /* @var $identity LSUserIdentity */ $identity = $beforeLogin->get('identity'); if (!$beforeLogin->isStopped() && is_null(App()->getRequest()->getPost('login_submit'))) { if (!is_null($beforeLogin->get('default'))) { $aData['defaultAuth'] = $beforeLogin->get('default'); } else { if (App()->getPluginManager()->isPluginActive(Yii::app()->getConfig('default_displayed_auth_method'))) { $aData['defaultAuth'] = Yii::app()->getConfig('default_displayed_auth_method'); } else { $aData['defaultAuth'] = 'Authdb'; } } $newLoginForm = new PluginEvent('newLoginForm'); App()->getPluginManager()->dispatchEvent($newLoginForm); $aData['summary'] = $this->_getSummary('logout'); $aData['pluginContent'] = $newLoginForm->getAllContent(); $this->_renderWrappedTemplate('authentication', 'login', $aData); } else { // Handle getting the post and populating the identity there $authMethod = App()->getRequest()->getPost('authMethod', $identity->plugin); $identity->plugin = $authMethod; $event = new PluginEvent('afterLoginFormSubmit'); $event->set('identity', $identity); App()->getPluginManager()->dispatchEvent($event, array($authMethod)); $identity = $event->get('identity'); // Now authenticate if ($identity->authenticate()) { FailedLoginAttempt::model()->deleteAttempts(); App()->user->setState('plugin', $authMethod); $this->getController()->_GetSessionUserRights(Yii::app()->session['loginID']); Yii::app()->session['just_logged_in'] = true; Yii::app()->session['loginsummary'] = $this->_getSummary(); $event = new PluginEvent('afterSuccessfulLogin'); App()->getPluginManager()->dispatchEvent($event); $this->_doRedirect(); } else { // Failed $event = new PluginEvent('afterFailedLoginAttempt'); $event->set('identity', $identity); App()->getPluginManager()->dispatchEvent($event); $message = $identity->errorMessage; if (empty($message)) { // If no message, return a default message $message = gT('Incorrect username and/or password!'); } App()->user->setFlash('error', $message); $this->getController()->redirect(array('/admin/authentication/sa/login')); } } }
/** * This function dispatches an event to all registered plugins. * @param PluginEvent $event Object holding all event properties * @param string|array $target Optional name of plugin to fire the event on * * @return PluginEvent */ public function dispatchEvent(PluginEvent $event, $target = array()) { $eventName = $event->getEventName(); if (is_string($target)) { $target = array($target); } if (isset($this->subscriptions[$eventName])) { foreach ($this->subscriptions[$eventName] as $subscription) { if (!$event->isStopped() && (empty($target) || in_array(get_class($subscription[0]), $target))) { $subscription[0]->setEvent($event); call_user_func($subscription); } } } return $event; }