| 2 |
FLEA::loadClass('FLEA_Dispatcher_Simple'); class FLEA_Dispatcher_Auth extends FLEA_Dispatcher_Simple { var $_auth; function FLEA_Dispatcher_Auth(& $request) {$request=str_replace('.html','',$request); parent::FLEA_Dispatcher_Simple($request); $this->_auth =& FLEA::getSingleton(FLEA::getAppInf('dispatcherAuthProvider')); } function & getAuthProvider() { return $this->_auth; } function setAuthProvider(& $auth) { $this->_auth =& $auth; } function setUser($userData, $rolesData = null) { $this->_auth->setUser($userData, $rolesData); } function getUser() { return $this->_auth->getUser(); } function getUserRoles() { return $this->_auth->getRolesArray(); } function clearUser() { $this->_auth->clearUser(); } function dispatching() { $controllerName = $this->getControllerName(); $actionName = $this->getActionName(); $controllerClass = $this->getControllerClass($controllerName); if ($this->check($controllerName, $actionName, $controllerClass)) { return $this->_executeAction($controllerName, $actionName, $controllerClass); } else { $callback = FLEA::getAppInf('dispatcherAuthFailedCallback'); $rawACT = $this->getControllerACT($controllerName, $controllerClass); if (is_null($rawACT) || empty($rawACT)) { return true; } $ACT = $this->_auth->prepareACT($rawACT); $roles = $this->_auth->getRolesArray(); $args = array($controllerName, $actionName, $controllerClass, $ACT, $roles); if ($this->_loadController($controllerClass)) { $methods = get_class_methods($controllerClass); if (in_array('_onAuthFailed', $methods, true)) { if (call_user_func_array(array($controllerClass, '_onAuthFailed'), $args) !== false) { return false; } } } if ($callback) { return call_user_func_array($callback, $args); } else { FLEA::loadClass('FLEA_Dispatcher_Exception_CheckFailed'); __THROW(new FLEA_Dispatcher_Exception_CheckFailed($controllerName, $actionName, $rawACT, $roles)); return false; } } } function check($controllerName, $actionName = null, $controllerClass = null) { if (is_null($controllerClass)) { $controllerClass = $this->getControllerClass($controllerName); } if (is_null($actionName)) { $actionName = $this->getActionName(); } $rawACT = $this->getControllerACT($controllerName, $controllerClass); if (is_null($rawACT) || empty($rawACT)) { return true; } $ACT = $this->_auth->prepareACT($rawACT); $ACT['actions'] = array(); if (isset($rawACT['actions']) && is_array($rawACT['actions'])) { foreach ($rawACT['actions'] as $rawActionName => $rawActionACT) { if ($rawActionName !== ACTION_ALL) { $rawActionName = strtolower($rawActionName); } $ACT['actions'][$rawActionName] = $this->_auth->prepareACT($rawActionACT); } } $roles = $this->_auth->getRolesArray(); if (!$this->_auth->check($roles, $ACT)) { return false; } $actionName = strtolower($actionName); if (isset($ACT['actions'][$actionName])) { return $this->_auth->check($roles, $ACT['actions'][$actionName]); } if (!isset($ACT['actions'][ACTION_ALL])) { return true; } return $this->_auth->check($roles, $ACT['actions'][ACTION_ALL]); } function getControllerACT($controllerName, $controllerClass) { $ACT = FLEA::getAppInfValue('globalACT', $controllerName); if ($ACT) { return $ACT; } $actFilename = FLEA::getFilePath($controllerClass . '.act.php'); if (!$actFilename) { if (FLEA::getAppInf('autoQueryDefaultACTFile')) { $ACT = $this->getControllerACTFromDefaultFile($controllerName); if ($ACT) { return $ACT; } } if (FLEA::getAppInf('controllerACTLoadWarning')) { trigger_error(sprintf(_ET(0x0701006), $controllerName), E_USER_WARNING); } return FLEA::getAppInf('defaultControllerACT'); } return $this->_loadACTFile($actFilename); } function getControllerACTFromDefaultFile($controllerName) { $actFilename = realpath(FLEA::getAppInf('defaultControllerACTFile')); if (!$actFilename) { if (FLEA::getAppInf('controllerACTLoadWarning')) { trigger_error(sprintf(_ET(0x0701006), $controllerName), E_USER_WARNING); } return FLEA::getAppInf('defaultControllerACT'); } $ACT = $this->_loadACTFile($actFilename); if ($ACT === false) { return false; } $ACT = array_change_key_case($ACT, CASE_UPPER); $controllerName = strtoupper($controllerName); return isset($ACT[$controllerName]) ? $ACT[$controllerName] : FLEA::getAppInf('defaultControllerACT'); } function _loadACTFile($actFilename) { static $files = array(); if (isset($files[$actFilename])) { return $files[$actFilename]; } $ACT = require($actFilename); if (is_array($ACT)) { $files[$actFilename] = $ACT; return $ACT; } FLEA::loadClass('FLEA_Rbac_Exception_InvalidACTFile'); __THROW(new FLEA_Rbac_Exception_InvalidACTFile($actFilename, $ACT)); return false; } } |