forked from webvimark/user-management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserManagementModule.php
291 lines (253 loc) · 8.02 KB
/
UserManagementModule.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
<?php
namespace webvimark\modules\UserManagement;
use Yii;
use yii\helpers\ArrayHelper;
class UserManagementModule extends \yii\base\Module
{
const SESSION_LAST_ATTEMPT = '_um_last_attempt';
const SESSION_ATTEMPT_COUNT = '_um_attempt_count';
/**
* If set true, then on registration username will be validated as email
*
* @var bool
*/
public $useEmailAsLogin = false;
/**
* Works only if $useEmailAsLogin = true
*
* If set true, then on after registration message with activation code will be sent
* to user email and after confirmation user status will be "active"
*
* @var bool
* @see $useEmailAsLogin
*/
public $emailConfirmationRequired = false;
/**
* Params for mailer
* They will be merged with $_defaultMailerOptions
*
* @var array
* @see $_defaultMailerOptions
*/
public $mailerOptions = [];
/**
* Default options for mailer
*
* @var array
*/
protected $_defaultMailerOptions = [
'from'=>'', // If empty it will be - [Yii::$app->params['adminEmail'] => Yii::$app->name . ' robot']
'registrationFormViewFile' => '/mail/registrationEmailConfirmation',
'passwordRecoveryFormViewFile' => '/mail/passwordRecoveryMail',
'confirmEmailFormViewFile' => '/mail/emailConfirmationMail',
];
/**
* Permission that will be assigned automatically for everyone, so you can assign
* routes like "site/index" to this permission and those routes will be available for everyone
*
* Basically it's permission for guests (and of course for everyone else)
*
* @var string
*/
public $commonPermissionName = 'commonPermission';
/**
* You can define your own registration form class here.
* Together with theming registration view file you can ultimately customize registration process.
*
* See AuthController::actionRegistration() and RegistrationForm how they work together
*
* @var string
*/
public $registrationFormClass = 'webvimark\modules\UserManagement\models\forms\RegistrationForm';
/**
* After how many seconds confirmation token will be invalid
*
* @var int
*/
public $confirmationTokenExpire = 3600; // 1 hour
/**
* Registration can be enabled either by this option or by adding '/user-management/auth/registration' route
* to guest permissions
* @var bool
*/
public $enableRegistration = false;
/**
* Roles that will be assigned to user registered via user-management/auth/registration
*
* @var array
*/
public $rolesAfterRegistration = [];
/**
* Pattern that will be applied for names on registration.
* Default pattern allows user enter only numbers and letters.
*
* This will not be used if $useEmailAsLogin set as true !
*
* @var string
*/
public $registrationRegexp = '/^(\w|\d)+$/';
/**
* Pattern that will be applied for names on registration. It contain regexp that should NOT be in username
* Default pattern doesn't allow anything having "admin"
*
* This will not be used if $useEmailAsLogin set as true !
*
* @var string
*/
public $registrationBlackRegexp = '/^(.)*admin(.)*$/i';
/**
* Pattern that will be applied for password.
* Default pattern does not restrict user and can enter any set of characters.
*
* example of pattern :
* '^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$'
*
* This example pattern allow user enter only:
*
* ^: 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 = '/^(.*)+$/';
/**
* Affects only web interface in "/user-management/user-permission/set" route. Tt means you still can assign
* multiple roles (for example via migrations) even if this attribute is "false"
*
* If true there will be checkbox list and user can have multiple roles.
* Otherwise there will be radio list and only 1 role can be assigned to user.
*
* @var bool
*/
public $userCanHaveMultipleRoles = true;
/**
* How much attempts user can made to login or recover password in $attemptsTimeout seconds interval
*
* @var int
*/
public $maxAttempts = 5;
/**
* Number of seconds after attempt counter to login or recover password will reset
*
* @var int
*/
public $attemptsTimeout = 60;
/**
* Options for registration and password recovery captcha
*
* @var array
*/
public $captchaOptions = [
'class' => 'yii\captcha\CaptchaAction',
'minLength' => 3,
'maxLength' => 4,
'offset' => 5
];
/**
* Table aliases
*
* @var string
*/
public $user_table = '{{%user}}';
public $user_visit_log_table = '{{%user_visit_log}}';
public $auth_item_table = '{{%auth_item}}';
public $auth_item_child_table = '{{%auth_item_child}}';
public $auth_item_group_table = '{{%auth_item_group}}';
public $auth_assignment_table = '{{%auth_assignment}}';
public $auth_rule_table = '{{%auth_rule}}';
public $controllerNamespace = 'webvimark\modules\UserManagement\controllers';
/**
* @p
*/
public function init()
{
parent::init();
$this->prepareMailerOptions();
}
/**
* For Menu
*
* @return array
*/
public static function menuItems()
{
return [
['label' => '<i class="fa fa-angle-double-right"></i> ' . UserManagementModule::t('back', 'Users'), 'url' => ['/user-management/user/index']],
['label' => '<i class="fa fa-angle-double-right"></i> ' . UserManagementModule::t('back', 'Roles'), 'url' => ['/user-management/role/index']],
['label' => '<i class="fa fa-angle-double-right"></i> ' . UserManagementModule::t('back', 'Permissions'), 'url' => ['/user-management/permission/index']],
['label' => '<i class="fa fa-angle-double-right"></i> ' . UserManagementModule::t('back', 'Permission groups'), 'url' => ['/user-management/auth-item-group/index']],
['label' => '<i class="fa fa-angle-double-right"></i> ' . UserManagementModule::t('back', 'Visit log'), 'url' => ['/user-management/user-visit-log/index']],
];
}
/**
* I18N helper
*
* @param string $category
* @param string $message
* @param array $params
* @param null|string $language
*
* @return string
*/
public static function t($category, $message, $params = [], $language = null)
{
if ( !isset(Yii::$app->i18n->translations['modules/user-management/*']) )
{
Yii::$app->i18n->translations['modules/user-management/*'] = [
'class' => 'yii\i18n\PhpMessageSource',
'sourceLanguage' => 'en',
'basePath' => '@vendor/webvimark/module-user-management/messages',
'fileMap' => [
'modules/user-management/back' => 'back.php',
'modules/user-management/front' => 'front.php',
],
];
}
return Yii::t('modules/user-management/' . $category, $message, $params, $language);
}
/**
* Check how much attempts user has been made in X seconds
*
* @return bool
*/
public function checkAttempts()
{
$lastAttempt = Yii::$app->session->get(static::SESSION_LAST_ATTEMPT);
if ( $lastAttempt )
{
$attemptsCount = Yii::$app->session->get(static::SESSION_ATTEMPT_COUNT, 0);
Yii::$app->session->set(static::SESSION_ATTEMPT_COUNT, ++$attemptsCount);
// If last attempt was made more than X seconds ago then reset counters
if ( ( $lastAttempt + $this->attemptsTimeout ) < time() )
{
Yii::$app->session->set(static::SESSION_LAST_ATTEMPT, time());
Yii::$app->session->set(static::SESSION_ATTEMPT_COUNT, 1);
return true;
}
elseif ( $attemptsCount > $this->maxAttempts )
{
return false;
}
return true;
}
Yii::$app->session->set(static::SESSION_LAST_ATTEMPT, time());
Yii::$app->session->set(static::SESSION_ATTEMPT_COUNT, 1);
return true;
}
/**
* Merge given mailer options with default
*/
protected function prepareMailerOptions()
{
if ( !isset($this->mailerOptions['from']) )
{
$this->mailerOptions['from'] = [Yii::$app->params['adminEmail'] => Yii::$app->name . ' robot'];
}
$this->mailerOptions = ArrayHelper::merge($this->_defaultMailerOptions, $this->mailerOptions);
}
}