Skip to content

Commit cddeb24

Browse files
committed
fix Prevent Duplicate Entry Errors
1 parent c492ae8 commit cddeb24

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

src/Controllers/OAuthController.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public function callBack(): RedirectResponse
9191
$updateFields = $oauthClass->getColumnsName('syncingUserInfo', $userInfo);
9292

9393
$userid = $this->syncingUserInfo($find, $updateFields);
94+
95+
if ($this->userExist->isBanned()) {
96+
return redirect()->to(config('Auth')->logoutRedirect())->with('error', $this->userExist->getBanMessage() ?? lang('Auth.bannedUser'));
97+
}
9498
} else {
9599
// Check config setting first to see if it can register automatically or not
96100
if (setting('ShieldOAuthConfig.oauthConfigs')[$oauthName]['allow_register'] === false) {
@@ -102,19 +106,19 @@ public function callBack(): RedirectResponse
102106
// new user
103107
$entitiesUser = new User($oauthClass->getColumnsName('newUser', $userInfo));
104108

105-
$users->save($entitiesUser);
106-
$userid = $users->getInsertID();
109+
try {
110+
$userid = $users->insert($entitiesUser);
111+
} catch (\Throwable $th) {
112+
//If the insert fails due to a duplicate key entry, see the log message for audit.
113+
return redirect()->to(config('Auth')->logoutRedirect())->with('error', lang('ShieldOAuthLang.Callback.account_disabled'));
114+
}
115+
107116
// To get the complete user object with ID, we need to get from the database
108-
$user = $users->findById($userid);
109-
$users->save($user);
117+
$user = $users->find($userid);
110118
// Add to default group
111119
$users->addToDefaultGroup($user);
112120
}
113121

114-
if ($this->userExist && $this->userExist->isBanned()) {
115-
return redirect()->to(config('Auth')->logoutRedirect())->with('error', $this->userExist->getBanMessage() ?? lang('Auth.bannedUser'));
116-
}
117-
118122
auth()->loginById($userid);
119123
$this->recordLoginAttempt($oauthName, $userInfo->email);
120124

src/Language/en/ShieldOAuthLang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'anti_forgery' => 'Your request has been detected as fake. we are sorry!',
2121
'account_not_found' => 'There is no account registered with the email "{0}".',
2222
'access_denied' => 'Authentication cancelled! You declined {0} permissions.',
23+
'account_disabled' => 'This account is no longer active. Please contact administrator for assistance.',
2324
],
2425

2526
// ShieldOAuthButton in views

src/Language/fa/ShieldOAuthLang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'anti_forgery' => 'متاسفانه، تلاش شما ، یک درخواست جعلی تشخیص داده شد.',
2121
'account_not_found' => 'هیچ حسابی با ایمیل "{0}" ثبت نشده است.',
2222
'access_denied' => 'تأیید اعتبار لغو شد! شما دسترسی‌های {0} را رد کردید.',
23+
'account_disabled' => '(To be translated) This account is no longer active. Please contact administrator for assistance.',
2324
],
2425

2526
// ShieldOAuthButton in views

src/Language/fr/ShieldOAuthLang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'anti_forgery' => 'Votre demande a été détectée comme erronée. Nous sommes désolés!',
2121
'account_not_found' => 'Il n\'y a pas de compte enregistré avec l\'email "{0}".',
2222
'access_denied' => 'Authentification annulée ! Vous avez refusé les autorisations {0}.',
23+
'account_disabled' => '(To be translated) This account is no longer active. Please contact administrator for assistance.',
2324
],
2425

2526
// ShieldOAuthButton in views

src/Language/id/ShieldOAuthLang.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'anti_forgery' => 'Maaf, permintaan Anda terdeteksi tidak valid!',
2121
'account_not_found' => 'Tidak ada akun yang terdaftar dengan email "{0}".',
2222
'access_denied' => 'Autentikasi dibatalkan! Anda menolak izin {0}.',
23+
'account_disabled' => 'Akun ini sudah tidak aktif. Silakan hubungi administrator untuk mendapatkan bantuan.',
2324
],
2425

2526
// ShieldOAuthButton in views

0 commit comments

Comments
 (0)