/** * Reroutes if http was not used * * @return void */ public static function enforce() { if (self::on() || \MUtil_Console::isConsole() || \Zend_Session::$_unitTestEnabled) { return; } $request = \Zend_Controller_Front::getInstance()->getRequest(); $url = 'https://' . $_SERVER['HTTP_HOST'] . $request->getRequestUri(); $redirector = \Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); $redirector->gotoUrl($url); }
/** * Action for displaying an error, CLI as well as HTTP */ public function errorAction() { $errors = $this->_getParam('error_handler'); $exception = $errors->exception; $info = null; $message = 'Application error'; $responseCode = 200; switch ($errors->type) { case \Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: case \Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: // 404 error -- controller or action not found $responseCode = 404; $message = 'Page not found'; break; case \Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER: if ($exception instanceof \Gems_Exception) { $responseCode = $exception->getCode(); $message = $exception->getMessage(); $info = $exception->getInfo(); break; } // Intentional fall through // Intentional fall through default: $message = $exception->getMessage(); break; } \Gems_Log::getLogger()->logError($errors->exception, $errors->request); if (\MUtil_Console::isConsole()) { $this->_helper->viewRenderer->setNoRender(true); echo $message . "\n\n"; if ($info) { echo $info . "\n\n"; } $next = $exception->getPrevious(); while ($next) { echo ' ' . $next->getMessage() . "\n"; $next = $next->getPrevious(); } echo $exception->getTraceAsString(); } else { if ($responseCode) { $this->getResponse()->setHttpResponseCode($responseCode); } $this->view->exception = $exception; $this->view->message = $message; $this->view->request = $errors->request; if ($info) { $this->view->info = $info; } } }
/** * Get the currently loggin in user * * @return \Gems_User_User */ public final function getCurrentUser() { if (!self::$currentUser) { if ($this->session->__isset('__user_definition')) { $defName = $this->session->__get('__user_definition'); // Check for during upgrade. Remove for version 1.6 if (substr($defName, -10, 10) != 'Definition') { $defName .= 'Definition'; } self::$currentUser = $this->_loadClass('User', true, array($this->session, $this->_getClass($defName))); } else { if (\MUtil_Console::isConsole()) { if (!$this->project->isConsoleAllowed()) { echo "Accessing " . GEMS_PROJECT_NAME . " from the command line is not allowed.\n"; exit; } $request = \Zend_Controller_Front::getInstance()->getRequest(); if ($request instanceof \MUtil_Controller_Request_Cli && $request->hasUserLogin()) { $user = $this->getUser($request->getUserName(), $request->getUserOrganization()); $authResult = $user->authenticate($request->getUserPassword()); if (!$authResult->isValid()) { echo "Invalid user login data.\n"; echo implode("\n", $authResult->getMessages()); exit; } self::$currentUser = $user; } elseif ($this->project->getConsoleRole()) { // \MUtil_Echo::track($this->request->getUserName(), $this->request->getUserOrganization()); self::$currentUser = $this->loadUser(self::USER_CONSOLE, 0, '(system)'); } } if (!self::$currentUser) { self::$currentUser = $this->getUser(null, self::SYSTEM_NO_ORG); } self::$currentUser->setAsCurrentUser(); } } return self::$currentUser; }
/** * * @param string $id A unique name identifying this batch * @param \MUtil_Batch_Stack_Stackinterface $stack Optional different stack than session stack */ public function __construct($id, \MUtil_Batch_Stack_Stackinterface $stack = null) { $id = preg_replace('/[^a-zA-Z0-9_]/', '', $id); if (isset(self::$_idStack[$id])) { throw new \MUtil_Batch_BatchException("Duplicate batch id created: '{$id}'"); } self::$_idStack[$id] = $id; $this->_id = $id; if (null === $stack) { $stack = new \MUtil_Batch_Stack_SessionStack($id); } $this->stack = $stack; $this->_initSession($id); if (\MUtil_Console::isConsole()) { $this->method = self::CONS; } }
/** * Initiate the router for the command line if this is a command line script. Thanks to: * http://stackoverflow.com/questions/2325338/running-a-zend-framework-action-from-command-line */ protected function _initRouter() { if (\MUtil_Console::isConsole()) { $this->bootstrap('frontController'); $front = $this->getResource('frontController'); $front->setParam('disableOutputBuffering', true); $front->setRouter(new \MUtil_Controller_Router_Cli()); $front->setRequest(new \MUtil_Controller_Request_Cli()); $front->setResponse(new \MUtil_Controller_Response_Cli()); } }
/** * Hook 6: Called after \Zend_Controller_Router has determined the route set by the request. * * This events enables you to adjust the route after the routing has run it's course. * * Not initialized is the $controller object. * * Previous hook: routeStartup() * Actions since: $router->route() * Actions after: nothing, but the route consisting of controller, action and module should now be fixed * Next hook: dispatchLoopStartup() * * Also sets $this->currentOrganization and $this->menu to access afterwards * * @param \Zend_Controller_Request_Abstract $request * @return void */ public function routeShutdown(\Zend_Controller_Request_Abstract $request) { $loader = $this->getLoader(); // Load the menu. As building the menu can depend on all resources and the request, we do it here. // // PS: The REQUEST is needed because otherwise the locale for translate is not certain. $menu = $loader->createMenu($this); $source = $menu->getParameterSource(); $user = $this->_container->currentUser; $user->setRequest($request); $organization = $user->getCurrentOrganization(); $organization->applyToMenuSource($source); $this->_container->currentOrganization = $organization; $this->_container->menu = $menu; $this->_updateVariable(array('currentOrganization', 'menu')); // Now is a good time to check for required values // Moved down here to prevent unit test from failing on missing salt $this->project->checkRequiredValues(); /** * Check if we are in maintenance mode or not. This is triggeren by a file in the var/settings * directory with the name lock.txt */ if ($this->getUtil()->getMaintenanceLock()->isLocked()) { if ($user->isActive() && !$user->hasPrivilege('pr.maintenance.maintenance-mode')) { //Still allow logoff so we can relogin as master if (!('index' == $request->getControllerName() && 'logoff' == $request->getActionName())) { $this->setError($this->_('Please check back later.'), 401, $this->_('System is in maintenance mode')); } $user->unsetAsCurrentUser(); } else { $this->addMessage($this->_('System is in maintenance mode')); \MUtil_Echo::r($this->_('System is in maintenance mode')); } } // Gems does not use index/index $action = $request->getActionName(); if ('index' == $request->getControllerName() && ('index' == $action || $user->isActive() && 'login' == $action)) { // Instead Gems routes to the first available menu item when this is the request target if (!$user->gotoStartPage($menu, $request)) { $this->setError($this->_('No access to site.'), 401, $this->_('You have no access to this site.'), true); return; } } else { //find first allowed item in the menu $menuItem = $menu->find(array('action' => $request->getActionName(), 'controller' => $request->getControllerName())); // Display error when not having the right priviliges if (!($menuItem && $menuItem->get('allowed'))) { // When logged in if ($user->getUserId()) { $this->setError($this->_('No access to page'), 403, sprintf($this->_('Access to the %s/%s page is not allowed for current role: %s.'), $request->getControllerName(), $request->getActionName(), $user->getRole()), true); } else { // No longer logged in if (\MUtil_Console::isConsole()) { $this->setError('No access to page.', 401, sprintf('Controller "%s" action "%s" is not accessible.', $request->getControllerName(), $request->getActionName()), true); return; } if ($request->getActionName() == 'autofilter') { // Throw an exception + HTTP 401 when an autofilter is called throw new \Gems_Exception("Session expired", 401); } $menuItem = $menu->findFirst(array('allowed' => true, 'visible' => true)); if ($menuItem) { // Do not store previous request & show message when the intended action is logoff if (!($request->getControllerName() == 'index' && $request->getActionName() == 'logoff')) { $this->addMessage($this->_('You are no longer logged in.')); $this->addMessage($this->_('You must login to access this page.')); if (!\MUtil_String::contains($request->getControllerName() . $request->getActionName(), '.')) { // save original request, we will redirect back once the user succesfully logs in $staticSession = $this->getStaticSession(); $staticSession->previousRequestParameters = $request->getParams(); $staticSession->previousRequestMode = $request->isPost() ? "POST" : "GET"; } } $redirector = \Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); $redirector->gotoRoute($menuItem->toRouteUrl($request)); } else { $this->setError($this->_('You are no longer logged in.'), 401, $this->_('You have no access to this site.'), true); return; } } } } if (isset($menuItem)) { $menuItem->applyHiddenParameters($request, $source); $menu->setCurrent($menuItem); } }