Skip to content

Commit

Permalink
update views. role and permission seperate
Browse files Browse the repository at this point in the history
  • Loading branch information
JamshidbekAkhlidinov committed Feb 11, 2024
1 parent b45335a commit 7c9968e
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 41 deletions.
23 changes: 13 additions & 10 deletions modules/admin/components/menu/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace app\modules\admin\components\menu;

use app\modules\admin\enums\AuthItemTypeEnum;
use app\modules\admin\enums\UserRolesEnum;
use app\modules\admin\widgets\MenuWidget;

Expand Down Expand Up @@ -122,26 +123,28 @@ public static function getRbacMenu()
'id' => 'rbac',
'type' => MenuWidget::type_item,
'items' => [
// [
// 'label' => translate("Rules"),
// 'url' => ['/admin/rbac/auth-rule'],
// 'class' => $controller_id == 'auth-rule',
// ],
[
'label' => translate("Items"),
'url' => ['/admin/rbac/auth-item'],
'active' => $controller_id == 'auth-item',
'label' => translate("Roles"),
'url' => ['/admin/rbac/auth-item', 'type' => AuthItemTypeEnum::ROLE],
'active' => $controller_id == 'auth-item' && get('type') == AuthItemTypeEnum::ROLE,
],
[
'label' => translate("Items child"),
'label' => translate("Permissions"),
'url' => ['/admin/rbac/auth-item', 'type' => AuthItemTypeEnum::PERMISSION],
'active' => $controller_id == 'auth-item' && get('type') == AuthItemTypeEnum::PERMISSION,
],
[
'label' => translate("Items childs"),
'url' => ['/admin/rbac/auth-item-child'],
'active' => $controller_id == 'auth-item-child',
],
/*
[
'label' => translate("Assignment"),
'label' => translate("Assignments"),
'url' => ['/admin/rbac/auth-assignment'],
'active' => $controller_id == 'auth-assignment',
],
*/
]
];
}
Expand Down
21 changes: 21 additions & 0 deletions modules/admin/enums/AuthItemTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/*
* Jamshidbek Akhlidinov
* 11 - 2 2024 17:31:17
* https://ustadev.uz
* https://github.com/JamshidbekAkhlidinov
*/

namespace app\modules\admin\enums;

interface AuthItemTypeEnum
{
public const ROLE = 1;
public const PERMISSION = 2;

public const ALL = [
self::ROLE => "Role",
self::PERMISSION => "Permission",
];

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function create()
'class' => "btn btn-success",
'label' => icon("folder-add"),
],
'url' => ['auth-item-child/create'],
'url' => ['auth-item-child/create', 'parent' => get('parent')],
'footer' => '',
'header' => "<h2>" . translate('Auth Item Child Form') . "</h2>"
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function behaviors()
*
* @return string
*/
public function actionIndex()
public function actionIndex($parent = null)
{
$searchModel = new AuthItemChildSearch();
$searchModel = new AuthItemChildSearch(['parent' => $parent]);
$dataProvider = $searchModel->search($this->request->queryParams);

return $this->render('index', [
Expand Down Expand Up @@ -66,20 +66,21 @@ public function actionView($parent, $child)
* If creation is successful, the browser will be redirected to the 'view' page.
* @return string|\yii\web\Response
*/
public function actionCreate()
public function actionCreate($parent = null)
{
$model = new AuthItemChild();

if ($this->request->isPost) {
if ($model->load($this->request->post()) && $model->save()) {
return $this->redirect(['index']);
return $this->redirect(['index', 'parent' => $parent]);
}
} else {
$model->loadDefaultValues();
}

return $this->renderAjax('_form', [
'model' => $model,
'parent' => $parent,
]);
}

Expand Down
5 changes: 3 additions & 2 deletions modules/admin/modules/rbac/controllers/AuthItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace app\modules\admin\modules\rbac\controllers;

use app\modules\admin\enums\AuthItemTypeEnum;
use app\modules\admin\modules\rbac\models\AuthItem;
use app\modules\admin\modules\rbac\search\AuthItem as AuthItemSearch;
use yii\web\Controller;
Expand Down Expand Up @@ -36,9 +37,9 @@ public function behaviors()
*
* @return string
*/
public function actionIndex()
public function actionIndex($type = AuthItemTypeEnum::ROLE)
{
$searchModel = new AuthItemSearch();
$searchModel = new AuthItemSearch(['type' => $type]);
$dataProvider = $searchModel->search($this->request->queryParams);

return $this->render('index', [
Expand Down
15 changes: 12 additions & 3 deletions modules/admin/modules/rbac/query/AuthItemQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@

namespace app\modules\admin\modules\rbac\query;

use app\modules\admin\enums\AuthItemTypeEnum;
use app\modules\admin\enums\UserRolesEnum;
use Cassandra\Type\UserType;

/**
* This is the ActiveQuery class for [[\app\modules\admin\modules\rbac\models\AuthItem]].
*
* @see \app\modules\admin\modules\rbac\models\AuthItem
*/
class AuthItemQuery extends \yii\db\ActiveQuery
{
/*public function active()
public function roles()
{
return $this->andWhere('[[status]]=1');
}*/
return $this->andWhere(['type' => AuthItemTypeEnum::ROLE]);
}

public function permissions()
{
return $this->andWhere(['type' => AuthItemTypeEnum::PERMISSION]);
}

/**
* {@inheritdoc}
Expand Down
4 changes: 4 additions & 0 deletions modules/admin/modules/rbac/search/AuthAssignmentSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public function search($params)
{
$query = AuthAssignment::find();

$query->select(['user_id','count(user_id) as count']);

$query->groupBy(['user_id']);

// add conditions that should always apply here

$dataProvider = new ActiveDataProvider([
Expand Down
4 changes: 3 additions & 1 deletion modules/admin/modules/rbac/search/AuthItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function scenarios()
public function search($params)
{
$query = AuthItemModel::find();

if ($type = $this->type) {
$query->andWhere(['type' => $type]);
}
// add conditions that should always apply here

$dataProvider = new ActiveDataProvider([
Expand Down
14 changes: 7 additions & 7 deletions modules/admin/modules/rbac/views/auth-assignment/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@
'columns' => [
['class' => 'yii\grid\SerialColumn'],

[
'attribute' => 'item_name',
'format' => 'raw',
'value' => function ($model) {
return AuthAssignmentButtons::update($model->item_name, $model->user_id);
}
],
[
'attribute' => 'user_id',
'value' => function ($data) {
$user = User::findOne(['id' => $data->user_id]);
return $user->publicIdentity;
},
],
[
'attribute' => 'item_name',
'format' => 'raw',
'value' => function ($model) {
return AuthAssignmentButtons::update($model->item_name, $model->user_id);
}
],
'created_at:datetime',
[
'class' => ActionColumn::className(),
Expand Down
31 changes: 21 additions & 10 deletions modules/admin/modules/rbac/views/auth-item-child/_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,32 @@
use yii\helpers\Html;
use yii\widgets\ActiveForm;

/** @var yii\web\View $this */
/** @var app\modules\admin\modules\rbac\models\AuthItemChild $model */
/** @var yii\widgets\ActiveForm $form */
/**
* @var yii\web\View $this
* @var app\modules\admin\modules\rbac\models\AuthItemChild $model
* @var yii\widgets\ActiveForm $form
* @var $parent
* */

?>

<div class="auth-item-child-form">

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'parent')->dropDownList(
ModelToData::getAuthItems(),
[
'prompt' => translate('--Select--')
]
) ?>
<?= $form->errorSummary($model) ?>
<?php
if ($parent) {
echo $form->field($model, 'parent')->hiddenInput(['value' => $parent])->label(false);
} else {
echo $form->field($model, 'parent')->dropDownList(
ModelToData::getAuthItems(),
[
'prompt' => translate('--Select--'),
]
);
}

?>

<?= $form->field($model, 'child')->dropDownList(
ModelToData::getAuthItems(),
Expand Down
12 changes: 9 additions & 3 deletions modules/admin/modules/rbac/views/auth-item/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@


<?php

use app\modules\admin\enums\AuthItemTypeEnum;
use app\modules\admin\modules\rbac\components\buttons\AuthItemButtons;
use app\modules\admin\modules\rbac\models\AuthItem;
use yii\helpers\Html;
Expand Down Expand Up @@ -42,8 +41,15 @@
],
[
'attribute' => 'type',
'format' => 'raw',
'value' => static function ($item) {
return $item->type == 1 ? 'Role' : 'Permission';
return Html::a(
AuthItemTypeEnum::ALL[$item->type] ?? "",
[
'auth-item-child/index',
'parent' => $item->name
]
);
}
],
'description:ntext',
Expand Down

0 comments on commit 7c9968e

Please sign in to comment.