public static function AddBatchItems($role, $permissions) { self::deleteAll(['role' => $role]); foreach ($permissions as $key => $value) { $newRelation = new Relation(); $newRelation->role = $role; $newRelation->permission = $key; $newRelation->value = is_string($value) ? $value : implode(',', $value); $newRelation->save(); } }
public function init() { parent::init(); $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 return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['like', 'role', $this->role])->andFilterWhere(['like', 'permission', $this->permission])->andFilterWhere(['like', 'value', $this->value]); return $dataProvider; }
public function actionRelation($role) { if (\Yii::$app->request->isPost) { $selectedPermissions = LuLu::getPostValue('Permission'); Relation::AddBatchItems($role, $selectedPermissions); return $this->redirect(['index', 'role' => $role]); } $allPermissions = Permission::getAllPermissionsGroupedByCategory(); $rolePermissions = Relation::find()->select(['permission', 'value'])->where(['role' => $role])->indexBy('permission')->all(); $categories = Permission::getCategoryItems(); $role = Role::findOne(['id' => $role]); return $this->render('relation', ['rolePermissions' => $rolePermissions, 'allPermissions' => $allPermissions, 'categories' => $categories, 'role' => $role]); }
/** * 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.'); } }
public function afterSave($insert, $changedAttributes) { parent::afterSave($insert, $changedAttributes); if (!$insert) { if (isset($changedAttributes['id'])) { Relation::updateAll(['permission' => $this->id], ['permission' => $changedAttributes['id']]); } } }