databaseConfigExists ()) { throw new Exception_BadInstallation ('Database configurations does not exist.'); } parent::__construct (); $this->defineExceptionHandler (); $this->defineSimpleConstants (); } function gate ($method) { $this->defineDatabasedConstants (); Zend_Registry::getInstance ()->Environment = new Environment_Runtime (); try { Zend_Session::start (); Zend_Session::registerValidator (new Zend_Session_Validator_UserIP ()); Zend_Session::setOptions (array ('strict' => false, 'throw_startup_exceptions' => 0)); if (in_array ($method, array ('logout', 'reminder'))) { $this->$method (); } else { try { if (! Zend_Session::sessionExists () || Zend_Session::isDestroyed ()) { if (request::is_ajax ()) { header ('HTTP/1.0 403 Access denied'); } $this->login (); return; } elseif (! Zend_Session::isStarted ()) { Zend_Session::start (); } if (Zend_Session::isStarted ()) { $acl = new Acl (); if ($acl->isAllowed ($this->getRole (), $this->resolveResourceByMethod ($method))) { if (request::is_ajax ()) { $method .= 'ajax'; } if (method_exists ($this, $method)) { $arguments = func_get_args (); array_shift ($arguments); if (method_exists ($this, $method)) { call_user_func_array (array ($this, $method), $arguments); } else { Kohana::show_404 (); } } else { throw new Exception_NotFound (); } } else { $this->login (); } } } catch (Exception_NotFound $e) { $view = new View_Admin ('form'); $view->form = 'Page does not exist.'; echo $view->render (); } } } catch (Zend_Session_Exception $e) { Zend_Session::destroy(true, false); Zend_Session::forgetMe (); url::newurl (url::href ('//admin/login')); } } private function getRole () { $namespace = new Zend_Session_Namespace ('application'); if (isset ($namespace->role)) { return $namespace->role; } } protected function resolveResourceByMethod ($method) { return Acl::RESOURCE_ADMINISTRATION; } private function login () { $view = new View_Admin ('login'); $view->title = 'Log in'; if (isset ($_GET ['loggedout'])) { $view->message = 'You were successfully logged out.'; } $form = new Formo_Form ('default', 'default'); $form ->setWidth ('50%') ->add ('text', 'Login') ->add ('password', 'Password') ->addSubmitButton ('Submit') ; $form->login->add_filter ('trim'); $form->password->add_filter ('trim'); $view->form = $form; if ($form->validate ()) { $values = $form->get_values (); $a = new Zend_Auth_Adapter_Setting (); $a->setLogin ($values['login']); $a->setPassword ($values['password']); $result = $a->authenticate (); if ($result->getCode () != Zend_Auth_Result::SUCCESS) { $form->error ('login', 'Invalid login and/or password.'); } else { $namespace = new Zend_Session_Namespace ('application'); $namespace->role = $result->getIdentity (); $namespace->login = $values['login']; $menu = $view->createMenu (); if ($menu->getFirstAvailable ()->getHref () == '/admin/login') { Kohana::log ('error', 'First menu item is not /admin/login but ' . $menu->getFirstAvailable ()->getHref ()); exit; } url::newurl ($menu->getFirstAvailable ()->getHref ()); } } echo $view->render (); } function getSubAction () { $url = Router::$routed_uri; return preg_replace ('~^\w+/gate/+[^/]+/+([^/]+)?.*~', '$1', $url); } }