/** * Finds the UserRole model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return UserRole the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = UserRole::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @param string|integer $user the user ID. * @param Item $item the role or permission that this rule is associated with * @param array $params parameters passed to ManagerInterface::checkAccess(). * @return boolean a value indicating whether the rule permits the role or permission it is associated with. */ public function execute($user, $item, $params) { if (!Yii::$app->user->isGuest) { foreach (Yii::$app->getModule('rbac')->roles as $role) { if ($item->name === $role) { return UserRole::find()->joinWith('role')->andWhere(['{{%user_role}}.user_id' => $user, '{{%role}}.name' => $role])->exists(); } } return false; } return false; }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = UserRole::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'role_id' => $this->role_id, 'tenant_id' => $this->tenant_id, 'created_at' => $this->created_at, 'create_user_id' => $this->create_user_id, 'updated_at' => $this->updated_at, 'update_user_id' => $this->update_user_id]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getUserRole() { return UserRole::find()->andWhere(['user_id' => $this->id]); }
/** * @return \yii\db\ActiveQuery */ public function getUserRoles() { return $this->hasMany(UserRole::className(), ['role_id' => 'id']); }