forked from dektrium/yii2-rbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap.php
93 lines (82 loc) · 2.9 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
<?php
/*
* This file is part of the Dektrium project.
*
* (c) Dektrium project <http://github.com/dektrium/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace dektrium\rbac;
use dektrium\rbac\components\DbManager;
use dektrium\rbac\components\ManagerInterface;
use dektrium\user\Module as UserModule;
use yii\base\Application;
use yii\web\Application as WebApplication;
use yii\base\BootstrapInterface;
use yii\base\InvalidConfigException;
/**
* Bootstrap class registers translations and needed application components.
* @author Dmitry Erofeev <[email protected]>
*/
class Bootstrap implements BootstrapInterface
{
const VERSION = '1.0.0-alpha';
/** @inheritdoc */
public function bootstrap($app)
{
// register translations
if (!isset($app->get('i18n')->translations['rbac*'])) {
$app->get('i18n')->translations['rbac*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => __DIR__ . '/messages',
'sourceLanguage' => 'en-US',
];
}
if ($this->checkRbacModuleInstalled($app)) {
$authManager = $app->get('authManager', false);
if (!$authManager) {
$app->set('authManager', [
'class' => DbManager::className(),
]);
} else if (!($authManager instanceof ManagerInterface)) {
throw new InvalidConfigException('You have wrong authManager configuration');
}
// if dektrium/user extension is installed, copy admin list from there
if ($this->checkUserModuleInstalled($app) && $app instanceof WebApplication) {
$app->getModule('rbac')->admins = $app->getModule('user')->admins;
}
}
}
/**
* Verifies that dektrium/yii2-rbac is installed and configured.
* @param Application $app
* @return bool
*/
protected function checkRbacModuleInstalled(Application $app)
{
if ($app instanceof WebApplication) {
return $app->hasModule('rbac') && $app->getModule('rbac') instanceof RbacWebModule;
} else {
return $app->hasModule('rbac') && $app->getModule('rbac') instanceof RbacConsoleModule;
}
}
/**
* Verifies that dektrium/yii2-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;
}
}