Skip to content

Commit 8fdd6b9

Browse files
added logging of login attempts
1 parent 2491ef3 commit 8fdd6b9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/controllers/UserController.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use ZakharovAndrew\user\models\ChangePasswordForm;
1717
use ZakharovAndrew\user\models\UserSettings;
1818
use ZakharovAndrew\user\models\UserSettingsConfig;
19+
use ZakharovAndrew\user\models\LoginAttempt;
1920
use yii\helpers\Url;
2021
// for avatar uploading
2122
use yii\web\UploadedFile;
@@ -295,10 +296,25 @@ public function actionLogin()
295296
if (!Yii::$app->user->isGuest) {
296297
return $this->goHome();
297298
}
299+
300+
// Get the user's IP address
301+
$userIp = Yii::$app->request->userIP;
302+
303+
// Check if the IP address is blocked
304+
if (LoginAttempt::isBlockedByIp($userIp)) {
305+
Yii::$app->session->setFlash('error', Module::t('Too many unsuccessful attempts. Please wait an hour before trying again.'));
306+
return $this->render('login', ['model' => $model]);
307+
}
298308

299309
$model = new \ZakharovAndrew\user\models\LoginForm();
300310
if ($model->load(Yii::$app->request->post()) && $model->login()) {
311+
// Successful authentication
312+
LoginAttempt::logLoginAttempt($model->username, true);
301313
return $this->goBack();
314+
} else {
315+
// Unsuccessful authentication
316+
LoginAttempt::logLoginAttempt($model->username, false);
317+
Yii::$app->session->setFlash('error', Module::t('Incorrect username or password.' ));
302318
}
303319

304320
$model->password = '';
@@ -494,7 +510,7 @@ public function actionToggleColumnVisibility()
494510
Yii::$app->session->set('gridViewColumnVisibility', $columnVisibility);
495511
}
496512
}
497-
513+
498514
/**
499515
* Finds the User model based on its primary key value.
500516
* If the model is not found, a 404 HTTP exception will be thrown.

src/messages/ru/user.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@
7070
'Profile update error' => 'Ошибка обновления профиля',
7171
'Today, no one is celebrating a birthday.' => 'Сегодня никто не празднует день рождения.',
7272
'Today’s birthdays:' => 'Сегодня отмечают дни рождения:',
73+
'IP Address' => 'IP-адрес',
74+
'Attempt Time' => 'Время попытки',
75+
'Successful Attempt' => 'Успешная попытка',
76+
'Too many unsuccessful attempts. Please wait an hour before trying again.' => 'Слишком много неудачных попыток. Пожалуйста, подождите час перед следующей попыткой.',
77+
'Incorrect username or password.' => 'Неверное имя пользователя или пароль.',
7378
// actions
7479
'Create User' => 'Создать пользователя',
7580
'Save' => 'Сохранить',

0 commit comments

Comments
 (0)