public function init() { parent::init(); // custom initialization code goes here $this->assignmentTable = Assignment::tableName(); $this->roleTable = Role::tableName(); $this->permissionTable = Permission::tableName(); $this->relationTable = Relation::tableName(); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Relation::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(['like', 'role', $this->role])->andFilterWhere(['like', 'permission', $this->permission])->andFilterWhere(['like', 'rule', $this->rule])->andFilterWhere(['like', 'data', $this->data])->andFilterWhere(['like', 'note', $this->note]); return $dataProvider; }
public function actionPermission($role) { $rolePermissions = Relation::find()->select(['permission', 'value'])->where(['role' => $role])->indexBy('permission')->all(); if (\Yii::$app->request->isPost) { var_dump(LuLu::getPostValue('Permission')); $selectedPermissions = LuLu::getPostValue('Permission'); $keys = array_keys($selectedPermissions); Relation::deleteAll(['role' => $role]); foreach ($selectedPermissions as $key => $value) { $newRelation = new Relation(); $newRelation->role = $role; $newRelation->permission = $key; $newRelation->value = is_string($value) ? $value : implode(',', $value); $newRelation->save(); } return $this->redirect(['index', 'role' => $role]); } $allPermissions = Permission::findAll(); return $this->render('index', ['rolePermissions' => $rolePermissions, 'allPermissions' => $allPermissions]); }
/** * Finds the Relation model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param string $role * @param string $permission * @return Relation the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($role, $permission) { if (($model = Relation::findOne(['role' => $role, 'permission' => $permission])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }