/
Bootstrap.php
128 lines (106 loc) · 3.98 KB
/
Bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* @link http://www.matacms.com/
* @copyright Copyright (c) 2015 Qi Interactive Limited
* @license http://www.matacms.com/license/
*/
namespace matacms\rbac;
use matacms\rbac\components\DbManager;
use matacms\rbac\components\ManagerInterface;
use matacms\rbac\behaviors\RoleAssignmentActiveFormBehavior;
use matacms\rbac\models\Assignment;
use matacms\user\Module as UserModule;
use matacms\widgets\ActiveField;
use matacms\controllers\module\Controller;
use matacms\user\controllers\AdminController as AdminController;
use mata\base\MessageEvent;
use yii\base\Application;
use yii\base\Event;
use yii\base\BootstrapInterface;
/**
* Bootstrap class registers translations and needed application components.
*/
class Bootstrap implements BootstrapInterface
{
/** @inheritdoc */
public function bootstrap($app)
{
// register translations
$app->get('i18n')->translations['rbac*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => __DIR__ . '/messages',
];
if ($this->checkRbacModuleInstalled($app)) {
// register auth manager
if (!$this->checkAuthManagerConfigured($app)) {
$app->set('authManager', [
'class' => DbManager::className(),
]);
}
// if matacms/matacms-user extension is installed, copy admin list from there
if ($this->checkUserModuleInstalled($app)) {
$app->getModule('rbac')->admins = $app->getModule('user')->admins;
}
}
Event::on(ActiveField::className(), ActiveField::EVENT_INIT_DONE, function(MessageEvent $event) {
$event->getMessage()->attachBehavior('roleAssignments', new RoleAssignmentActiveFormBehavior());
});
Event::on(AdminController::class, Controller::EVENT_MODEL_UPDATED, function(\matacms\base\MessageEvent $event) {
$this->processSave($event->getMessage());
});
Event::on(AdminController::class, Controller::EVENT_MODEL_CREATED, function(\matacms\base\MessageEvent $event) {
$this->processSave($event->getMessage());
});
}
/**
* Verifies that matacms/matacms-rbac is installed and configured.
* @param Application $app
* @return bool
*/
protected function checkRbacModuleInstalled(Application $app)
{
return $app->hasModule('rbac') && $app->getModule('rbac') instanceof Module;
}
/**
* Verifies that matacms/matacms-user is installed and configured.
* @param Application $app
* @return bool
*/
protected function checkUserModuleInstalled(Application $app)
{
return $app->hasModule('user') && $app->getModule('user') instanceof UserModule;
}
/**
* Verifies that authManager component is configured.
* @param Application $app
* @return bool
*/
protected function checkAuthManagerConfigured(Application $app)
{
return $app->authManager instanceof ManagerInterface;
}
private function processSave($model) {
$userId = $model->getId();
\Yii::$app->authManager->deleteAllItemsByUser($userId);
if (empty($roles = \Yii::$app->request->post('RoleAssignments')))
return;
if(is_array($roles)) {
foreach ($roles as $role) {
$this->saveRoleAssignment($role, $model, $userId);
}
} elseif(is_string($roles)) {
$this->saveRoleAssignment($roles, $model, $userId);
}
}
private function saveRoleAssignment($roleName, $model, $userId)
{
$auth = \Yii::$app->authManager;
$assignmentItem = $auth->getItemByUser($roleName, $userId);
if ($assignmentItem == null) {
$role = $auth->getRole($roleName);
$assignment = $auth->assign($role, $userId);
if(empty($assignment))
throw new \yii\web\ServerErrorHttpException(\yii\helpers\CVarDumper::dumpAsString($assignment));
}
}
}