Skip to content

Commit

Permalink
added composer package "dmstr/yii2-web": "~0.1" and use AccessBehavio…
Browse files Browse the repository at this point in the history
…rTrait, code doc and .gitignore updated
  • Loading branch information
Chris Stebe committed Apr 8, 2016
1 parent a961afa commit 629beab
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 85 deletions.
34 changes: 6 additions & 28 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace dmstr\modules\pages;

use dmstr\modules\pages\models\Tree;
use yii\filters\AccessControl;
use dmstr\web\traits\AccessBehaviorTrait;

/**
* Class Module
Expand All @@ -19,6 +19,8 @@
*/
class Module extends \yii\base\Module
{
use AccessBehaviorTrait;

/**
* @var array the list of rights that are allowed to access this module.
* If you modify, you also need to enable authManager.
Expand All @@ -32,36 +34,9 @@ class Module extends \yii\base\Module

public $availableViews = [];


/**
* Restrict access permissions to admin user and users with auth-item 'module-controller'
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'matchCallback' => function () {
if ($this->roles) {
foreach ($this->roles as $role) {
if (\Yii::$app->user->can($role)) {
return true;
}
}
return (\Yii::$app->user->identity && \Yii::$app->user->identity->isAdmin);
}
return true;
},
]
]
]
];
}

public function init()
{
parent::init();
Expand All @@ -80,6 +55,9 @@ public function init()
}
}

/**
* @return mixed|object dmstr\modules\pages\models\Tree
*/
public function getLocalizedRootNode()
{
$localizedRoot = 'root_' . \Yii::$app->language;
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"kartik-v/yii2-tree-manager": "~1.0.3",
"kartik-v/yii2-widget-select2": "^2.0.1",
"rmrevin/yii2-fontawesome": "~2.9",
"devgroup/yii2-jsoneditor": "1.0.*"
"devgroup/yii2-jsoneditor": "1.0.*",
"dmstr/yii2-web": "~0.1"
},
"autoload": {
"psr-4": {
Expand Down
31 changes: 2 additions & 29 deletions controllers/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use dmstr\modules\pages\models\Tree;
use Yii;
use yii\filters\AccessControl;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\HttpException;
Expand All @@ -20,39 +19,13 @@
/**
* Class DefaultController
* @package dmstr\modules\pages\controllers
* @author $Author
* @author Christopher Stebe <c.stebe@herzogkommunikation.de>
*/
class DefaultController extends Controller
{
/**
* @var boolean whether to enable CSRF validation for the actions in this controller.
* CSRF validation is enabled only when both this property and [[Request::enableCsrfValidation]] are true.
* @return mixed
*/
public $enableCsrfValidation = false;

/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'matchCallback' => function ($rule, $action) {
return \Yii::$app->user->can(
$this->module->id . '_' . $this->id . '_' . $action->id,
['route' => true]
);
},
]
]
]
];
}

public function actionIndex()
{

Expand Down
33 changes: 6 additions & 27 deletions controllers/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,19 @@

namespace dmstr\modules\pages\controllers;


use dmstr\modules\pages\models\Tree;
use yii\filters\AccessControl;
use yii\web\Controller;

/**
* Class TestController
* @package dmstr\modules\pages\controllers
* @author $Author
*/
class TestController extends Controller
{

/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'matchCallback' => function ($rule, $action) {
return \Yii::$app->user->can(
$this->module->id.'_'.$this->id.'_'.$action->id,
['route' => true]
);
},
]
]
]
];
}

public function actionIndex()
{
$tree = Tree::getMenuItems('root_'.\Yii::$app->language);
$tree = Tree::getMenuItems('root_' . \Yii::$app->language);
return $this->render('index', ['tree' => $tree]);
}
}
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
_output

tests/_output/*

0 comments on commit 629beab

Please sign in to comment.