From 5d8dadfaaef56c77c5590471299015f12bef7a28 Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Thu, 7 Mar 2024 18:47:00 +0100 Subject: [PATCH 1/2] Update last_login_at and last_login_ip on login via auth action --- CHANGELOG.md | 1 + src/User/Service/SocialNetworkAuthenticateService.php | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f922f622..033fa2d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## dev +- Fix: Update last_login_at and last_login_ip on social networt authenticate (e.luhr) - Enh: Keycloak auth client (e.luhr) - Fix: Social Network Auth (eluhr) - Enh #532: /user/registration/register now shows form validation errors diff --git a/src/User/Service/SocialNetworkAuthenticateService.php b/src/User/Service/SocialNetworkAuthenticateService.php index c2626848..dd604b87 100644 --- a/src/User/Service/SocialNetworkAuthenticateService.php +++ b/src/User/Service/SocialNetworkAuthenticateService.php @@ -74,9 +74,14 @@ public function run() Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Your account has been blocked.')); $this->authAction->setSuccessUrl(Url::to(['/user/security/login'])); } else { - Yii::$app->user->login($account->user, $this->controller->module->rememberLoginLifespan); - $this->authAction->setSuccessUrl(Yii::$app->getUser()->getReturnUrl()); - $result = true; + $result = Yii::$app->user->login($account->user, $this->controller->module->rememberLoginLifespan); + if ($result) { + $account->user->updateAttributes([ + 'last_login_at' => time(), + 'last_login_ip' => $this->controller->module->disableIpLogging ? '127.0.0.1' : Yii::$app->request->getUserIP(), + ]); + $this->authAction->setSuccessUrl(Yii::$app->getUser()->getReturnUrl()); + } } } else { $this->authAction->setSuccessUrl($account->getConnectionUrl()); From e6d696562b7f349cd47d4ce0e07738de0bd060e5 Mon Sep 17 00:00:00 2001 From: Elias Luhr Date: Thu, 7 Mar 2024 18:52:38 +0100 Subject: [PATCH 2/2] fix hardcoded urls in SocialNetworkAuthenticateService --- src/User/Service/SocialNetworkAuthenticateService.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/User/Service/SocialNetworkAuthenticateService.php b/src/User/Service/SocialNetworkAuthenticateService.php index dd604b87..5d7c6692 100644 --- a/src/User/Service/SocialNetworkAuthenticateService.php +++ b/src/User/Service/SocialNetworkAuthenticateService.php @@ -19,12 +19,15 @@ use Da\User\Model\User; use Da\User\Query\SocialNetworkAccountQuery; use Da\User\Query\UserQuery; +use Da\User\Traits\ModuleAwareTrait; use Yii; use yii\authclient\AuthAction; use yii\helpers\Url; class SocialNetworkAuthenticateService implements ServiceInterface { + use ModuleAwareTrait; + protected $controller; protected $authAction; protected $client; @@ -50,7 +53,7 @@ public function run() $account = $this->socialNetworkAccountQuery->whereClient($this->client)->one(); if (!$this->controller->module->enableSocialNetworkRegistration && ($account === null || $account->user === null)) { Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Registration on this website is disabled')); - $this->authAction->setSuccessUrl(Url::to(['/user/security/login'])); + $this->authAction->setSuccessUrl(Url::to(['/' . $this->getModule()->id . '/security/login'])); return false; } @@ -58,7 +61,7 @@ public function run() $account = $this->createAccount(); if (!$account) { Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Unable to create an account.')); - $this->authAction->setSuccessUrl(Url::to(['/user/security/login'])); + $this->authAction->setSuccessUrl(Url::to(['/' . $this->getModule()->id . '/security/login'])); return false; } @@ -72,7 +75,7 @@ public function run() if ($account->user instanceof User) { if ($account->user->getIsBlocked()) { Yii::$app->session->setFlash('danger', Yii::t('usuario', 'Your account has been blocked.')); - $this->authAction->setSuccessUrl(Url::to(['/user/security/login'])); + $this->authAction->setSuccessUrl(Url::to(['/' . $this->getModule()->id . '/security/login'])); } else { $result = Yii::$app->user->login($account->user, $this->controller->module->rememberLoginLifespan); if ($result) {