forked from dektrium/yii2-rbac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bootstrap.php
81 lines (72 loc) · 2.38 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
<?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\base\BootstrapInterface;
/**
* Bootstrap class registers translations and needed application components.
* @author Dmitry Erofeev <[email protected]>
*/
class Bootstrap implements BootstrapInterface
{
/** @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',
];
}
if ($this->checkRbacModuleInstalled($app)) {
// register auth manager
if (!$this->checkAuthManagerConfigured($app)) {
$app->set('authManager', [
'class' => DbManager::className(),
]);
}
// if dektrium/user extension is installed, copy admin list from there
if ($this->checkUserModuleInstalled($app)) {
$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)
{
return $app->hasModule('rbac') && $app->getModule('rbac') instanceof Module;
}
/**
* 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;
}
}