Skip to content

Commit 7df2ce1

Browse files
Большое обновление CMS + CRM
1 parent d142333 commit 7df2ce1

4 files changed

Lines changed: 236 additions & 78 deletions

File tree

src/console/controllers/InitController.php

Lines changed: 192 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use skeeks\cms\admin\AdminComponent;
1212
use skeeks\cms\backend\IBackendComponent;
1313
use skeeks\cms\IHasPermissions;
14+
use skeeks\cms\models\CmsContent;
1415
use skeeks\cms\models\User;
1516
use skeeks\cms\modules\admin\controllers\AdminController;
1617
use skeeks\cms\rbac\CmsManager;
18+
use skeeks\cms\rbac\models\CmsAuthItem;
1719
use Yii;
1820
use yii\base\Exception;
1921
use yii\console\Controller;
@@ -25,32 +27,63 @@
2527
use yii\rbac\Rule;
2628

2729
/**
28-
* Setting permissions
29-
*
30-
* @author Semenov Alexander <[email protected]>
30+
* Работа с правами доступа и ролями
3131
*/
3232
class InitController extends Controller
3333
{
34+
/**
35+
* @var bool Перезагрузить все привелегии
36+
* 0 - нет
37+
* 1 - да
38+
*/
39+
public $reload_perrmions = 0;
40+
3441
/**
3542
* @var string the default command action.
3643
*/
3744
public $defaultAction = 'init';
3845

46+
public function options($actionID)
47+
{
48+
// $actionId might be used in subclasses to provide options specific to action id
49+
return ArrayHelper::merge(parent::options($actionID), [
50+
'reload_perrmions',
51+
]);
52+
}
53+
3954
/**
40-
* Загрузка конфига и применение правил
55+
* Применить все привилегии и роли
4156
*/
4257
public function actionInit()
4358
{
44-
$this->initRbacModules();
45-
$this->initBackendData();
46-
$this->initRootAssigning();
47-
$this->initRootUser();
59+
if ($this->reload_perrmions == 1) {
60+
$this->actionClearPermissions();
61+
}
62+
63+
$this->actionInitBackendData();
64+
65+
$this->actionInitRbacModules();
66+
67+
$this->actionInitRootAssigning();
68+
69+
$this->actionInitRootUser();
70+
}
71+
72+
/**
73+
* Удаление всех привилегий
74+
* @return void
75+
*/
76+
public function actionClearPermissions()
77+
{
78+
$this->stdout("Удаление привилегий\n", Console::FG_YELLOW);
79+
$deleted = CmsAuthItem::deleteAll(['type' => 2]);
80+
$this->stdout("\tУдалено: {$deleted}\n");
4881
}
4982

5083
/**
5184
* Получение rules, permissions and data по всем расширениям и модулям
5285
*/
53-
public function initRbacModules()
86+
public function actionInitRbacModules()
5487
{
5588
$this->stdout("Init rules, permissions adn data from all modules and extensions\n", Console::BOLD);
5689
$this->stdout("\t1) Loading config\n", Console::FG_YELLOW);
@@ -72,6 +105,36 @@ public function loadConfig()
72105
{
73106
$config = \Yii::$app->authManager->config;
74107

108+
/*$q = CmsContent::find()->andWhere(['base_role' != CmsContent::ROLE_PRODUCTS]);
109+
foreach ($q->each(10) as $cmsContent)
110+
{
111+
$permissionName = $cmsContent->adminPermissionName;
112+
$permissions = [
113+
[
114+
[
115+
'name' => $permissionName . "/index",
116+
'description' => $cmsContent->name . " | Список",
117+
'child' => [
118+
'permissions' => [
119+
\skeeks\crm\components\CrmComponent::CRM_PROJECT_VIEW_PERMISSION,
120+
],
121+
],
122+
'ruleName' => \skeeks\crm\rbac\rules\CrmViewProjectRule::class
123+
],
124+
[
125+
'name' => $permissionName . "/create",
126+
'description' => $cmsContent->name . " | Список",
127+
'child' => [
128+
'permissions' => [
129+
\skeeks\crm\components\CrmComponent::CRM_PROJECT_VIEW_PERMISSION,
130+
],
131+
],
132+
'ruleName' => \skeeks\crm\rbac\rules\CrmViewProjectRule::class
133+
],
134+
]
135+
];
136+
}*/
137+
75138
$this->stdout("\tAll config is ready: ", Console::FG_GREEN);
76139
$this->stdout(" (rules: " . count(ArrayHelper::getValue($config, 'rules', [])) . ';');
77140
$this->stdout(" roles: " . count(ArrayHelper::getValue($config, 'roles', [])) . ';');
@@ -162,13 +225,20 @@ protected function _applyRole($config)
162225
if (!$name = ArrayHelper::getValue($config, 'name')) {
163226
return false;
164227
}
228+
165229
$description = ArrayHelper::getValue($config, 'description');
230+
$description = is_array($description) ? \Yii::t($description[0], $description[1]): $description;
231+
166232
if ($role = \Yii::$app->authManager->getRole($name)) {
233+
if ($role->description != $description) {
234+
$role->description = $description;
235+
\Yii::$app->authManager->updateRole($name, $role);
236+
}
167237
return $role;
168238
}
169239
//Менеджер который может управлять только своими данными
170240
$role = \Yii::$app->authManager->createRole($name);
171-
$role->description = is_array($description) ? \Yii::t($description[0], $description[1]): $description;
241+
$role->description = $description;
172242
if (\Yii::$app->authManager->add($role)) {
173243
return $role;
174244
}
@@ -189,14 +259,22 @@ protected function _applyPermission($config)
189259
return false;
190260
}
191261
$description = ArrayHelper::getValue($config, 'description');
262+
$description = is_array($description) ? \Yii::t($description[0], $description[1]): $description;
263+
192264
$ruleName = ArrayHelper::getValue($config, 'ruleName', '');
193265
if ($role = \Yii::$app->authManager->getPermission($name)) {
266+
267+
if ($role->description != $description) {
268+
$role->description = $description;
269+
\Yii::$app->authManager->updatePermission($name, $role);
270+
}
271+
194272
return $role;
195273
}
196274
//Менеджер который может управлять только своими данными
197275
$role = \Yii::$app->authManager->createPermission($name);
198276
if ($description) {
199-
$role->description = is_array($description) ? \Yii::t($description[0], $description[1]): $description;
277+
$role->description = $description;
200278
}
201279
if ($ruleName) {
202280
$role->ruleName = $ruleName;
@@ -329,9 +407,14 @@ protected function _assignPermission($config)
329407
return $permission;
330408
}
331409

332-
public function initBackendData()
410+
/**
411+
* Сбор и настройка нужных привелегий из контроллеров
412+
* @return void
413+
* @throws \yii\base\InvalidConfigException
414+
*/
415+
public function actionInitBackendData()
333416
{
334-
$this->stdout("\t3)Init backend data\n", Console::FG_YELLOW);
417+
$this->stdout("Сбор и настройка нужных привелегий из контроллеров\n", Console::FG_YELLOW);
335418

336419
$auth = Yii::$app->authManager;
337420
//print_r(\Yii::getAlias('@vendor/skeeks/cms/app-web-create.php'));die;
@@ -347,31 +430,65 @@ public function initBackendData()
347430
$webApplication = include_once \Yii::getAlias('@vendor/skeeks/cms/app-web-create.php');
348431
var_dump($webApplication);die;*/
349432

350-
foreach (Yii::$app->getComponents(true) as $id => $component) {
351-
$component = \Yii::$app->get($id);
433+
$webApplication = $this->_getWebApplication();
434+
435+
foreach ($webApplication->getComponents(true) as $id => $component) {
436+
$component = $webApplication->get($id);
352437

353438
if ($component instanceof IBackendComponent) {
354-
$this->stdout("\t\tInit backend {$id}\n");
439+
$this->stdout("Init backend {$id}\n");
440+
355441
foreach ($component->getMenu()->data as $itemData) {
356442
$this->_initMenuItem($itemData);
357443
}
358444

359445
}
360446
}
361447

362-
return $this;
363448
}
364449

450+
protected $_web_application = null;
451+
452+
protected function _getWebApplication() {
453+
if ($this->_web_application === null) {
454+
$config = new \Yiisoft\Config\Config(
455+
new \Yiisoft\Config\ConfigPaths(ROOT_DIR, "config"),
456+
null,
457+
[
458+
\Yiisoft\Config\Modifier\RecursiveMerge::groups('web', 'web-prod', 'params', "params-web-prod"),
459+
],
460+
"params-web-prod"
461+
);
462+
463+
if ($config->has('web-prod')) {
464+
$configData = $config->get('web-prod');
465+
} else {
466+
$configData = $config->get('web');
467+
}
468+
469+
ArrayHelper::remove($configData, "components.log.targets");
470+
ArrayHelper::remove($configData, "bootstrap");
471+
$this->_web_application = new \yii\web\Application($configData);
472+
}
473+
474+
return $this->_web_application;
475+
}
476+
365477
protected function _initMenuItem($itemData = null)
366478
{
367479
if (!is_array($itemData)) {
368480
return false;
369481
}
370482

483+
484+
$applicationWeb = $this->_getWebApplication();
485+
486+
371487
if ($url = ArrayHelper::getValue($itemData, 'url')) {
372488

373489
if (is_array($url)) {
374490

491+
375492
$url = $url[0];
376493
if (!$url || !is_string($url)) {
377494
return false;
@@ -381,25 +498,68 @@ protected function _initMenuItem($itemData = null)
381498
/**
382499
* @var $controller \yii\web\Controller|IHasPermissions
383500
*/
384-
if ($result = \Yii::$app->createController($url)) {
385-
list($controller, $route) = $result;
386501

387-
$this->stdout("\t\tcreated: {$url}\n", Console::FG_GREEN);
502+
/*if (in_array($url, ['shop/admin-cms-content-element', 'cms/admin-cms-content-element'])) {
503+
return false;
504+
}*/
505+
506+
507+
if ($result = $applicationWeb->createController($url)) {
508+
[$controller, $route] = $result;
509+
510+
388511

389512
if ($controller) {
513+
514+
515+
390516
if ($controller instanceof IHasPermissions) {
517+
391518
$controller->isAllow;
519+
520+
if ($controller->generateAccessActions) {
521+
522+
523+
$this->stdout("\t Controller {$url}\n");
524+
525+
$actions = $controller->actions();
526+
$totalActions = count($actions);
527+
$this->stdout("\t\t Has genereted actions\n");
528+
$this->stdout("\t\t actions: {$totalActions}\n");
529+
foreach ($actions as $key => $action)
530+
{
531+
$action = $controller->createAction($key);
532+
$action->getIsAllow();
533+
}
534+
/*
535+
if (method_exists($controller, 'getModelActions')) {
536+
$this->stdout("\t\t Has getModelActions\n");
537+
if ($controller->modelActions) {
538+
foreach ($controller->modelActions as $key => $action)
539+
{
540+
$rf = new \ReflectionClass($action);
541+
$this->stdout("\t\t\tAction: {$key}\n", Console::FG_GREEN);
542+
$this->stdout("\t\t\tAction: {$rf->getName()}\n", Console::FG_GREEN);
543+
$action->getIsAllow();
544+
}
545+
}
546+
547+
}*/
548+
549+
}
392550
}
393551
}
394552
} else {
395553
//$this->stdout("\t\tnot create: {$url}\n", Console::FG_RED);
396554
}
397555
} catch (\Exception $e) {
398-
//$this->stdout("\t\t{$e->getMessage()}\n", Console::FG_RED);
556+
$this->stdout("\t\t{$e->getMessage()}\n", Console::FG_RED);
557+
//die;
399558
}
400559

401560
}
402561
}
562+
403563

404564
if ($items = ArrayHelper::getValue($itemData, 'items')) {
405565
if (is_array($items)) {
@@ -412,9 +572,13 @@ protected function _initMenuItem($itemData = null)
412572
return $this;
413573
}
414574

415-
public function initRootAssigning()
575+
/**
576+
* Настройка роли суперадминистратор
577+
* @return void
578+
*/
579+
public function actionInitRootAssigning()
416580
{
417-
$this->stdout("\t5) Init root assigning \n", Console::FG_YELLOW);
581+
$this->stdout("Настройка роли суперадминистратор \n", Console::FG_YELLOW);
418582
$roleRoot = \Yii::$app->authManager->getRole(CmsManager::ROLE_ROOT);
419583
foreach (\Yii::$app->authManager->getPermissions() as $permission) {
420584
//$this->stdout("\t\tassign root permisssion: " . $permission->name);
@@ -438,12 +602,12 @@ public function initRootAssigning()
438602
}
439603

440604
/**
441-
* Автоматическая генерация
605+
* Насктройка пользователя - суперадминистратор
442606
* @return $this
443607
*/
444-
protected function initRootUser()
608+
protected function actionInitRootUser()
445609
{
446-
$this->stdout("\t6) Init root user \n", Console::FG_YELLOW);
610+
$this->stdout("Насктройка пользователя - суперадминистратор \n", Console::FG_YELLOW);
447611
$root = User::findByUsername('root');
448612
$aManager = \Yii::$app->authManager;
449613
if ($root && $aManager->getRole(CmsManager::ROLE_ROOT)) {
@@ -460,5 +624,7 @@ protected function initRootUser()
460624
public function actionViewConfig()
461625
{
462626
$this->loadConfig();
627+
$config = \Yii::$app->authManager->config;
628+
print_r($config);
463629
}
464630
}

src/controllers/AdminPermissionController.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,7 @@ public function init()
4646
$this->modelClassName = CmsAuthItem::class;
4747

4848
$this->generateAccessActions = false;
49-
//$this->permissionName = CmsManager::PERMISSION_ROOT_ACCESS;
50-
$this->accessCallback = function () {
51-
if (!\Yii::$app->skeeks->site->is_default) {
52-
return false;
53-
}
54-
return \Yii::$app->user->can(CmsManager::PERMISSION_ROOT_ACCESS);
55-
};
49+
$this->permissionName = CmsManager::PERMISSION_ROOT_ACCESS;
5650

5751
parent::init();
5852
}
@@ -205,7 +199,7 @@ public function actionUpdateData()
205199

206200
$cmd = "php yii rbac/init";
207201

208-
$process = new \Symfony\Component\Process\Process($cmd, \Yii::getAlias('@root'));
202+
$process = new \Symfony\Component\Process\Process([$cmd], \Yii::getAlias('@root'));
209203
$process->run();
210204

211205
/*foreach (\Yii::$app->getComponents(true) as $id => $component) {

0 commit comments

Comments
 (0)