/** * {@inheritDoc} * @see CConsoleCommandRunner::createCommand() */ public function createCommand($name) { if (strpos($name, '/') !== false) { // route. Search if the route is valid, otherwise is a path, and // should not be modified. $route_parts = explode('/', trim($name, '/')); $module_parts = array_slice($route_parts, 0, -1); // a route is composed of module/submodule1/submodule2/.../class $commandpath = implode('/', $module_parts); if (!in_array($commandpath, $this->_modules_searched)) { $module = YiiPlug::app(); foreach ($module_parts as $module_route) { if ($module !== null) { $module = $module->getModule($module_route); } else { break; } } if ($module !== null) { /* @var $module CModule */ $modulepath = $module->getBasePath() . DIRECTORY_SEPARATOR . 'commands'; $commands = $this->findCommands($modulepath); foreach ($commands as $cname => $command) { $this->commands[$commandpath . '/' . $cname] = $command; } } $this->_modules_searched[] = $commandpath; } } return parent::createCommand($name); }
/** * Sets up all the roles and their hierarchy with all the elements that the * module is able to provide in the installation. * * @param string $moduleid */ public function actionModule($moduleid) { $module = YiiPlug::app()->getAutoloadedModule($moduleid); if ($module === null) { throw new CException(Yii::t('access.install', 'Impossible to find the module {id}.', array('{id}' => $moduleid))); } /* @var $accessModule AccessModule */ $accessModule = $this->getModule(); try { $transaction = AccessRole::model()->getDbConnection()->beginTransaction(); $accessModule->registerAuthHierarchyFromModule($module); $transaction->commit(); } catch (CDbException $e) { try { $transaction->rollback(); } catch (CException $e2) { /* silent */ } echo "The module hierarchy couldn't be installed.\n"; echo $e->getMessage() . "\n"; echo $e->getTraceAsString() . "\n"; return; } echo "The module hierarchy is correctly installed.\n"; }
/** * Gets owner's module * @return AccessModule */ public function getModule() { if (($module = parent::getModule()) !== null) { return $module; } return YiiPlug::app()->getModule('access'); }
/** * Configures this module to use the connection module with id $dbmodule, * and the db connection it manages with id $dbid * * @param string $dbmodule the id of the database connection manager module, * as it it registered into the application * @param string $dbid the id of the database connection itself as it is * registered into the database connection module * @param $pwdminlen the minimal length for user passwords * @param $pwdminupper the minimal number of upper characters in passwords * @param $pwdminlower the minimal number of lower characters in passwords * @param $pwdmindigit the minimal number of digit characters in passwords * @param $pwdminsymbol the minimal number of symbol characters in passwords */ public function actionConfigure($dbmodule, $dbid, $pwdminlen = 8, $pwdminupper = 1, $pwdminlower = 1, $pwdmindigit = 1, $pwdminsymbol = 1) { $connectionModule = YiiPlug::app()->getConnectionModuleOrFail($dbmodule); if ($connectionModule === null) { throw new CException(Yii::t('user.install', 'Impossible to find the connection module with given id {id}.', array('{id}' => $dbmodule))); } $connection = $connectionModule->getConnection($dbid); if ($connection === null) { throw new CException(Yii::t('user.install', 'Impossible to find the connection with given id {id} in module {module}.', array('{id}' => $dbid, '{module}' => $dbmodule))); } $form = new UserModuleSettingsForm(); $form->database_module = $dbmodule; $form->database_identifier = $dbid; $form->password_min_length = $pwdminlen; $form->password_min_uppercase = $pwdminupper; $form->password_min_lowercase = $pwdminlower; $form->password_min_digits = $pwdmindigit; $form->password_min_symbols = $pwdminsymbol; if ($form->validate()) { $module = $this->getModule(); if ($module->saveSettings($form)) { echo "The configuration for module user is successfully saved.\n"; } else { echo "The configuration for module user couldn't be saved.\n"; } } else { throw new CException($form->getErrorListAsString()); } }
/** * {@inheritDoc} * @see IPluggableAccessModule::checkAccess() */ public function checkAccess(IPluggableModule $module, IPluggableWebUser $user, $role, $askForPrivilegeElevation = true, array $params = array()) { if ($user->getIsGuest()) { return false; } if (!$this->isInstalled()) { return false; } foreach (YiiPlug::app()->getUserModules() as $userModule) { $profile = $userModule->getUserProfile($user); if ($profile !== null) { break; } } if ($profile === null) { return false; } $mgr = $this->getAccessManager(); $item = $mgr->getAuthItem($module, $role); if ($item === null) { return false; } try { return $mgr->checkAccess($module, $item, $profile, $askForPrivilegeElevation, $params); } catch (CException $e) { /* silent */ } return false; }
/** * {@inheritDoc} * @see PluggableWebModule::getNavbarElements() * @see CMenu */ public function getNavbarElements() { if (YiiPlug::app()->getUser()->getIsGuest()) { return array(9999 => array('label' => '<i class="glyphicon glyphicon-log-in"> </i>' . CHtml::encode(Yii::t('user.module', 'Sign In')), 'url' => array('/user/profile/login'), 'items' => array(array('label' => '<i class="glyphicon glyphicon-log-in"> </i>' . CHtml::encode(Yii::t('user.module', 'Sign In')), 'url' => array('/user/profile/login')), array('label' => '<i class="glyphicon glyphicon-plus"> </i>' . CHtml::encode(Yii::t('user.module', 'Register')), 'url' => array('/user/profile/register'))))); } return array(9999 => array('label' => '<i class="glyphicon glyphicon-user"></i>', 'url' => '#', 'items' => array(array('label' => '<i class="glyphicon glyphicon-user"> </i>' . CHtml::encode(Yii::t('user.module', 'My Profile')), 'url' => array('/user/profile/index')), array('label' => '<i class="glyphicon glyphicon-log-out"> </i>' . CHtml::encode(Yii::t('user.module', 'Log Out')), 'url' => array('/site/logout'))))); }
/** * {@inheritDoc} * @see CModule::init() */ public function init() { // this method is called when the module is being created // you may place code here to customize the module or the application // import the module-level models and components $this->setImport(array($this->id . '.forms.*', $this->id . '.components.*')); $this->setComponent('repository', array('class' => 'DConnectionRepository', 'runtimePath' => YiiPlug::app()->getRuntimeDirectory($this))); }
/** * Gets the user profile from user id. * * @param string $userId * @return IPluggableUserProfile, null if does not exists. */ protected function getProfileFromId($userId) { foreach (YiiPlug::app()->getUserModules() as $userModule) { $profile = $userModule->getUserProfileFromId($userId); if ($profile !== null) { return $profile; } } return null; }
/** * * @return number the number of bytes written */ public function save() { if ($this->getForm()->validate()) { $filename = $this->filename . '.php'; $path = YiiPlug::app()->getRuntimeDirectory($this->module); $fullpath = $path . DIRECTORY_SEPARATOR . $filename; $str = "<?php return " . get_class($this->getForm()) . "::__set_state(" . var_export($this->getForm()->getAttributes(), true) . ");\n"; return 0 !== file_put_contents($fullpath, $str); } return false; }
/** * Authenticates the user to perform privilege escalation. */ public function actionRing() { $model = new UserRingForm(); if (isset($_POST[get_class($model)])) { $model->attributes = $_POST[get_class($model)]; if ($model->validate() && $model->authenticate()) { $this->redirect(YiiPlug::app()->getUser()->getReturnUrl()); } } $this->title = Yii::t('user.profile', 'Confirm Password to continue'); $this->render('ring', array('model' => $model)); }
/** * Authenticates the user and sets its new ring level with the associated * timeout. * * @param string $attribute * @param mixed[] $params */ public function authenticate($attribute, $params) { if (!$this->hasErrors()) { $user = YiiPlug::app()->getUser(); /* @var $identity UserUserIdentity */ $classname = (new UserLoginForm())->getIdentityClassname(); $identity = new $classname($user->getName(), $this->password); if ($identity->authenticate()) { $requiredRing = $user->getState('ring_value.required', 0); $user->setState('ring_value.current', $requiredRing); $user->setState('ring_value.required', null); // update session time availability. // if session ring level is 1000, the session lasts for the next hour // if session ring level is 6000, the session lasts for the next 10 min $user->setState('ring_value.expire', time() + 3600000 / $requiredRing); Yii::app()->request->redirect($user->getReturnUrl()); } else { $this->addError('password', 'Incorrect password.'); } } }
/** * * @return IPluggableConnectionModule */ public function getDatabaseModule() { return YiiPlug::app()->getConnectionModule($this->database_module); }
/** * {@inheritDoc} * @see IPluggableUserModule::getConnectionModule() */ public function getConnectionModule($id = null) { return YiiPlug::app()->getConnectionModule(null); }
<?php /* @var $this DefaultController */ $this->breadcrumbs = array(YiiPlug::app()->getUser()->getName()); ?> <div class="col-md-3"> <div class="panel panel-default"> <div class="panel-heading"> <?php echo Yii::t('user.view', 'My Informations'); ?> </div> <div class="panel-body"> <?php echo CHtml::encode(YiiPlug::app()->getUser()->getName()); ?> </div> </div> </div>
/** * Gets the pluggable user module needed * @return IPluggableUserModule */ public function getUserModule() { return YiiPlug::app()->getUserModule($this->usermodule); }
/** * Checks if the user has enough accesses to display an item in a menu. * * @param string $role the role which the user should have * @param mixed[] $params * @return boolean true if the user is allowed. */ public function checkAccessMenu($role, array $params = array()) { return YiiPlug::app()->getUser()->checkAccess($role, $params, true, false); }
/** * Returns whether the current ring level of the user * * @param AccessRole $role the item to be checked against * @param IPluggableUserProfile $profile the profile of the user * @param boolean $askForPrivilegeElevation whether we should check for * privilege elevation if the user has access to the action * @return boolean if the user has access to the action */ protected function checkRingLevel(AccessRole $role, $askForPrivilegeElevation) { if (!$askForPrivilegeElevation) { return true; } $user = YiiPlug::app()->getUser(); if ($role->ring > $user->getState('ring_value.current', 0)) { $user->ringUpdateRequired($role->ring); } return true; }
/** * {@inheritDoc} * @see CWebUser::checkAccess() * @see IPluggableWebUser::checkAccess() */ public function checkAccess($operation, $params = array(), $allowCaching = true, $askForPrivilegeElevation = true) { if ($this->getIsGuest()) { return false; } if ($allowCaching && $params === array() && isset($this->_access[$operation])) { return $this->_access[$operation]; } $module = null; if (($pos = strpos($operation, '.')) !== false) { $moduleId = substr($operation, 0, $pos); $module = Yii::app()->getModule($moduleId); $operation = substr($operation, $pos + 1); } if ($module === null) { $module = Yii::app()->controller->module; } if ($module === null) { $module = Yii::app(); } $access = false; if ($module instanceof IPluggableModule) { foreach (YiiPlug::app()->getAccessModules() as $accessModule) { /* @var $accessModule IPluggableAccessModule */ $access = $accessModule->checkAccess($module, $this, $operation, $askForPrivilegeElevation, $params); if ($access) { break; } } } if ($allowCaching && $params === array()) { $this->_access[$operation] = $access; } return $access; }
/** * {@inheritDoc} * @see IPluggableModuleSettingsForm::getAccessModule() */ public function getAccessModule() { return YiiPlug::app()->getAccessModule($this->access_module); }