itstructure /
yii2-rbac-module
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Itstructure\RbacModule; |
||
| 4 | |||
| 5 | use Yii; |
||
| 6 | use yii\web\View; |
||
| 7 | use yii\helpers\ArrayHelper; |
||
| 8 | use yii\rbac\ManagerInterface; |
||
| 9 | use yii\base\{Module as BaseModule, InvalidConfigException}; |
||
| 10 | use Itstructure\RbacModule\components\{RbacValidateComponent, ProfileValidateComponent}; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Rbac module class. |
||
| 14 | * |
||
| 15 | * @property null|string|array $loginUrl |
||
| 16 | * @property array $accessRoles |
||
| 17 | * @property View $_view |
||
| 18 | * @property ManagerInterface $_authManager |
||
| 19 | * |
||
| 20 | * @package Itstructure\RbacModule |
||
| 21 | */ |
||
| 22 | class Module extends BaseModule |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Login url. |
||
| 26 | * |
||
| 27 | * @var null|string|array |
||
| 28 | */ |
||
| 29 | public $loginUrl = null; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Array of roles to module access. |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | public $accessRoles = ['@']; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * View component to render content. |
||
| 40 | * |
||
| 41 | * @var View |
||
| 42 | */ |
||
| 43 | private $_view = null; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Auth manager. |
||
| 47 | * |
||
| 48 | * @var ManagerInterface |
||
| 49 | */ |
||
| 50 | private $_authManager; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Module translations. |
||
| 54 | * |
||
| 55 | * @var array|null |
||
| 56 | */ |
||
| 57 | private static $_translations = null; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @inheritdoc |
||
| 61 | */ |
||
| 62 | public function init() |
||
| 63 | { |
||
| 64 | parent::init(); |
||
| 65 | |||
| 66 | $this->_authManager = Yii::$app->authManager; |
||
| 67 | |||
| 68 | if (null === $this->_authManager) { |
||
| 69 | throw new InvalidConfigException('The authManager is not defined.'); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (!$this->_authManager instanceof ManagerInterface) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 73 | throw new InvalidConfigException('The authManager must be implemented from yii\rbac\ManagerInterface.'); |
||
| 74 | } |
||
| 75 | |||
| 76 | Yii::setAlias('@rbac', static::getBaseDir()); |
||
| 77 | |||
| 78 | if (null !== $this->loginUrl && method_exists(Yii::$app, 'getUser')) { |
||
| 79 | Yii::$app->getUser()->loginUrl = $this->loginUrl; |
||
| 80 | } |
||
| 81 | |||
| 82 | self::registerTranslations(); |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Set Rbac validate component |
||
| 86 | */ |
||
| 87 | $this->setComponents( |
||
| 88 | ArrayHelper::merge( |
||
| 89 | $this->getRbacValidateComponentConfig(), |
||
| 90 | $this->components |
||
| 91 | ) |
||
| 92 | ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Set Profile validate component |
||
| 96 | */ |
||
| 97 | $this->setComponents( |
||
| 98 | ArrayHelper::merge( |
||
| 99 | $this->getProfileValidateComponentConfig(), |
||
| 100 | $this->components |
||
| 101 | ) |
||
| 102 | ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get the view. |
||
| 107 | * |
||
| 108 | * @return View |
||
| 109 | */ |
||
| 110 | public function getView() |
||
| 111 | { |
||
| 112 | if (null === $this->_view) { |
||
| 113 | $this->_view = $this->get('view'); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $this->_view; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns module root directory. |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public static function getBaseDir(): string |
||
| 125 | { |
||
| 126 | return __DIR__; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Module translator. |
||
| 131 | * |
||
| 132 | * @param $category |
||
| 133 | * @param $message |
||
| 134 | * @param array $params |
||
| 135 | * @param null $language |
||
|
0 ignored issues
–
show
|
|||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public static function t($category, $message, $params = [], $language = null) |
||
| 140 | { |
||
| 141 | if (null === self::$_translations) { |
||
| 142 | self::registerTranslations(); |
||
| 143 | } |
||
| 144 | |||
| 145 | return Yii::t('modules/rbac/' . $category, $message, $params, $language); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Set i18N component. |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | private static function registerTranslations(): void |
||
| 154 | { |
||
| 155 | self::$_translations = [ |
||
| 156 | 'modules/rbac/*' => [ |
||
| 157 | 'class' => 'yii\i18n\PhpMessageSource', |
||
| 158 | 'forceTranslation' => true, |
||
| 159 | 'sourceLanguage' => Yii::$app->language, |
||
| 160 | 'basePath' => '@rbac/messages', |
||
| 161 | 'fileMap' => [ |
||
| 162 | 'modules/rbac/main' => 'main.php', |
||
| 163 | 'modules/rbac/roles' => 'roles.php', |
||
| 164 | 'modules/rbac/permissions' => 'permissions.php', |
||
| 165 | 'modules/rbac/profiles' => 'profiles.php', |
||
| 166 | 'modules/rbac/rbac' => 'rbac.php', |
||
| 167 | ], |
||
| 168 | ] |
||
| 169 | ]; |
||
| 170 | |||
| 171 | Yii::$app->i18n->translations = ArrayHelper::merge( |
||
| 172 | self::$_translations, |
||
| 173 | Yii::$app->i18n->translations |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Rbac validate component config. |
||
| 179 | * |
||
| 180 | * @return array |
||
| 181 | */ |
||
| 182 | private function getRbacValidateComponentConfig(): array |
||
| 183 | { |
||
| 184 | return [ |
||
| 185 | 'rbac-validate-component' => [ |
||
| 186 | 'class' => RbacValidateComponent::class, |
||
| 187 | 'authManager' => $this->_authManager, |
||
| 188 | ] |
||
| 189 | ]; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Profile validate component config. |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | private function getProfileValidateComponentConfig(): array |
||
| 198 | { |
||
| 199 | return [ |
||
| 200 | 'profile-validate-component' => [ |
||
| 201 | 'class' => ProfileValidateComponent::class, |
||
| 202 | 'authManager' => $this->_authManager, |
||
| 203 | ] |
||
| 204 | ]; |
||
| 205 | } |
||
| 206 | } |
||
| 207 |