1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the 2amigos/yii2-usuario project. |
5
|
|
|
* |
6
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Da\User; |
13
|
|
|
|
14
|
|
|
use Da\User\Component\AuthDbManagerComponent; |
15
|
|
|
use Da\User\Contracts\AuthManagerInterface; |
16
|
|
|
use Da\User\Helper\ClassMapHelper; |
17
|
|
|
use Da\User\Model\User; |
18
|
|
|
use Da\User\Validator\TimeZoneValidator; |
19
|
|
|
use Yii; |
20
|
|
|
use yii\authclient\Collection; |
21
|
|
|
use yii\base\Application; |
22
|
|
|
use yii\base\BootstrapInterface; |
23
|
|
|
use yii\base\Exception; |
24
|
|
|
use yii\console\Application as ConsoleApplication; |
25
|
|
|
use yii\i18n\PhpMessageSource; |
26
|
|
|
use yii\web\Application as WebApplication; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Bootstrap class of the yii2-usuario extension. Configures container services, initializes translations, |
30
|
|
|
* builds class map, and does the other setup actions participating in the application bootstrap process. |
31
|
|
|
*/ |
32
|
|
|
class Bootstrap implements BootstrapInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
|
|
public function bootstrap($app) |
38
|
|
|
{ |
39
|
|
|
if ($app->hasModule('user') && $app->getModule('user') instanceof Module) { |
40
|
|
|
$map = $this->buildClassMap($app->getModule('user')->classMap); |
41
|
|
|
$this->initContainer($app, $map); |
42
|
|
|
$this->initTranslations($app); |
43
|
|
|
$this->initMailServiceConfiguration($app, $app->getModule('user')); |
44
|
|
|
|
45
|
|
|
if ($app instanceof WebApplication) { |
46
|
|
|
$this->initControllerNamespace($app); |
47
|
|
|
$this->initUrlRoutes($app); |
48
|
|
|
$this->initAuthCollection($app); |
49
|
|
|
$this->initAuthManager($app); |
50
|
|
|
} else { |
51
|
|
|
/* @var $app ConsoleApplication */ |
52
|
|
|
$this->initConsoleCommands($app); |
53
|
|
|
$this->initAuthManager($app); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Initialize container with module classes. |
60
|
|
|
* |
61
|
|
|
* @param \yii\base\Application $app |
62
|
|
|
* @param array $map the previously built class map list |
63
|
|
|
*/ |
64
|
10 |
|
protected function initContainer($app, $map) |
65
|
|
|
{ |
66
|
|
|
$di = Yii::$container; |
67
|
|
|
try { |
68
|
|
|
// events |
69
|
|
|
$di->set(Event\FormEvent::class); |
70
|
|
|
$di->set(Event\ProfileEvent::class); |
71
|
|
|
$di->set(Event\ResetPasswordEvent::class); |
72
|
|
|
$di->set(Event\SocialNetworkAuthEvent::class); |
73
|
|
|
$di->set(Event\SocialNetworkConnectEvent::class); |
74
|
|
|
$di->set(Event\UserEvent::class); |
75
|
|
|
|
76
|
|
|
// forms |
77
|
|
|
$di->set(Form\LoginForm::class); |
78
|
|
|
$di->set(Form\RecoveryForm::class); |
79
|
|
|
$di->set(Form\RegistrationForm::class); |
80
|
|
|
$di->set(Form\ResendForm::class); |
81
|
|
|
$di->set(Form\SettingsForm::class); |
82
|
|
|
|
83
|
|
|
// helpers |
84
|
|
|
$di->set(Helper\AuthHelper::class); |
85
|
|
|
$di->set(Helper\GravatarHelper::class); |
86
|
|
|
$di->set(Helper\SecurityHelper::class); |
87
|
|
|
$di->set(Helper\TimezoneHelper::class); |
88
|
|
|
|
89
|
|
|
// services |
90
|
|
|
$di->set(Service\AccountConfirmationService::class); |
91
|
|
|
$di->set(Service\EmailChangeService::class); |
92
|
|
|
$di->set(Service\PasswordRecoveryService::class); |
93
|
|
|
$di->set(Service\ResendConfirmationService::class); |
94
|
|
|
$di->set(Service\ResetPasswordService::class); |
95
|
|
|
$di->set(Service\SocialNetworkAccountConnectService::class); |
96
|
|
|
$di->set(Service\SocialNetworkAuthenticateService::class); |
97
|
|
|
$di->set(Service\UserBlockService::class); |
98
|
|
|
$di->set(Service\UserCreateService::class); |
99
|
|
|
$di->set(Service\UserRegisterService::class); |
100
|
|
|
$di->set(Service\UserConfirmationService::class); |
101
|
|
|
$di->set(Service\AuthItemEditionService::class); |
102
|
|
|
$di->set(Service\UpdateAuthAssignmentsService::class); |
103
|
|
|
$di->set(Service\SwitchIdentityService::class); |
104
|
|
|
|
105
|
|
|
// email change strategy |
106
|
|
|
$di->set(Strategy\DefaultEmailChangeStrategy::class); |
107
|
|
|
$di->set(Strategy\InsecureEmailChangeStrategy::class); |
108
|
|
|
$di->set(Strategy\SecureEmailChangeStrategy::class); |
109
|
|
|
|
110
|
|
|
// validators |
111
|
|
|
$di->set(Validator\AjaxRequestModelValidator::class); |
112
|
|
|
$di->set(TimeZoneValidator::class); |
113
|
|
|
|
114
|
|
|
// class map models + query classes |
115
|
|
|
$modelClassMap = []; |
116
|
|
|
foreach ($map as $class => $definition) { |
117
|
|
|
$di->set($class, $definition); |
118
|
|
|
$model = is_array($definition) ? $definition['class'] : $definition; |
119
|
|
|
$name = (substr($class, strrpos($class, '\\') + 1)); |
120
|
|
|
$modelClassMap[$class] = $model; |
121
|
|
|
if (in_array($name, ['User', 'Profile', 'Token', 'SocialNetworkAccount'])) { |
122
|
|
|
$di->set( |
123
|
|
|
"Da\\User\\Query\\{$name}Query", |
124
|
10 |
|
function () use ($model) { |
125
|
10 |
|
return $model::find(); |
126
|
|
|
} |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
$di->setSingleton(ClassMapHelper::class, ClassMapHelper::class, [$modelClassMap]); |
131
|
|
|
|
132
|
|
|
// search classes |
133
|
|
|
if (!$di->has(Search\UserSearch::class)) { |
134
|
|
|
$di->set(Search\UserSearch::class, [$di->get(Query\UserQuery::class)]); |
135
|
|
|
} |
136
|
|
|
if (!$di->has(Search\PermissionSearch::class)) { |
137
|
|
|
$di->set(Search\PermissionSearch::class); |
138
|
|
|
} |
139
|
|
|
if (!$di->has(Search\RoleSearch::class)) { |
140
|
|
|
$di->set(Search\RoleSearch::class); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($app instanceof WebApplication) { |
144
|
|
|
// override Yii |
145
|
|
|
$di->set( |
146
|
|
|
'yii\web\User', |
147
|
|
|
[ |
148
|
|
|
'enableAutoLogin' => true, |
149
|
|
|
'loginUrl' => ['/user/security/login'], |
150
|
|
|
'identityClass' => $di->get(ClassMapHelper::class)->get(User::class), |
151
|
|
|
] |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
} catch (Exception $e) { |
155
|
|
|
die($e); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Registers module translation messages. |
161
|
|
|
* |
162
|
|
|
* @param Application $app |
163
|
|
|
*/ |
164
|
|
|
protected function initTranslations(Application $app) |
165
|
|
|
{ |
166
|
|
|
if (!isset($app->get('i18n')->translations['usuario*'])) { |
167
|
|
|
$app->get('i18n')->translations['usuario*'] = [ |
168
|
|
|
'class' => PhpMessageSource::class, |
169
|
|
|
'basePath' => __DIR__ . '/resources/i18n', |
170
|
|
|
'sourceLanguage' => 'en-US', |
171
|
|
|
]; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Ensures the auth manager is the one provided by the library. |
177
|
|
|
* |
178
|
|
|
* @param Application $app |
179
|
|
|
*/ |
180
|
|
|
protected function initAuthManager(Application $app) |
181
|
|
|
{ |
182
|
|
|
if (!($app->getAuthManager() instanceof AuthManagerInterface)) { |
183
|
|
|
$app->set( |
184
|
|
|
'authManager', |
185
|
|
|
[ |
186
|
|
|
'class' => AuthDbManagerComponent::class, |
187
|
|
|
] |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Initializes web url routes (rules in Yii2). |
194
|
|
|
* |
195
|
|
|
* @param WebApplication $app |
196
|
|
|
*/ |
197
|
|
|
protected function initUrlRoutes(WebApplication $app) |
198
|
|
|
{ |
199
|
|
|
/** @var $module Module */ |
200
|
|
|
$module = $app->getModule('user'); |
201
|
|
|
$config = [ |
202
|
|
|
'class' => 'yii\web\GroupUrlRule', |
203
|
|
|
'prefix' => $module->prefix, |
204
|
|
|
'rules' => $module->routes, |
205
|
|
|
]; |
206
|
|
|
|
207
|
|
|
if ($module->prefix !== 'user') { |
208
|
|
|
$config['routePrefix'] = 'user'; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$rule = Yii::createObject($config); |
212
|
|
|
$app->getUrlManager()->addRules([$rule], false); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Ensures required mail parameters needed for the mail service. |
217
|
|
|
* |
218
|
|
|
* @param Application $app |
219
|
|
|
* @param Module|\yii\base\Module $module |
220
|
|
|
*/ |
221
|
|
|
protected function initMailServiceConfiguration(Application $app, Module $module) |
222
|
|
|
{ |
223
|
|
|
$defaults = [ |
224
|
|
|
'fromEmail' => '[email protected]', |
225
|
|
|
'welcomeMailSubject' => Yii::t('usuario', 'Welcome to {0}', $app->name), |
|
|
|
|
226
|
|
|
'confirmationMailSubject' => Yii::t('usuario', 'Confirm account on {0}', $app->name), |
|
|
|
|
227
|
|
|
'reconfirmationMailSubject' => Yii::t('usuario', 'Confirm email change on {0}', $app->name), |
|
|
|
|
228
|
|
|
'recoveryMailSubject' => Yii::t('usuario', 'Complete password reset on {0}', $app->name), |
|
|
|
|
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
$module->mailParams = array_merge($defaults, $module->mailParams); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Ensures the authCollection component is configured. |
236
|
|
|
* |
237
|
|
|
* @param WebApplication $app |
238
|
|
|
*/ |
239
|
|
|
protected function initAuthCollection(WebApplication $app) |
240
|
|
|
{ |
241
|
|
|
if (!$app->has('authClientCollection')) { |
242
|
|
|
$app->set('authClientCollection', Collection::class); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Registers console commands to main app. |
248
|
|
|
* |
249
|
|
|
* @param ConsoleApplication $app |
250
|
|
|
*/ |
251
|
|
|
protected function initConsoleCommands(ConsoleApplication $app) |
252
|
|
|
{ |
253
|
|
|
$app->getModule('user')->controllerNamespace = 'Da\User\Command'; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Registers controllers. |
258
|
|
|
* |
259
|
|
|
* @param WebApplication $app |
260
|
|
|
*/ |
261
|
|
|
protected function initControllerNamespace(WebApplication $app) |
262
|
|
|
{ |
263
|
|
|
$app->getModule('user')->controllerNamespace = 'Da\User\Controller'; |
264
|
|
|
$app->getModule('user')->setViewPath('@Da/User/resources/views'); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* Builds class map according to user configuration. |
269
|
|
|
* |
270
|
|
|
* @param array $userClassMap user configuration on the module |
271
|
|
|
* |
272
|
|
|
* @return array |
273
|
|
|
*/ |
274
|
|
|
protected function buildClassMap(array $userClassMap) |
275
|
|
|
{ |
276
|
|
|
$map = []; |
277
|
|
|
|
278
|
|
|
$defaults = [ |
279
|
|
|
// --- models |
280
|
|
|
'User' => 'Da\User\Model\User', |
281
|
|
|
'SocialNetworkAccount' => 'Da\User\Model\SocialNetworkAccount', |
282
|
|
|
'Profile' => 'Da\User\Model\Profile', |
283
|
|
|
'Token' => 'Da\User\Model\Token', |
284
|
|
|
'Assignment' => 'Da\User\Model\Assignment', |
285
|
|
|
'Permission' => 'Da\User\Model\Permission', |
286
|
|
|
'Role' => 'Da\User\Model\Role', |
287
|
|
|
// --- search |
288
|
|
|
'UserSearch' => 'Da\User\Search\UserSearch', |
289
|
|
|
'PermissionSearch' => 'Da\User\Search\PermissionSearch', |
290
|
|
|
'RoleSearch' => 'Da\User\Search\RoleSearch', |
291
|
|
|
// --- forms |
292
|
|
|
'RegistrationForm' => 'Da\User\Form\RegistrationForm', |
293
|
|
|
'ResendForm' => 'Da\User\Form\ResendForm', |
294
|
|
|
'LoginForm' => 'Da\User\Form\LoginForm', |
295
|
|
|
'SettingsForm' => 'Da\User\Form\SettingsForm', |
296
|
|
|
'RecoveryForm' => 'Da\User\Form\RecoveryForm', |
297
|
|
|
]; |
298
|
|
|
|
299
|
|
|
$routes = [ |
300
|
|
|
'Da\User\Model' => [ |
301
|
|
|
'User', |
302
|
|
|
'SocialNetworkAccount', |
303
|
|
|
'Profile', |
304
|
|
|
'Token', |
305
|
|
|
'Assignment', |
306
|
|
|
'Permission', |
307
|
|
|
'Role', |
308
|
|
|
], |
309
|
|
|
'Da\User\Search' => [ |
310
|
|
|
'UserSearch', |
311
|
|
|
'PermissionSearch', |
312
|
|
|
'RoleSearch', |
313
|
|
|
], |
314
|
|
|
'Da\User\Form' => [ |
315
|
|
|
'RegistrationForm', |
316
|
|
|
'ResendForm', |
317
|
|
|
'LoginForm', |
318
|
|
|
'SettingsForm', |
319
|
|
|
'RecoveryForm', |
320
|
|
|
], |
321
|
|
|
]; |
322
|
|
|
|
323
|
|
|
$mapping = array_merge($defaults, $userClassMap); |
324
|
|
|
|
325
|
|
|
foreach ($mapping as $name => $definition) { |
326
|
|
|
$map[$this->getRoute($routes, $name) . "\\$name"] = $definition; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $map; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Returns the parent class name route of a short class name. |
334
|
|
|
* |
335
|
|
|
* @param array $routes class name routes |
336
|
|
|
* @param string $name |
337
|
|
|
* |
338
|
|
|
* @throws Exception |
339
|
|
|
* @return int|string |
340
|
|
|
* |
341
|
|
|
*/ |
342
|
|
|
protected function getRoute(array $routes, $name) |
343
|
|
|
{ |
344
|
|
|
foreach ($routes as $route => $names) { |
345
|
|
|
if (in_array($name, $names)) { |
346
|
|
|
return $route; |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
throw new Exception("Unknown configuration class name '{$name}'"); |
350
|
|
|
} |
351
|
|
|
} |
352
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: