forked from sjaakp/yii2-pluto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModule.php
326 lines (292 loc) · 10.2 KB
/
Module.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
* yii2-pluto
* ----------
* User management module for Yii2 framework
* Version 1.0.0
* Copyright (c) 2019
* Sjaak Priester, Amsterdam
* MIT License
* https://github.com/sjaakp/yii2-pluto
* https://sjaakpriester.nl
*/
namespace sjaakp\pluto;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Module as YiiModule;
use yii\base\BootstrapInterface;
use yii\console\Application as ConsoleApplication;
use yii\web\Application as WebApplication;
use yii\web\GroupUrlRule;
use yii\web\UserEvent;
use yii\helpers\ArrayHelper;
use sjaakp\pluto\models\User;
/**
* user module definition class
*/
class Module extends YiiModule implements BootstrapInterface
{
/**
* @var array options for certain aspects of views
*/
public $viewOptions = [
'row' => [ 'class' => 'row justify-content-center' ],
'col' => [ 'class' => 'col-md-6 col-lg-5' ],
'button' => [ 'class' => 'btn btn-success' ],
'link' => [ 'class' => 'btn btn-sm btn-secondary' ],
];
/**
* @var array locations of view override files
*/
public $views = [
// 'default' => [
// 'settings' => '@app/views/pluto/default/settings.php'
// ]
];
/**
* @var array options for app mailer
* @link https://www.yiiframework.com/doc/api/2.0/yii-mail-basemailer
* If you want to override the mailer views, set viewPath.
*/
public $mailOptions = [
'viewPath' => '@sjaakp/pluto/mail',
'htmlLayout' => '@sjaakp/pluto/mail/layouts/html',
'textLayout' => '@sjaakp/pluto/mail/layouts/text',
];
/**
* @var array
* key: one of the actions ('signup', 'login', 'forgot', 'recover', 'resend', 'pw_change', 'delete'
* key may also be 'all', in which case the value applies to all actions
* value: string|array of the flags 'reveal', 'double', 'captcha'
*/
public $passwordFlags = [
'all' => 'reveal',
// 'delete' => ['double', 'captcha']
];
/**
* Pattern that will be applied for password.
*
* example:
* '^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$'
*
* ^: anchored to beginning of string
* \S*: any set of characters
* (?=\S{8,}): of at least length 8
* (?=\S*[a-z]): containing at least one lowercase letter
* (?=\S*[A-Z]): and at least one uppercase letter
* (?=\S*[\d]): and at least one number
* $: anchored to the end of the string
*
* @var string
*/
public $passwordRegexp = '/^\S*(?=\S{6,})\S*$/'; // at least 6 characters
/**
* @var string Text hint displayed with password inputs; description of $passwordRegexp
*/
public $passwordHint = 'At least 6 characters';
/**
* @var null|string|array name(s) of Role(s) assigned to new User
*/
public $defaultRole;
/**
* @var null|string|array name(s) of Role(s) assigned to first new User
*/
public $firstDefaultRole = 'admin';
/**
* @var string
*/
public $ruleNamespace = 'app\rbac';
/**
* @var int seconds that a token remains valid. Default is six hours.
*/
public $tokenStamina = 21600;
/**
* @var int seconds that a cookie-based login remains valid. Default is thirty days.
*/
public $loginStamina = 2592000;
/**
* @var bool whether a user can have multiple roles
*/
public $multipleRoles = false;
/**
* @var string form class used in dialogs; if null, is set to bootstrap ActiveForm
*/
public $formClass;
/**
* @var bool|string
* if true, puts the whole site behind a 'fence': only authenticated users can enter
* if string: Permission name, only users with Permission can enter
*/
public $fenceMode = false;
/**
* @var string yii\db\BaseActiveRecord class name; used as profile
*/
public $profileClass;
/**
* @var string the class name of the identity object associated with the current user
* May be changed into a class extended from sjaakp\pluto\models\User
*/
public $identityClass = 'sjaakp\pluto\models\User';
/**
* @throws InvalidConfigException
*/
public function init()
{
if (! Yii::$app->has('authManager')) {
throw new InvalidConfigException('$app::authManager is not configured.');
}
parent::init();
if (! isset( Yii::$app->i18n->translations['pluto'])) {
Yii::$app->i18n->translations['pluto'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '@sjaakp/pluto/messages',
];
}
}
/**
* @param $action
* @return int
*/
public function getPwFlags($action)
{
$actionFlags = $this->passwordFlags[$action] ?? [];
if (is_string($actionFlags)) $actionFlags = empty($actionFlags) ? [] : [$actionFlags];
$allFlags = $this->passwordFlags['all'] ?? [];
if (is_string($allFlags)) $allFlags = empty($allFlags) ? [] : [$allFlags];
$flags = array_merge($actionFlags, $allFlags);
$captcha = array_search('captcha', $flags);
if ($captcha !== false && (Yii::$app->reCaptcha ?? false)) {
unset($flags[$captcha]);
$flags[] = 'reCaptcha';
}
return $flags;
}
/**
* @param $event UserEvent
*/
public static function beforeLogin($event)
{
/* @var $user User */
$user = $event->identity;
if ($user->status != User::STATUS_ACTIVE) $event->isValid = false; // holds blocked user
else {
$user->touch('lastlogin_at');
$user->updateCounters(['login_count' => 1]);
}
}
/**
* @param $rule
* @param $action
* @return yii\web\Response
* @throws yii\web\ForbiddenHttpException
*/
public static function accessDenied($rule, $action)
{
return self::denyAccess($action->controller);
}
/**
* @param $controller yii\web\Controller
* @param null $message string
* @param array $messageKey string
* @return yii\web\Response
* @throws yii\web\ForbiddenHttpException
*/
public static function denyAccess($controller, $message = null, $messageKey = 'danger')
{
$user = Yii::$app->user;
if ($user !== false && $user->isGuest) {
return $user->loginRequired();
} else {
/* @var $identity User */
$identity = $user->identity;
if (! $message) {
$elmnts = $controller->module === Yii::$app ? [] : [ $controller->module->id ];
$elmnts[] = $controller->id;
$elmnts[] = $controller->action->id;
$message = Yii::t('pluto','Sorry {username}, you\'re not allowed to visit <strong>{route}</strong> on this site.', [
'username' => $identity->name ?? '',
'route' => implode('/', $elmnts)
]);
}
Yii::$app->session->setFlash($messageKey, $message);
$r = $controller->goBack();
Yii::$app->user->setReturnUrl(null);
return $r;
}
}
/**
* {@inheritdoc}
*
* @throws InvalidConfigException
*/
public function bootstrap($app)
{
if ($app instanceof WebApplication) {
$rules = new GroupUrlRule([
'prefix' => $this->id,
'rules' => [
'<a:(confirm|recover)>/<token:[A-Za-z0-9_-]+>' => 'default/<a>',
'<a:[\w\-]+>/<id:\d+>' => 'default/<a>',
'<c:[\w\-]+>/<a:[\w\-]+>/<id:[\w\-]+>' => '<c>/<a>',
'<c:(user|role|permission)>' => '<c>/index',
'<a:[\w\-]+>' => 'default/<a>',
]
]);
$app->getUrlManager()->addRules([$rules], false);
$app->setComponents([
'user' => ArrayHelper::merge($app->components['user'], [
'identityClass' => $this->identityClass,
'loginUrl' => [$this->id . '/default/login'],
'on beforeLogin' => [$this, 'beforeLogin']
]),
]);
if ($this->fenceMode !== false) {
$app->on(WebApplication::EVENT_BEFORE_ACTION, function($event) {
$forbidden = false;
$user = Yii::$app->user;
if ($user->isGuest) {
$forbidden = true;
}
else if (is_string($this->fenceMode) && ! $user->can($this->fenceMode)) {
$user->logout();
$forbidden = true;
}
if ($forbidden) {
$action = $event->action->id;
if ($event->action->controller->module->id != $this->id ||
! in_array($action, ['login', 'error', 'forgot', 'recover'])) {
$event->isValid = false;
return $user->loginRequired();
}
}
return null;
});
}
if (empty($this->formClass)) $this->formClass = $this->bootstrapNamespace() . '\ActiveForm';
} else {
/* @var $app ConsoleApplication */
$app->controllerMap = ArrayHelper::merge($app->controllerMap, [
'migrate' => [
'class' => '\yii\console\controllers\MigrateController',
'migrationNamespaces' => [
'sjaakp\pluto\migrations'
]
],
'pluto' => 'sjaakp\pluto\commands\PlutoController'
]);
}
}
/**
* @return string the namespace of the Bootstrap extension ('yii\bootstrap' or 'yii\bootstrap4')
* @throws InvalidConfigException
*/
public function bootstrapNamespace()
{
foreach ([ '4', '3', ''] as $v) {
$ns = 'yii/bootstrap' . $v;
if (strrpos(Yii::getAlias( '@' . $ns, false),'/src') !== false) return str_replace('/', '\\', $ns);
}
throw new InvalidConfigException( 'No Bootstrap extension found');
}
}