From fb202393bd0f64067433f0181e314111e3cfcbff Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 4 Sep 2024 10:57:30 +0200 Subject: [PATCH 1/3] #1096: fixed `findExistingForSocialLogin` finder --- src/Model/Behavior/SocialBehavior.php | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index 36c0fb4c6..03a01e927 100644 --- a/src/Model/Behavior/SocialBehavior.php +++ b/src/Model/Behavior/SocialBehavior.php @@ -14,8 +14,10 @@ namespace CakeDC\Users\Model\Behavior; use Cake\Core\Configure; +use Cake\Database\Expression\QueryExpression; use Cake\Datasource\EntityInterface; use Cake\Event\EventDispatcherTrait; +use Cake\ORM\Query\SelectQuery; use Cake\Utility\Hash; use CakeDC\Users\Exception\AccountNotActiveException; use CakeDC\Users\Exception\MissingEmailException; @@ -138,7 +140,6 @@ protected function _createSocialUser($data, $options = []) $useEmail = $options['use_email'] ?? null; $validateEmail = (bool)($options['validate_email'] ?? null); $tokenExpiration = $options['token_expiration'] ?? null; - $existingUser = null; $email = $data['email'] ?? null; if ($useEmail && empty($email)) { throw new MissingEmailException(__d('cake_d_c/users', 'Email not present')); @@ -276,19 +277,17 @@ public function generateUniqueUsername($username) * Prepare a query to retrieve existing entity for social login * * @param \Cake\ORM\Query\SelectQuery $query The base query. - * @param array $options Find options with email key. + * @param mixed $email Find options with email key. * @return \Cake\ORM\Query\SelectQuery */ - public function findExistingForSocialLogin(\Cake\ORM\Query\SelectQuery $query, array $options) + public function findExistingForSocialLogin(SelectQuery $query, mixed $email): SelectQuery { - $email = $options['email'] ?? null; - if (!$email) { - return $query->where('1 != 1'); - } - - return $query->where([ - $this->_table->aliasField('email') => $email, - ]); + return $query->where( + fn(QueryExpression $expression) => $expression->eq( + $this->_table->aliasField('email'), + $email + ) + ); } /** From 313ef12e78d4b3bad13ef80fa1ac609b8fd04c3f Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 4 Sep 2024 14:46:49 +0200 Subject: [PATCH 2/3] #1096: simplified `findExistingForSocialLogin` finder --- src/Model/Behavior/SocialBehavior.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index 03a01e927..2c0e982c8 100644 --- a/src/Model/Behavior/SocialBehavior.php +++ b/src/Model/Behavior/SocialBehavior.php @@ -14,7 +14,6 @@ namespace CakeDC\Users\Model\Behavior; use Cake\Core\Configure; -use Cake\Database\Expression\QueryExpression; use Cake\Datasource\EntityInterface; use Cake\Event\EventDispatcherTrait; use Cake\ORM\Query\SelectQuery; @@ -277,17 +276,18 @@ public function generateUniqueUsername($username) * Prepare a query to retrieve existing entity for social login * * @param \Cake\ORM\Query\SelectQuery $query The base query. - * @param mixed $email Find options with email key. + * @param string|null $email Find options with email key. * @return \Cake\ORM\Query\SelectQuery */ - public function findExistingForSocialLogin(SelectQuery $query, mixed $email): SelectQuery + public function findExistingForSocialLogin(SelectQuery $query, ?string $email = null): SelectQuery { - return $query->where( - fn(QueryExpression $expression) => $expression->eq( - $this->_table->aliasField('email'), - $email - ) - ); + if (!$email) { + return $query->where('1 != 1'); + } + + return $query->where([ + $this->_table->aliasField('email') => $email, + ]); } /** From 325c48cda803182c74cfbfa35ad65c24900f62c2 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 4 Sep 2024 20:18:34 +0200 Subject: [PATCH 3/3] #1096: fixed cs --- .../Controller/Traits/Webauthn2FaTraitTest.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/tests/TestCase/Controller/Traits/Webauthn2FaTraitTest.php b/tests/TestCase/Controller/Traits/Webauthn2FaTraitTest.php index 6dfa19bfd..2550c1194 100644 --- a/tests/TestCase/Controller/Traits/Webauthn2FaTraitTest.php +++ b/tests/TestCase/Controller/Traits/Webauthn2FaTraitTest.php @@ -108,11 +108,10 @@ public function testWebauthn2faIsRegister() $this->Trait ->expects($this->exactly(2)) ->method('set') - ->willReturnCallback(fn ($name, $value) => - match ([$name, $value]) { - ['isRegister', true] => null, - ['username', 'user-2'] => null - }); + ->willReturnCallback(fn ($name, $value) => match ([$name, $value]) { + ['isRegister', true] => null, + ['username', 'user-2'] => null + }); $this->Trait->webauthn2fa(); $this->assertSame( $user, @@ -146,11 +145,10 @@ public function testWebauthn2faDontRequireRegister() $this->Trait ->expects($this->exactly(2)) ->method('set') - ->willReturnCallback(fn ($name, $value) => - match ([$name, $value]) { - ['isRegister', false] => null, - ['username', 'user-1'] => null - }); + ->willReturnCallback(fn ($name, $value) => match ([$name, $value]) { + ['isRegister', false] => null, + ['username', 'user-1'] => null + }); $this->Trait->webauthn2fa(); $this->assertSame( $user,