/** * Finds user by [[username]] * * @return User|null */ public function getUser() { if ($this->_user === false) { $this->_user = TAdmUser::findByUsername($this->username); } return $this->_user; }
/** * Creates data provider instance with search query applied * @param array $params * @return ActiveDataProvider */ public function search($params) { $query = TAdmUser::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'password', $this->password])->andFilterWhere(['like', 'userphoto', $this->userphoto]); return $dataProvider; }
/** * 修改密码 * @return string|Response */ public function actionChangepwd() { $model = TAdmUser::findOne(Yii::$app->user->id); $model->scenario = 'chgpwd'; if (Yii::$app->request->isPost) { $model->load(Yii::$app->request->post()); if ($model->save()) { Yii::$app->session->setFlash('success'); } else { Yii::$app->session->setFlash('fail'); } return $this->goHome(); } return $this->render('changepwd', ['model' => $model]); }
/** * 给用户分配角色 * @param $id * @return string */ public function actionAssignrole($id) { $auth = Yii::$app->authManager; $model = TAdmUser::findOne($id); if (Yii::$app->request->isPost) { $action = Yii::$app->request->get('action'); $roles = Yii::$app->request->post('roles'); if ($action == 'assign') { foreach ($roles as $rolename) { $role = $auth->getRole($rolename); $auth->assign($role, $id); } } else { foreach ($roles as $rolename) { $role = $auth->getRole($rolename); $auth->revoke($role, $id); } } //所有角色 $allroles = ArrayHelper::map($auth->getRoles(), 'name', 'name'); //所有已选择的角色 $selectedroles = ArrayHelper::map($auth->getRolesByUser($id), 'name', 'name'); $res = [Html::renderSelectOptions('', array_diff($allroles, $selectedroles)), Html::renderSelectOptions('', $selectedroles)]; Yii::$app->response->format = Response::FORMAT_JSON; return $res; } //获取已有角色 $assignedroles = ArrayHelper::map($auth->getRolesByUser($id), 'name', 'name'); //获取所有角色 $allroles = ArrayHelper::map($auth->getRoles(), 'name', 'name'); //未被选择的角色 $roles = array_diff($allroles, $assignedroles); return $this->render('assignrole', ['roles' => $roles, 'assignedroles' => $assignedroles, 'model' => $model, 'id' => $id]); }