-
Notifications
You must be signed in to change notification settings - Fork 19
/
Yee.php
252 lines (216 loc) · 7.35 KB
/
Yee.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
<?php
/**
* @link http://www.yee-soft.com/
* @copyright Copyright (c) 2015 Taras Makitra
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
namespace yeesoft;
use yeesoft\helpers\LanguageHelper;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* Yee CMS
*
* @author Taras Makitra <[email protected]>
*/
class Yee extends \yii\base\Module
{
/**
* Version number of the module.
*/
const VERSION = '0.1-a';
const SESSION_LAST_ATTEMPT = '_um_last_attempt';
const SESSION_ATTEMPT_COUNT = '_um_attempt_count';
/**
* 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 = true;
/**
* 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']
'signup-confirmation' => '/mail/signup-email-confirmation-html',
'password-reset-mail' => '/mail/password-reset-html',
'confirm-email' => '/mail/email-confirmation-html',
];
/**
* 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';
/**
* After how many seconds confirmation token will be invalid
*
* @var int
*/
public $confirmationTokenExpire = 3600; // 1 hour
/**
* Roles that will be assigned to user registered via /auth/signup
*
* @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 $usernameRegexp = '/^(\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 $usernameBlackRegexp = '/^(.)*admin(.)*$/i';
/**
* 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' => 5,
'maxLength' => 5,
'height' => 45,
'width' => 100,
'fontFile' => '@vendor/yeesoft/yii2-yee-auth/fonts/lightweight.ttf',
'padding' => 0
];
/**
* 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 = 'yeesoft\usermanagement\controllers';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (Yii::$app->id != 'console') {
//Check language settings
if (!LanguageHelper::isSiteMultilingual() &&
!in_array(Yii::$app->language, array_keys(LanguageHelper::getLanguages()))
) {
throw new InvalidConfigException('Invalid language settings! Field "languages" in \common\config\params.php should contain the same language as "language" param in config\main.php');
}
//Check here: redirLangs not contains in langs param
$this->registerTranslations();
$this->prepareMailerOptions();
$this->initFormatter();
}
}
protected function registerTranslations()
{
Yii::$app->i18n->translations['yee*'] = [
'class' => 'yeesoft\i18n\DbMessageSource',
'sourceLanguage' => 'en-US',
'enableCaching' => true,
];
}
protected function initFormatter()
{
date_default_timezone_set(Yii::$app->settings->get('general.timezone', 'UTC'));
Yii::$app->formatter->timeZone = Yii::$app->settings->get('general.timezone', 'UTC');
Yii::$app->formatter->dateFormat = Yii::$app->settings->get('general.dateformat', "yyyy-MM-dd");
Yii::$app->formatter->timeFormat = Yii::$app->settings->get('general.timeformat', "HH:mm");
Yii::$app->formatter->datetimeFormat = Yii::$app->formatter->dateFormat . " " . Yii::$app->formatter->timeFormat;
}
/**
* 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];
}
$this->mailerOptions = ArrayHelper::merge($this->_defaultMailerOptions, $this->mailerOptions);
}
/**
* Returns an HTML hyperlink that can be displayed on your Web page.
* @return string
*/
public static function powered()
{
return '<a href="http://www.yee-soft.com/" rel="external">Yee CMS</a>';
}
/**
* Returns a string representing the current version of the Yee CMS Core.
* @return string the version of Yee CMS Core
*/
public static function getVersion()
{
return self::VERSION;
}
}