From 3b4b63a6478820aeb0309bfd7595c3b5d97b2d24 Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Mon, 29 Jan 2024 19:20:52 +0100 Subject: [PATCH] Fixed access definitions --- src/generators/crud/Generator.php | 3 +- .../crud/default/access_definition.php | 8 ++-- src/generators/crud/default/controller.php | 5 +-- .../crud/default/migration_access.php | 20 ++++----- src/generators/crud/default/views/index.php | 10 ++--- src/generators/crud/default/views/view.php | 42 +++++++++++++++++++ .../crud/editable/access_definition.php | 8 ++-- .../crud/editable/migration_access.php | 20 ++++----- src/generators/crud/editable/views/index.php | 8 ++-- 9 files changed, 82 insertions(+), 42 deletions(-) diff --git a/src/generators/crud/Generator.php b/src/generators/crud/Generator.php index 45ba427c..3166489f 100644 --- a/src/generators/crud/Generator.php +++ b/src/generators/crud/Generator.php @@ -395,7 +395,8 @@ public function generate() if (is_file($templatePath.'/'.$file) && pathinfo($file, PATHINFO_EXTENSION) === 'php') { $files[] = new CodeFile("$viewPath/$file", $this->render("views/$file", [ 'model' => $model, - 'safeAttributes' => $safeAttributes + 'safeAttributes' => $safeAttributes, + 'accessDefinitions' => $accessDefinitions ])); } } diff --git a/src/generators/crud/default/access_definition.php b/src/generators/crud/default/access_definition.php index e7b10baf..2b1dcaa9 100644 --- a/src/generators/crud/default/access_definition.php +++ b/src/generators/crud/default/access_definition.php @@ -13,7 +13,7 @@ /** * permissions - create name and descriptions */ -$permisions = []; +$permissions = []; foreach ($actions as $k => $action){ $name = $this->getModuleId() . '_' . $this->getControllerID() @@ -21,7 +21,7 @@ $description = $this->getModuleId() . '/' . $this->getControllerID() . '/' . $action; - $permisions[$action] = [ + $permissions[$action] = [ 'name' => $name, 'description' => $description, ]; @@ -48,6 +48,6 @@ } return [ - 'permisions' => $permisions, + 'permissions' => $permissions, 'roles' => $roles, -]; \ No newline at end of file +]; diff --git a/src/generators/crud/default/controller.php b/src/generators/crud/default/controller.php index 5b8f2967..3334f9f8 100644 --- a/src/generators/crud/default/controller.php +++ b/src/generators/crud/default/controller.php @@ -50,9 +50,6 @@ use yii\base\InvalidConfigException; use yii\helpers\Url; use baseControllerClass, '\\') ?>; -accessFilter): ?> -use yii\filters\AccessControl; - use yii\web\NotFoundHttpException; use yii\web\Request; use yii\web\Response; @@ -175,7 +172,7 @@ public function actionUpdate() { $model = $this->findModel(); if ($model->load($this->request->post()) && $model->save()) { - return $this->redirect(Url::previous()); + return $this->redirect(['view', ]); } return $this->render('update', ['model' => $model]); } diff --git a/src/generators/crud/default/migration_access.php b/src/generators/crud/default/migration_access.php index 3e708e85..0a841eea 100644 --- a/src/generators/crud/default/migration_access.php +++ b/src/generators/crud/default/migration_access.php @@ -13,26 +13,26 @@ class migrationClass?> extends Migration /** * @var array controller all actions */ - public $permisions = var_export54($accessDefinitions['permisions'],' ')?>; + public $permissions = var_export54($accessDefinitions['permissions'],' ')?>; /** - * @var array roles and maping to actions/permisions + * @var array roles and maping to actions/permissions */ public $roles = var_export54($accessDefinitions['roles'],' ')?>; public function up() { - $permisions = []; + $permissions = []; $auth = \Yii::$app->authManager; /** - * create permisions for each controller action + * create permissions for each controller action */ - foreach ($this->permisions as $action => $permission) { - $permisions[$action] = $auth->createPermission($permission['name']); - $permisions[$action]->description = $permission['description']; - $auth->add($permisions[$action]); + foreach ($this->permissions as $action => $permission) { + $permissions[$action] = $auth->createPermission($permission['name']); + $permissions[$action]->description = $permission['description']; + $auth->add($permissions[$action]); } /** @@ -46,7 +46,7 @@ public function up() * to role assign permissions */ foreach ($actions as $action) { - $auth->addChild($role, $permisions[$action]); + $auth->addChild($role, $permissions[$action]); } } } @@ -59,7 +59,7 @@ public function down() { $auth->remove($role); } - foreach ($this->permisions as $permission) { + foreach ($this->permissions as $permission) { $authItem = $auth->createPermission($permission['name']); $auth->remove($authItem); } diff --git a/src/generators/crud/default/views/index.php b/src/generators/crud/default/views/index.php index 659640ca..de3a3057 100644 --- a/src/generators/crud/default/views/index.php +++ b/src/generators/crud/default/views/index.php @@ -6,11 +6,11 @@ /** * @var yii\web\View $this * @var schmunk42\giiant\generators\crud\Generator $generator - * @var array $permisions */ $urlParams = $generator->generateUrlParams(); $nameAttribute = $generator->getNameAttribute(); +$permissions = $accessDefinitions['permissions']; /** @var \yii\db\ActiveRecord $model */ $model = new $generator->modelClass(); @@ -60,15 +60,15 @@ */ $actionColumnTemplates = []; -if (\Yii::$app->user->can('', ['route' => true])) { +if (\Yii::$app->user->can('', ['route' => true])) { $actionColumnTemplates[] = '{view}'; } -if (\Yii::$app->user->can('', ['route' => true])) { +if (\Yii::$app->user->can('', ['route' => true])) { $actionColumnTemplates[] = '{update}'; } -if (\Yii::$app->user->can('', ['route' => true])) { +if (\Yii::$app->user->can('', ['route' => true])) { $actionColumnTemplates[] = '{delete}'; } accessFilter){ echo " -if(\Yii::$app->user->can('', ['route' => true])){ +if(\Yii::$app->user->can('', ['route' => true])){ \n" ?> diff --git a/src/generators/crud/default/views/view.php b/src/generators/crud/default/views/view.php index 76011100..efde802f 100644 --- a/src/generators/crud/default/views/view.php +++ b/src/generators/crud/default/views/view.php @@ -27,6 +27,7 @@ $className = $model::class; $modelName = Inflector::camel2words(StringHelper::basename($className)); $urlParams = $generator->generateUrlParams(); +$permissions = $accessDefinitions['permissions']; echo " @@ -60,23 +61,54 @@
+ + accessFilter) { + echo 'getUser()->can(\'' . $permissions['update']['name'] . '\')): ?>'; + } + ?> Html::a( ' ' . generateString('Edit ' . $modelName) ?>, [ 'update', ], ['class' => 'btn btn-info']) ?> + accessFilter) { + echo ''; + } + ?> + accessFilter) { + echo 'getUser()->can(\'' . $permissions['update']['name'] . '\')): ?>'; + } + ?> Html::a( ' ' . generateString('Copy ' . $modelName) ?>, ['create', , 'modelClass) ?>'=>$copyParams], ['class' => 'btn btn-success']) ?> + accessFilter) { + echo ''; + } + ?> + accessFilter) { + echo 'getUser()->can(\'' . $permissions['create']['name'] . '\')): ?>'; + } + ?> Html::a( ' ' . generateString('New ' . $modelName) ?>, ['create'], ['class' => 'btn btn-success']) ?> + accessFilter) { + echo ''; + } + ?>
@@ -115,6 +147,11 @@
+ accessFilter) { + echo 'getUser()->can(\'' . $permissions['delete']['name'] . '\')): ?>'; + } + ?> Html::a(' ' . generateString( 'Delete ' . $modelName @@ -125,6 +162,11 @@ 'data-method' => 'post', ]); ?> + accessFilter) { + echo ''; + } + ?> endBlock(); ?>\n\n"; ?> $action){ $name = $this->getModuleId() . '_' . $this->getControllerID() @@ -30,7 +30,7 @@ $description = $this->getModuleId() . '/' . $this->getControllerID() . '/' . $action; - $permisions[$action] = [ + $permissions[$action] = [ 'name' => $name, 'description' => $description, ]; @@ -76,6 +76,6 @@ } return [ - 'permisions' => $permisions, + 'permissions' => $permissions, 'roles' => $roles, -]; \ No newline at end of file +]; diff --git a/src/generators/crud/editable/migration_access.php b/src/generators/crud/editable/migration_access.php index 3e708e85..0a841eea 100644 --- a/src/generators/crud/editable/migration_access.php +++ b/src/generators/crud/editable/migration_access.php @@ -13,26 +13,26 @@ class migrationClass?> extends Migration /** * @var array controller all actions */ - public $permisions = var_export54($accessDefinitions['permisions'],' ')?>; + public $permissions = var_export54($accessDefinitions['permissions'],' ')?>; /** - * @var array roles and maping to actions/permisions + * @var array roles and maping to actions/permissions */ public $roles = var_export54($accessDefinitions['roles'],' ')?>; public function up() { - $permisions = []; + $permissions = []; $auth = \Yii::$app->authManager; /** - * create permisions for each controller action + * create permissions for each controller action */ - foreach ($this->permisions as $action => $permission) { - $permisions[$action] = $auth->createPermission($permission['name']); - $permisions[$action]->description = $permission['description']; - $auth->add($permisions[$action]); + foreach ($this->permissions as $action => $permission) { + $permissions[$action] = $auth->createPermission($permission['name']); + $permissions[$action]->description = $permission['description']; + $auth->add($permissions[$action]); } /** @@ -46,7 +46,7 @@ public function up() * to role assign permissions */ foreach ($actions as $action) { - $auth->addChild($role, $permisions[$action]); + $auth->addChild($role, $permissions[$action]); } } } @@ -59,7 +59,7 @@ public function down() { $auth->remove($role); } - foreach ($this->permisions as $permission) { + foreach ($this->permissions as $permission) { $authItem = $auth->createPermission($permission['name']); $auth->remove($authItem); } diff --git a/src/generators/crud/editable/views/index.php b/src/generators/crud/editable/views/index.php index 19112894..34264164 100644 --- a/src/generators/crud/editable/views/index.php +++ b/src/generators/crud/editable/views/index.php @@ -56,15 +56,15 @@ */ $actionColumnTemplates = []; -if (\Yii::$app->user->can('')) { +if (\Yii::$app->user->can('')) { $actionColumnTemplates[] = '{view}'; } -if (\Yii::$app->user->can('')) { +if (\Yii::$app->user->can('')) { $actionColumnTemplates[] = '{update}'; } -if (\Yii::$app->user->can('')) { +if (\Yii::$app->user->can('')) { $actionColumnTemplates[] = '{delete}'; } accessFilter){ echo " -if(\Yii::$app->user->can('')){ +if(\Yii::$app->user->can('')){ \n" ?>