Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix SocialNetworkAuthenticateService #544

Merged
merged 2 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions src/User/Service/SocialNetworkAuthenticateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,15 +53,15 @@ 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;
}
if ($account === null) {
$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;
}
Expand All @@ -72,11 +75,16 @@ 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 {
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());
Expand Down
Loading