-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 68bfe4a
Showing
33 changed files
with
2,180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
vendor | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
checks: | ||
php: | ||
code_rating: true | ||
duplication: true | ||
|
||
filter: | ||
excluded_paths: | ||
- vendor/* | ||
|
||
before_commands: | ||
- "composer install --prefer-source" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018 Andrey Girnik | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
the Software, and to permit persons to whom the Software is furnished to do so, | ||
subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
Yii2 RBAC module | ||
============== | ||
|
||
1 Introduction | ||
---------------------------- | ||
|
||
**Rbac module** -- Module for the Yii2 framework, which provides management with the next data: | ||
- Roles | ||
- Permissions | ||
|
||
2 Dependencies | ||
---------------------------- | ||
|
||
- php >= 7.1 | ||
- composer | ||
- MySql >= 5.5 | ||
|
||
3 Installation | ||
---------------------------- | ||
|
||
Via composer: | ||
|
||
```composer require "itstructure/yii2-rbac-module": "^1.0.0"``` | ||
|
||
or in section **require** of composer.json file set the following: | ||
``` | ||
"require": { | ||
"itstructure/yii2-rbac-module": "^1.0.0" | ||
} | ||
``` | ||
and command ```composer install```, if you install yii2 project extensions first, | ||
|
||
or command ```composer update```, if all yii2 project extensions are already installed. | ||
|
||
In accordance with the [documentation for Yii2](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html), set **authManager** for application: | ||
|
||
```php | ||
'components' => [ | ||
'authManager' => [ | ||
'class' => 'yii\rbac\DbManager', | ||
], | ||
// ... | ||
], | ||
``` | ||
|
||
In accordance with the [documentation for Yii2](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html), run command: | ||
|
||
```php | ||
yii migrate --migrationPath=@yii/rbac/migrations | ||
``` | ||
|
||
4 Usage | ||
---------------------------- | ||
|
||
### 4.1 Main properties | ||
|
||
- The **name** of module: ```rbac``` | ||
- The **namespace** for used classes: ```Itstructure\RbacModule```. | ||
- The **alias** to access in to module root directory: ```@rbac```. | ||
- **There is not a layout !** It's taken from application layout **main** by default **or how it is | ||
configured**. | ||
You cat set ```layout``` attribute in module by custom. | ||
- **View** component is taken by default from the framework like **yii\web\View**. You cat set | ||
**view** component in module by custom. | ||
|
||
### 4.2 Application config | ||
Base application config must be like in example below: | ||
|
||
```php | ||
use Itstructure\RbacModule\Module; | ||
use Itstructure\RbacModule\controllers\RolesController; | ||
use Itstructure\RbacModule\controllers\PermissionsController; | ||
``` | ||
```php | ||
'modules' => [ | ||
'rbac' => [ | ||
'class' => Module::class, | ||
'controllerMap' => [ | ||
'roles' => RolesController::class, | ||
'permissions' => PermissionsController::class, | ||
], | ||
], | ||
], | ||
``` | ||
|
||
### 4.3 Useful module attributes | ||
|
||
- ```loginUrl``` - set url to be redirected if you are not authorized. | ||
- ```accessRoles``` - The roles of users who are allowed access. | ||
|
||
License | ||
---------------------------- | ||
|
||
Copyright © 2018 Andrey Girnik girnikandrey@gmail.com. | ||
|
||
Licensed under the [MIT license](http://opensource.org/licenses/MIT). See LICENSE.txt for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "itstructure/yii2-rbac-module", | ||
"description": "Module to manage roles and permissions for the Yii2 Framework.", | ||
"type": "package", | ||
"keywords": ["yii2", "yii 2", "rbac", "module"], | ||
"require": { | ||
"php": ">=7.1.0", | ||
"yiisoft/yii2": "2.*", | ||
"yiisoft/yii2-bootstrap": "^2.0" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Andrey Girnik", | ||
"email": "girnikandrey@gmail.com" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"prefer-stable": true, | ||
"autoload": { | ||
"psr-4": { | ||
"Itstructure\\RbacModule\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
<?php | ||
|
||
namespace Itstructure\RbacModule; | ||
|
||
use Yii; | ||
use yii\web\View; | ||
use yii\helpers\ArrayHelper; | ||
use yii\rbac\ManagerInterface; | ||
use yii\base\{Module as BaseModule, InvalidConfigException}; | ||
use Itstructure\RbacModule\components\RbacValidateComponent; | ||
|
||
/** | ||
* Rbac module class. | ||
* | ||
* @property null|string|array $loginUrl | ||
* @property array $accessRoles | ||
* @property View $_view | ||
* @property ManagerInterface $_authManager | ||
* | ||
* @package Itstructure\RbacModule | ||
*/ | ||
class Module extends BaseModule | ||
{ | ||
/** | ||
* Login url. | ||
* | ||
* @var null|string|array | ||
*/ | ||
public $loginUrl = null; | ||
|
||
/** | ||
* Array of roles to module access. | ||
* | ||
* @var array | ||
*/ | ||
public $accessRoles = ['@']; | ||
|
||
/** | ||
* View component to render content. | ||
* | ||
* @var View | ||
*/ | ||
private $_view = null; | ||
|
||
/** | ||
* Auth manager. | ||
* | ||
* @var ManagerInterface | ||
*/ | ||
private $_authManager; | ||
|
||
/** | ||
* Module translations. | ||
* | ||
* @var array|null | ||
*/ | ||
private static $_translations = null; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
|
||
$this->_authManager = Yii::$app->authManager; | ||
|
||
if (null === $this->_authManager){ | ||
throw new InvalidConfigException('The authManager is not defined.'); | ||
} | ||
|
||
if (!$this->_authManager instanceof ManagerInterface){ | ||
throw new InvalidConfigException('The authManager must be implemented from yii\rbac\ManagerInterface.'); | ||
} | ||
|
||
Yii::setAlias('@rbac', static::getBaseDir()); | ||
|
||
if (null !== $this->loginUrl && method_exists(Yii::$app, 'getUser')) { | ||
Yii::$app->getUser()->loginUrl = $this->loginUrl; | ||
} | ||
|
||
self::registerTranslations(); | ||
|
||
/** | ||
* Set Rbac validate component | ||
*/ | ||
$this->setComponents( | ||
ArrayHelper::merge( | ||
$this->getRbacValidateComponentConfig(), | ||
$this->components | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Get the view. | ||
* | ||
* @return View | ||
*/ | ||
public function getView() | ||
{ | ||
if (null === $this->_view) { | ||
$this->_view = $this->get('view'); | ||
} | ||
|
||
return $this->_view; | ||
} | ||
|
||
/** | ||
* Returns module root directory. | ||
* | ||
* @return string | ||
*/ | ||
public static function getBaseDir(): string | ||
{ | ||
return __DIR__; | ||
} | ||
|
||
/** | ||
* Module translator. | ||
* | ||
* @param $category | ||
* @param $message | ||
* @param array $params | ||
* @param null $language | ||
* | ||
* @return string | ||
*/ | ||
public static function t($category, $message, $params = [], $language = null) | ||
{ | ||
if (null === self::$_translations){ | ||
self::registerTranslations(); | ||
} | ||
|
||
return Yii::t('modules/rbac/' . $category, $message, $params, $language); | ||
} | ||
|
||
/** | ||
* Set i18N component. | ||
* | ||
* @return void | ||
*/ | ||
private function registerTranslations(): void | ||
{ | ||
self::$_translations = [ | ||
'modules/rbac/*' => [ | ||
'class' => 'yii\i18n\PhpMessageSource', | ||
'forceTranslation' => true, | ||
'sourceLanguage' => Yii::$app->language, | ||
'basePath' => '@rbac/messages', | ||
'fileMap' => [ | ||
'modules/rbac/main' => 'main.php', | ||
'modules/rbac/roles' => 'roles.php', | ||
'modules/rbac/permissions' => 'permissions.php', | ||
], | ||
] | ||
]; | ||
|
||
Yii::$app->i18n->translations = ArrayHelper::merge( | ||
self::$_translations, | ||
Yii::$app->i18n->translations | ||
); | ||
} | ||
|
||
/** | ||
* Rbac validate component config. | ||
* | ||
* @return array | ||
*/ | ||
private function getRbacValidateComponentConfig(): array | ||
{ | ||
return [ | ||
'rbac-validate-component' => [ | ||
'class' => RbacValidateComponent::class, | ||
'authManager' => $this->_authManager, | ||
] | ||
]; | ||
} | ||
} |
Oops, something went wrong.