/** **在请求交由action处理之前,判断用户属性,如果当前用户没有登录,或者登录用户没有管理员权限,那么抛出403异常,即只有管理员才能进入该管理模块. * @param \yii\base\Action $action * @return bool * @throws HttpException */ public function beforeAction($action) { if (!User::getCurrent() || !Admin::getCurrent()) { throw new HttpException(403, 'You are not an admin'); } return parent::beforeAction($action); }
public function requireAdmin() { if (!$this->hasErrors()) { $admin = Admin::getCurrent(); if (!$admin || $admin->is_blocked) { $this->addError('username', 'This account is not an admin or has been blocked as admin.'); } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Admin::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'is_blocked' => $this->is_blocked, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); return $dataProvider; }
/** * Finds the Admin model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Admin the loaded model * @throws HttpException if the model cannot be found */ protected function findModel($id) { if (($model = Admin::findOne($id)) !== null) { return $model; } else { throw new HttpException(404, 'The requested page does not exist.'); } }
/** * @return null|Admin */ public static function getCurrent() { return Admin::findOne(['user_id' => Yii::$app->user->id]); }
/** * @return \yii\db\ActiveQuery */ public function getAdmin() { return $this->hasOne(\common\models\admin\Admin::className(), ['user_id' => 'id']); }
/** **显示主页. * @return string content of home page */ public function actionIndex() { $admin = Admin::getCurrent(); return $this->render('index', ['admin' => $admin]); }