Skip to content

Добавление роутингов приложения #53

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions components/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

namespace developeruz\db_rbac\components;

use Yii;
use Exception;
use yii\helpers\Inflector;

/**
* Description of Route
*
*/
class Route
{
//put your code here

/**
* Lists all Route models.
*
* @return mixed
*/
public static function getAppRoutes()
{
$result = [];
static::getRouteRecrusive(Yii::$app, $result);

return $result;
}

/**
* Get route(s) recrusive
*
* @param \yii\base\Module $module
* @param array $result
*/
private static function getRouteRecrusive($module, &$result)
{
foreach ($module->getModules() as $id => $child) {
if (($child = $module->getModule($id)) !== null) {
static::getRouteRecrusive($child, $result);
}
}
foreach ($module->controllerMap as $id => $type) {
static::getControllerActions($type, $id, $module, $result);
}
$namespace = trim($module->controllerNamespace, '\\') . '\\';
static::getControllerFiles($module, $namespace, '', $result);
$result[] = ($module->uniqueId === '' ? '' : '/' . $module->uniqueId) . '/*';
}

/**
* Get list controller under module
*
* @param \yii\base\Module $module
* @param string $namespace
* @param string $prefix
* @param mixed $result
*
* @return mixed
*/
private static function getControllerFiles($module, $namespace, $prefix,
&$result)
{
$path = @Yii::getAlias('@' . str_replace('\\', '/', $namespace));
if (!is_dir($path)) {
return;
}
foreach (scandir($path) as $file) {
if ($file == '.' || $file == '..') {
continue;
}
if (is_dir($path . '/' . $file)) {
static::getControllerFiles($module, $namespace . $file . '\\', $prefix . $file . '/', $result);
} elseif (strcmp(substr($file, -14), 'Controller.php') === 0) {
$id = Inflector::camel2id(substr(basename($file), 0, -14));
$className = $namespace . Inflector::id2camel($id) . 'Controller';
if (strpos($className, '-') === false && class_exists($className)
&& is_subclass_of($className, 'yii\base\Controller')) {
static::getControllerActions($className, $prefix . $id, $module, $result);
}
}
}
}

/**
* Get list action of controller
*
* @param mixed $type
* @param string $id
* @param \yii\base\Module $module
* @param string $result
*/
private static function getControllerActions($type, $id, $module, &$result)
{
/* @var $controller \yii\base\Controller */
$controller = Yii::createObject($type, [$id, $module]);
static::getActionRoutes($controller, $result);
$result[] = '/' . $controller->uniqueId . '/*';
}

/**
* Get route of action
*
* @param \yii\base\Controller $controller
* @param array $result all controller action.
*/
private static function getActionRoutes($controller, &$result)
{
$prefix = '/' . $controller->uniqueId . '/';
foreach ($controller->actions() as $id => $value) {
$result[] = $prefix . $id;
}
$class = new \ReflectionClass($controller);
foreach ($class->getMethods() as $method) {
$name = $method->getName();
if ($method->isPublic() && !$method->isStatic() && strpos($name, 'action') === 0
&& $name !== 'actions') {
$result[] = $prefix . Inflector::camel2id(substr($name, 6));
}
}
}
}
27 changes: 22 additions & 5 deletions views/access/addPermission.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php

namespace developeruz\db_rbac\views\access;

use Yii;
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use frontend\components\Route;

/* @var $this yii\web\View */
/* @var $model common\models\Links */
/* @var $form yii\widgets\ActiveForm */
$this->title = Yii::t('db_rbac', 'Новое правило');
$this->params['breadcrumbs'][] = ['label' => Yii::t('db_rbac', 'Правила доступа'), 'url' => ['permission']];
$this->title = Yii::t('db_rbac', 'Новое правило');
$this->params['breadcrumbs'][] = ['label' => Yii::t('db_rbac', 'Правила доступа'),
'url' => ['permission']];
$this->params['breadcrumbs'][] = Yii::t('db_rbac', 'Новое правило');
?>
<div class="news-index">
Expand All @@ -25,10 +28,21 @@
echo implode('<br>', $error);
?>
</div>
<?php
<?php
}
?>

<h3>Роутинг приложения</h3>
<p>
<code style="height: 300px; display: block; overflow-y: auto">
<?php
foreach (Route::getAppRoutes()as $value) {
echo $value . '<br/>';
}
?>
</code>
</p>

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

<div class="form-group">
Expand All @@ -39,11 +53,14 @@
<div class="form-group">
<?= Html::label(Yii::t('db_rbac', 'Разрешенный доступ')); ?>
<?= Html::textInput('name'); ?>
<?=Yii::t('db_rbac', '<br>* Формат: <strong>module/controller/action</strong><br><strong>site/article</strong> - доступ к странице "site/article"<br><strong>site</strong> - доступ к любым action контроллера "site"');?>
<?= Yii::t('db_rbac', '<br>* Формат: <strong>module/controller/action</strong><br><strong>site/article</strong> - доступ к странице "site/article"<br><strong>site</strong> - доступ к любым action контроллера "site"'); ?>
</div>

<div class="form-group">
<?= Html::submitButton(Yii::t('db_rbac', 'Сохранить'), ['class' => 'btn btn-success']) ?>
<?=
Html::submitButton(Yii::t('db_rbac', 'Сохранить'), [
'class' => 'btn btn-success'])
?>
</div>

<?php ActiveForm::end(); ?>
Expand Down