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

fix: Prevent Duplicate Entry Errors #175

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
21 changes: 13 additions & 8 deletions src/Controllers/OAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Models\LoginModel;
use Datamweb\ShieldOAuth\Libraries\Basic\ControllersInterface;
use Throwable;

class OAuthController extends BaseController implements ControllersInterface
{
Expand Down Expand Up @@ -91,6 +92,10 @@ public function callBack(): RedirectResponse
$updateFields = $oauthClass->getColumnsName('syncingUserInfo', $userInfo);

$userid = $this->syncingUserInfo($find, $updateFields);

if ($this->userExist->isBanned()) {
return redirect()->to(config('Auth')->logoutRedirect())->with('error', $this->userExist->getBanMessage() ?? lang('Auth.bannedUser'));
}
} else {
// Check config setting first to see if it can register automatically or not
if (setting('ShieldOAuthConfig.oauthConfigs')[$oauthName]['allow_register'] === false) {
Expand All @@ -102,19 +107,19 @@ public function callBack(): RedirectResponse
// new user
$entitiesUser = new User($oauthClass->getColumnsName('newUser', $userInfo));

$users->save($entitiesUser);
$userid = $users->getInsertID();
try {
$userid = $users->insert($entitiesUser);
} catch (Throwable $th) {
// If the insert fails due to a duplicate key entry, see the log message for audit.
return redirect()->to(config('Auth')->logoutRedirect())->with('error', lang('ShieldOAuthLang.Callback.account_disabled'));
}

// To get the complete user object with ID, we need to get from the database
$user = $users->findById($userid);
$users->save($user);
$user = $users->find($userid);
// Add to default group
$users->addToDefaultGroup($user);
}

if ($this->userExist && $this->userExist->isBanned()) {
return redirect()->to(config('Auth')->logoutRedirect())->with('error', $this->userExist->getBanMessage() ?? lang('Auth.bannedUser'));
}

auth()->loginById($userid);
$this->recordLoginAttempt($oauthName, $userInfo->email);

Expand Down
1 change: 1 addition & 0 deletions src/Language/en/ShieldOAuthLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'anti_forgery' => 'Your request has been detected as fake. we are sorry!',
'account_not_found' => 'There is no account registered with the email "{0}".',
'access_denied' => 'Authentication cancelled! You declined {0} permissions.',
'account_disabled' => 'This account is no longer active. Please contact administrator for assistance.',
],

// ShieldOAuthButton in views
Expand Down
1 change: 1 addition & 0 deletions src/Language/fa/ShieldOAuthLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'anti_forgery' => 'متاسفانه، تلاش شما ، یک درخواست جعلی تشخیص داده شد.',
'account_not_found' => 'هیچ حسابی با ایمیل "{0}" ثبت نشده است.',
'access_denied' => 'تأیید اعتبار لغو شد! شما دسترسی‌های {0} را رد کردید.',
'account_disabled' => '(To be translated) This account is no longer active. Please contact administrator for assistance.',
],

// ShieldOAuthButton in views
Expand Down
1 change: 1 addition & 0 deletions src/Language/fr/ShieldOAuthLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'anti_forgery' => 'Votre demande a été détectée comme erronée. Nous sommes désolés!',
'account_not_found' => 'Il n\'y a pas de compte enregistré avec l\'email "{0}".',
'access_denied' => 'Authentification annulée ! Vous avez refusé les autorisations {0}.',
'account_disabled' => '(To be translated) This account is no longer active. Please contact administrator for assistance.',
],

// ShieldOAuthButton in views
Expand Down
1 change: 1 addition & 0 deletions src/Language/id/ShieldOAuthLang.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'anti_forgery' => 'Maaf, permintaan Anda terdeteksi tidak valid!',
'account_not_found' => 'Tidak ada akun yang terdaftar dengan email "{0}".',
'access_denied' => 'Autentikasi dibatalkan! Anda menolak izin {0}.',
'account_disabled' => 'Akun ini sudah tidak aktif. Silakan hubungi administrator untuk mendapatkan bantuan.',
],

// ShieldOAuthButton in views
Expand Down
Loading