Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model settings, Menu Event, Permission Form Event #109

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions .github/ISSUE_REPLY_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
This project is not being actively maintained and there is no one officially offering support or help.

Only PRs are being evaluated (there is no deadline for evaluation).

Read more here: https://github.com/dektrium/yii2-user/issues/903
22 changes: 22 additions & 0 deletions .github/support.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Configuration for support-requests - https://github.com/dessant/support-requests

# Label used to mark issues as support requests
supportLabel: support

# Comment to post on issues marked as support requests, `{issue-author}` is an
# optional placeholder. Set to `false` to disable
supportComment: >
:wave: @{issue-author}, this project is not being actively maintained and there is no one officially offering support or help.
Only PRs are being evaluated (there is no deadline for evaluation).
Read more here: https://github.com/dektrium/yii2-user/issues/903
# Close issues marked as support requests
close: true

# Lock issues marked as support requests
lock: false

# Assign `off-topic` as the reason for locking. Set to `false` to disable
setLockReason: true

# Repository to extend settings from
# _extends: repo
49 changes: 47 additions & 2 deletions RbacWebModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
*/
class RbacWebModule extends BaseModule
{
const EVENT_INIT = 'init';
const EVENT_MENU = 'menu';
const EVENT_PERMISSION_FORM = 'permissionForm';

const MODEL_ASSIGNMENT = 100;
const MODEL_AUTHITEM = 101;
const MODEL_PERMISSION = 102;
const MODEL_ROLE = 103;
const MODEL_RULE = 104;
const MODEL_RULE_SEARCH = 105;
const MODEL_SEARCH = 106;

/**
* @var string
*/
Expand All @@ -29,11 +41,19 @@ class RbacWebModule extends BaseModule
*/
public $admins = [];

/**
/**
* @var string The Administrator permission name.
*/
public $adminPermission;

/**
* @var array the model settings for the module. The keys will be one of the `self::MODEL_` constants
* and the value will be the model class names you wish to set.
*
* @see `setConfig()` method for the default settings
*/
public $modelSettings = [];

/** @inheritdoc */
public function behaviors()
{
Expand All @@ -50,6 +70,26 @@ public function behaviors()
],
];
}

public function init()
{
parent::init();
$this->setConfig();
$this->trigger(self::EVENT_INIT);
}

public function setConfig()
{
$this->modelSettings = array_replace_recursive([
self::MODEL_ASSIGNMENT => 'dektrium\rbac\models\Assignment',
self::MODEL_AUTHITEM => 'dektrium\rbac\models\AuthItem',
self::MODEL_PERMISSION => 'dektrium\rbac\models\Permission',
self::MODEL_ROLE => 'dektrium\rbac\models\Role',
self::MODEL_RULE => 'dektrium\rbac\models\Rule',
self::MODEL_RULE_SEARCH => 'dektrium\rbac\models\RuleSearch',
self::MODEL_SEARCH => 'dektrium\rbac\models\Search',
], $this->modelSettings);
}

/**
* Checks access.
Expand All @@ -63,9 +103,14 @@ public function checkAccess()
if (method_exists($user, 'getIsAdmin')) {
return $user->getIsAdmin();
} else if ($this->adminPermission) {
return $this->adminPermission ? \Yii::$app->user->can($this->adminPermission) : false;
return \Yii::$app->user->can($this->adminPermission);
} else {
return isset($user->username) ? in_array($user->username, $this->admins) : false;
}
}

public function getModelClass($m)
{
return $this->modelSettings[$m];
}
}
107 changes: 54 additions & 53 deletions controllers/AssignmentController.php
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace dektrium\rbac\controllers;

use dektrium\rbac\models\Assignment;
use Yii;
use yii\web\Controller;

/**
* @author Dmitry Erofeev <[email protected]>
*/
class AssignmentController extends Controller
{
/**
* Show form with auth items for user.
*
* @param int $id
*/
public function actionAssign($id)
{
$model = Yii::createObject([
'class' => Assignment::className(),
'user_id' => $id,
]);

if ($model->load(\Yii::$app->request->post()) && $model->updateAssignments()) {
}

return \dektrium\rbac\widgets\Assignments::widget([
'model' => $model,
]);
/*$model = Yii::createObject([
'class' => Assignment::className(),
'user_id' => $id,
]);

if ($model->load(Yii::$app->request->post()) && $model->updateAssignments()) {

}

return $this->render('assign', [
'model' => $model,
]);*/
}
<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace dektrium\rbac\controllers;

use dektrium\rbac\RbacWebModule as Module;
use Yii;
use yii\web\Controller;

/**
* @author Dmitry Erofeev <[email protected]>
*/
class AssignmentController extends Controller
{
protected $modelClass = Module::MODEL_ASSIGNMENT;
/**
* Show form with auth items for user.
*
* @param int $id
*/
public function actionAssign($id)
{
$model = Yii::createObject([
'class' => $this->module->getModelClass($this->modelClass),
'user_id' => $id,
]);

if ($model->load(\Yii::$app->request->post()) && $model->updateAssignments()) {
}

return \dektrium\rbac\widgets\Assignments::widget([
'model' => $model,
]);
/*$model = Yii::createObject([
'class' => Assignment::className(),
'user_id' => $id,
]);

if ($model->load(Yii::$app->request->post()) && $model->updateAssignments()) {

}

return $this->render('assign', [
'model' => $model,
]);*/
}
}
8 changes: 4 additions & 4 deletions controllers/ItemControllerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function actionCreate()
{
/** @var \dektrium\rbac\models\Role|\dektrium\rbac\models\Permission $model */
$model = \Yii::createObject([
'class' => $this->modelClass,
'class' => $this->module->getModelClass($this->modelClass),
'scenario' => 'create',
]);

Expand All @@ -103,11 +103,11 @@ public function actionUpdate($name)
/** @var \dektrium\rbac\models\Role|\dektrium\rbac\models\Permission $model */
$item = $this->getItem($name);
$model = \Yii::createObject([
'class' => $this->modelClass,
'class' => $this->module->getModelClass($this->modelClass),
'scenario' => 'update',
'item' => $item,
]);

$this->performAjaxValidation($model);

if ($model->load(\Yii::$app->request->post()) && $model->save()) {
Expand Down Expand Up @@ -141,7 +141,7 @@ protected function performAjaxValidation(Model $model)
{
if (\Yii::$app->request->isAjax && $model->load(\Yii::$app->request->post())) {
\Yii::$app->response->format = Response::FORMAT_JSON;
echo json_encode(ActiveForm::validate($model));
\Yii::$app->response->data = json_encode(ActiveForm::validate($model));
\Yii::$app->end();
}
}
Expand Down
79 changes: 40 additions & 39 deletions controllers/PermissionController.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace dektrium\rbac\controllers;

use yii\rbac\Permission;
use yii\web\NotFoundHttpException;
use yii\rbac\Item;

/**
* @author Dmitry Erofeev <[email protected]>
*/
class PermissionController extends ItemControllerAbstract
{
/** @var string */
protected $modelClass = 'dektrium\rbac\models\Permission';

/** @var int */
protected $type = Item::TYPE_PERMISSION;

/** @inheritdoc */
protected function getItem($name)
{
$role = \Yii::$app->authManager->getPermission($name);

if ($role instanceof Permission) {
return $role;
}

throw new NotFoundHttpException;
}
<?php

/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium>
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace dektrium\rbac\controllers;

use yii\rbac\Permission;
use yii\web\NotFoundHttpException;
use yii\rbac\Item;
use dektrium\rbac\RbacWebModule as Module;

/**
* @author Dmitry Erofeev <[email protected]>
*/
class PermissionController extends ItemControllerAbstract
{
/** @var string */
protected $modelClass = Module::MODEL_PERMISSION;

/** @var int */
protected $type = Item::TYPE_PERMISSION;

/** @inheritdoc */
protected function getItem($name)
{
$role = \Yii::$app->authManager->getPermission($name);

if ($role instanceof Permission) {
return $role;
}

throw new NotFoundHttpException;
}
}
Loading