public function getRolesByUser($user) { $on = $this->buildOn([$this->roleTable, 'key'], [$this->assignmentTable, 'role']); $query = Role::find()->innerJoin($this->assignmentTable, $on); $query->andWhere([$this->assignmentTable . '.user' => $user]); return $query->all(); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Role::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(['category_id' => $this->category_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'key', $this->key])->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'note', $this->note]); return $dataProvider; }
public function actionRole($user) { $userRoles = Assignment::find()->select('role')->where(['user' => $user])->indexBy('role')->all(); if (\Yii::$app->request->isPost) { $selectedRoles = LuLu::getPostValue('roles', []); Assignment::deleteAll(['and', 'user=\'' . $user . '\'', ['not in', 'role', $selectedRoles]]); foreach ($selectedRoles as $selectedRole) { if ($userRoles != null && isset($userRoles[$selectedRole])) { continue; } $newAssignment = new Assignment(); $newAssignment->user = $user; $newAssignment->role = $selectedRole; $newAssignment->save(); } return $this->redirect(['role', 'user' => $user]); } $allRoles = Role::findAll(); return $this->render('role', ['userRoles' => $userRoles, 'allRoles' => $allRoles]); }
/** * Finds the Role model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $id * @return Role the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Role::findOne(['key' => $id])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }