/** * Makes sure that the name is available. * This is the 'nameIsAvailable' validator as declared in rules(). */ public function nameIsAvailable($attribute, $params) { // Make sure that an authorization item with the name does not already exist if (Rights::getAuthorizer()->authManager->getAuthItem($this->name) !== null) { $this->addError('name', Rights::t('core', 'An item with this name already exists.', array(':name' => $this->name))); } }
/** * Actions to be taken after logging in. * Overloads the parent method in order to mark superusers. * @param boolean $fromCookie whether the login is based on cookie. */ public function afterLogin($fromCookie) { parent::afterLogin($fromCookie); // Mark the user as a superuser if necessary. if (Rights::getAuthorizer()->isSuperuser($this->getId()) === true) { $this->isSuperuser = true; } }
/** * Initializes the data provider. */ public function init() { $this->_authorizer = Rights::getAuthorizer(); // Set properties and generate the data $this->setRoles(); $this->setItems(); $this->setPermissions(); $this->setParents(); $this->generateData(); }
/** * Fetches the data from the persistent data storage. * @return array list of data items */ public function fetchData() { if ($this->sortable !== null) { $this->processSortable(); } if ($this->items === null) { $this->items = Rights::getAuthorizer()->getAuthItems($this->type, $this->userId, $this->parent, true, $this->exclude); } $data = array(); foreach ($this->items as $name => $item) { $data[] = $item; } return $data; }
/** * Gets the users assignments. * @param boolean whether to display the authorization item type. * @return string the assignments markup. */ public function getAssignments($displayType = false) { $authorizer = Rights::getAuthorizer(); $assignments = $authorizer->authManager->getAuthAssignments($this->getId()); $items = $authorizer->authManager->getAuthItemsByNames(array_keys($assignments)); $items = $authorizer->attachAuthItemBehavior($items); $assignedItems = array(); foreach ($items as $itemName => $item) { $itemMarkup = $item->getNameText(); if ($displayType === true) { $itemMarkup .= ' (<span class="type-text">' . Rights::getAuthItemTypeName($item->type) . '</span>)'; } $assignedItems[] = $itemMarkup; } return implode('<br />', $assignedItems); }
/** * Returns the authorization items assigned to the user. * @return string the assignments markup. */ public function getAssignments() { if ($this->_assignments !== null) { return $this->_assignments; } else { $authorizer = Rights::getAuthorizer(); $authAssignments = $authorizer->authManager->getAuthAssignments($this->getId()); $nestedItems = $authorizer->authManager->getAuthItemsByNames(array_keys($authAssignments), true); $assignments = array(); foreach ($nestedItems as $type => $items) { $items = $authorizer->attachAuthItemBehavior($items); $assignments[$type] = array(); foreach ($items as $itemName => $item) { $assignments[$type][$itemName] = $item; } } return $this->_assignments = $assignments; } }
/** * Actions to be taken after logging in. * Overloads the parent method in order to mark superusers. * @param boolean $fromCookie whether the login is based on cookie. */ public function afterLogin($fromCookie) { parent::afterLogin($fromCookie); $command = Yii::app()->db->createCommand(); $command->select('username,user_url,display_name,email,fbuid,status,recent_login,avatar')->from('{{user}} u')->where('user_id=' . (int) $this->getId())->limit(1); $user = $command->queryRow(); //Add only some neccessary field if ($user) { // Set User States here $this->setState('current_user', $user); // Set User Roles here $this->setState('current_roles', User::getArrayRoles($this->getId())); if (Rights::getAuthorizer()->isSuperuser($this->getId()) === true) { $this->isSuperuser = true; } } else { throw new CHttpException(503, t('cms', 'Error while Logging into your account. Please try again later.')); } }
/** * Returns the children of the specified item. * Overloads the parent method to allow for caching. * @param mixed $names the parent item name. This can be either a string or an array. * The latter represents a list of item names (available since version 1.0.5). * @param boolean $allowCaching whether to accept cached data. * @return array all child items of the parent */ public function getItemChildren($names, $allowCaching = true) { // Resolve the key for runtime caching. $key = $names === (array) $names ? implode('|', $names) : $names; // Get the children from cache if possible. if ($allowCaching && isset($this->_itemChildren[$key]) === true) { return $this->_itemChildren[$key]; } else { // We only have one name. if (is_string($names)) { $condition = 'parent=' . $this->db->quoteValue($names); } else { if ($names === (array) $names && $names !== array()) { foreach ($names as &$name) { $name = $this->db->quoteValue($name); } $condition = 'parent IN (' . implode(', ', $names) . ')'; } else { $condition = '1'; } } $sql = "SELECT name, type, description, bizrule, data\r\n\t\t\t\tFROM {$this->db->quoteTableName($this->itemTable)}, {$this->db->quoteTableName($this->itemChildTable)}\r\n\t\t\t\tWHERE {$condition} AND name=child"; $children = array(); foreach ($this->db->createCommand($sql)->queryAll() as $row) { if (($data = @unserialize($row['data'])) === false) { $data = null; } $children[$row['name']] = new CAuthItem($this, $row['name'], $row['type'], $row['description'], $row['bizrule'], $data); } // Attach the authorization item behavior. $children = Rights::getAuthorizer()->attachAuthItemBehavior($children); // Cache the result. return $this->_itemChildren[$key] = $children; } }
/** * Fetches the data from the persistent data storage. * @return array list of data items */ public function fetchData() { $this->items = Rights::getAuthorizer()->getAuthItemParents($this->parent->name, $this->type, null, true); return parent::fetchData(); }
/** * Actions to be taken after logging in. * Overloads the parent method in order to mark superusers. * @param boolean $fromCookie whether the login is based on cookie. */ public function afterLogin($fromCookie) { parent::afterLogin($fromCookie); // Mark the user as a superuser if necessary. //Get the user from the CActiveRecord $user = User::model()->findByPk($this->getId()); Yii::app()->getSession()->remove('current_user'); Yii::app()->getSession()->add('current_user', $user); if (Rights::getAuthorizer()->isSuperuser($this->getId()) === true) { $this->isSuperuser = true; } }
/** * Fetches the data from the persistent data storage. * @return array list of data items */ public function fetchData() { $this->items = Rights::getAuthorizer()->getAuthItemChildren($this->parent->name, $this->type); return parent::fetchData(); }
/** * Updates a particular model. * If update is successful, the browser will be redirected to the 'view' page. */ public function actionUpdate() { $model = $this->loadModel(); $profile = $model->profile; $this->performAjaxValidation(array($model, $profile)); /* Get current user role. Added by Phihx. date 14/02/2014*/ $assignedItems = Rights::getAuthorizer()->getAuthItems(null, $model->id); $userCurrenRole = array_keys($assignedItems); if (isset($_POST['User'])) { $model->attributes = $_POST['User']; $profile->attributes = $_POST['Profile']; if ($model->validate() && $profile->validate()) { /*$old_password = User::model()->notsafe()->findByPk($model->id); if ($old_password->password!=$model->password) { $model->password=Yii::app()->controller->module->encrypting($model->password); $model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password); }*/ if (!empty($_POST['newPassword'])) { $model->password = Yii::app()->controller->module->encrypting($_POST['newPassword']); $model->activkey = Yii::app()->controller->module->encrypting(microtime() . $_POST['newPassword']); } $model->save(); $profile->save(); /*remove role for user. added by phihx. date 14/02/2014*/ if (!empty($userCurrenRole)) { foreach ($userCurrenRole as $role) { Rights::revoke($role, $model->id); } } /*Add role for user. added by phihx. date 14/02/2014*/ if (!empty($_POST['user_role'])) { //foreach($_POST['user_role'] as $role){ Rights::assign($_POST['user_role'], $model->id); //} } Yii::app()->user->setFlash('success', translate('Chỉnh sửa người dùng thành công.')); $this->redirect(PIUrl::createUrl('/user')); } else { $profile->validate(); } } /* Get All role. Added by Phihx. date 14/02/2014*/ $allRoles = $this->getAllRoleUser(); //$allClass = Classes::model()->findAll(); $arrClass[''] = '---Chọn lớp---'; Yii::app()->theme = 'flatlab'; $this->render('update', array('model' => $model, 'profile' => $profile, 'allRoles' => $allRoles, 'userCurrenRole' => $userCurrenRole)); }
/** * Actions to be taken after logging in. * Overloads the parent method in order to mark superusers. * @param boolean $fromCookie whether the login is based on cookie. */ public function afterLogin($fromCookie) { parent::afterLogin($fromCookie); // Mark the user as a superuser if necessary. //Get the user from the CActiveRecord //$user=User::model()->findByPk($this->getId()); $command = Yii::app()->db->createCommand(); $command->select('username,user_url,display_name,email,fbuid,status,recent_login,avatar')->from('{{user}} u')->where('user_id=' . (int) $this->getId())->limit(1); $user = $command->queryRow(); //Add only some neccessary field if ($user) { Yii::app()->getSession()->remove('current_user'); Yii::app()->getSession()->add('current_user', $user); if (Rights::getAuthorizer()->isSuperuser($this->getId()) === true) { $this->isSuperuser = true; } } else { throw new CHttpException(503, t('Error while Logging into your account. Please try again later.')); } }