Skip to content

Commit

Permalink
Fixed access definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
eluhr committed Jan 29, 2024
1 parent c69b994 commit 3b4b63a
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 42 deletions.
3 changes: 2 additions & 1 deletion src/generators/crud/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]));
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/generators/crud/default/access_definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
/**
* permissions - create name and descriptions
*/
$permisions = [];
$permissions = [];
foreach ($actions as $k => $action){
$name = $this->getModuleId()
. '_' . $this->getControllerID()
. '_' . $action;
$description = $this->getModuleId()
. '/' . $this->getControllerID()
. '/' . $action;
$permisions[$action] = [
$permissions[$action] = [
'name' => $name,
'description' => $description,
];
Expand All @@ -48,6 +48,6 @@
}

return [
'permisions' => $permisions,
'permissions' => $permissions,
'roles' => $roles,
];
];
5 changes: 1 addition & 4 deletions src/generators/crud/default/controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@
use yii\base\InvalidConfigException;
use yii\helpers\Url;
use <?= ltrim($generator->baseControllerClass, '\\') ?>;
<?php if($generator->accessFilter): ?>
use yii\filters\AccessControl;
<?php endif; ?>
use yii\web\NotFoundHttpException;
use yii\web\Request;
use yii\web\Response;
Expand Down Expand Up @@ -175,7 +172,7 @@ public function actionUpdate(<?= $actionParams ?>)
{
$model = $this->findModel(<?= $actionParams ?>);
if ($model->load($this->request->post()) && $model->save()) {
return $this->redirect(Url::previous());
return $this->redirect(['view', <?= $urlParams ?>]);
}
return $this->render('update', ['model' => $model]);
}
Expand Down
20 changes: 10 additions & 10 deletions src/generators/crud/default/migration_access.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ class <?=$generator->migrationClass?> extends Migration
/**
* @var array controller all actions
*/
public $permisions = <?=$generator->var_export54($accessDefinitions['permisions'],' ')?>;
public $permissions = <?=$generator->var_export54($accessDefinitions['permissions'],' ')?>;

/**
* @var array roles and maping to actions/permisions
* @var array roles and maping to actions/permissions
*/
public $roles = <?=$generator->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]);
}

/**
Expand All @@ -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]);
}
}
}
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/generators/crud/default/views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -60,15 +60,15 @@
*/
$actionColumnTemplates = [];

if (\Yii::$app->user->can('<?=$permisions['view']['name']?>', ['route' => true])) {
if (\Yii::$app->user->can('<?=$permissions['view']['name']?>', ['route' => true])) {
$actionColumnTemplates[] = '{view}';
}

if (\Yii::$app->user->can('<?=$permisions['update']['name']?>', ['route' => true])) {
if (\Yii::$app->user->can('<?=$permissions['update']['name']?>', ['route' => true])) {
$actionColumnTemplates[] = '{update}';
}

if (\Yii::$app->user->can('<?=$permisions['delete']['name']?>', ['route' => true])) {
if (\Yii::$app->user->can('<?=$permissions['delete']['name']?>', ['route' => true])) {
$actionColumnTemplates[] = '{delete}';
}
<?php
Expand Down Expand Up @@ -105,7 +105,7 @@
if($generator->accessFilter){
echo "<?php\n"
?>
if(\Yii::$app->user->can('<?=$permisions['create']['name']?>', ['route' => true])){
if(\Yii::$app->user->can('<?=$permissions['create']['name']?>', ['route' => true])){
<?php
echo "?>\n"
?>
Expand Down
42 changes: 42 additions & 0 deletions src/generators/crud/default/views/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
$className = $model::class;
$modelName = Inflector::camel2words(StringHelper::basename($className));
$urlParams = $generator->generateUrlParams();
$permissions = $accessDefinitions['permissions'];

echo "<?php\n";
?>
Expand Down Expand Up @@ -60,23 +61,54 @@

<!-- menu buttons -->
<div class='pull-left'>

<?php
if ($generator->accessFilter) {
echo '<?php if(\Yii::$app->getUser()->can(\'' . $permissions['update']['name'] . '\')): ?>';
}
?>
<?= '<?php ' . PHP_EOL . ' echo ' ?>Html::a(
'<span class="glyphicon glyphicon-pencil"></span> ' . <?= $generator->generateString('Edit ' . $modelName) ?>,
[ 'update', <?= $urlParams ?>],
['class' => 'btn btn-info'])
?>
<?php
if ($generator->accessFilter) {
echo '<?php endif ?>';
}
?>

<?php
if ($generator->accessFilter) {
echo '<?php if(\Yii::$app->getUser()->can(\'' . $permissions['update']['name'] . '\')): ?>';
}
?>
<?= '<?php ' . PHP_EOL . ' echo ' ?>Html::a(
'<span class="glyphicon glyphicon-copy"></span> ' . <?= $generator->generateString('Copy ' . $modelName) ?>,
['create', <?= $urlParams ?>, '<?= StringHelper::basename($generator->modelClass) ?>'=>$copyParams],
['class' => 'btn btn-success'])
?>
<?php
if ($generator->accessFilter) {
echo '<?php endif ?>';
}
?>

<?php
if ($generator->accessFilter) {
echo '<?php if(\Yii::$app->getUser()->can(\'' . $permissions['create']['name'] . '\')): ?>';
}
?>
<?= '<?php ' . PHP_EOL . ' echo ' ?>Html::a(
'<span class="glyphicon glyphicon-plus"></span> ' . <?= $generator->generateString('New ' . $modelName) ?>,
['create'],
['class' => 'btn btn-success'])
?>
<?php
if ($generator->accessFilter) {
echo '<?php endif ?>';
}
?>
</div>

<div class="pull-right">
Expand Down Expand Up @@ -115,6 +147,11 @@

<hr/>

<?php
if ($generator->accessFilter) {
echo '<?php if(\Yii::$app->getUser()->can(\'' . $permissions['delete']['name'] . '\')): ?>';
}
?>
<?= '<?php ' . PHP_EOL . ' echo ' ?>Html::a('<span class="glyphicon glyphicon-trash"></span> '
. <?= $generator->generateString(
'Delete ' . $modelName
Expand All @@ -125,6 +162,11 @@
'data-method' => 'post',
]);
?>
<?php
if ($generator->accessFilter) {
echo '<?php endif ?>';
}
?>
<?= "<?php \$this->endBlock(); ?>\n\n"; ?>

<?php
Expand Down
8 changes: 4 additions & 4 deletions src/generators/crud/editable/access_definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
/**
* permissions - create name and descriptions
*/
$permisions = [];
$permissions = [];
foreach ($actions as $k => $action){
$name = $this->getModuleId()
. '_' . $this->getControllerID()
. '_' . $action;
$description = $this->getModuleId()
. '/' . $this->getControllerID()
. '/' . $action;
$permisions[$action] = [
$permissions[$action] = [
'name' => $name,
'description' => $description,
];
Expand Down Expand Up @@ -76,6 +76,6 @@
}

return [
'permisions' => $permisions,
'permissions' => $permissions,
'roles' => $roles,
];
];
20 changes: 10 additions & 10 deletions src/generators/crud/editable/migration_access.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ class <?=$generator->migrationClass?> extends Migration
/**
* @var array controller all actions
*/
public $permisions = <?=$generator->var_export54($accessDefinitions['permisions'],' ')?>;
public $permissions = <?=$generator->var_export54($accessDefinitions['permissions'],' ')?>;

/**
* @var array roles and maping to actions/permisions
* @var array roles and maping to actions/permissions
*/
public $roles = <?=$generator->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]);
}

/**
Expand All @@ -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]);
}
}
}
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions src/generators/crud/editable/views/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@
*/
$actionColumnTemplates = [];

if (\Yii::$app->user->can('<?=$permisions['view']['name']?>')) {
if (\Yii::$app->user->can('<?=$permissions['view']['name']?>')) {
$actionColumnTemplates[] = '{view}';
}

if (\Yii::$app->user->can('<?=$permisions['update']['name']?>')) {
if (\Yii::$app->user->can('<?=$permissions['update']['name']?>')) {
$actionColumnTemplates[] = '{update}';
}

if (\Yii::$app->user->can('<?=$permisions['delete']['name']?>')) {
if (\Yii::$app->user->can('<?=$permissions['delete']['name']?>')) {
$actionColumnTemplates[] = '{delete}';
}
<?php
Expand Down Expand Up @@ -105,7 +105,7 @@
if($generator->accessFilter){
echo "<?php\n"
?>
if(\Yii::$app->user->can('<?=$permisions['create']['name']?>')){
if(\Yii::$app->user->can('<?=$permissions['create']['name']?>')){
<?php
echo "?>\n"
?>
Expand Down

0 comments on commit 3b4b63a

Please sign in to comment.