From d850b917d717f196195825c20035c8788068a959 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 4 Sep 2024 11:03:58 +0200 Subject: [PATCH 1/5] #1096: fixed `findExistingForSocialLogin` finder --- src/Model/Behavior/SocialBehavior.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index 763be8c9..d6e35f80 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; use Cake\Utility\Hash; use CakeDC\Users\Exception\AccountNotActiveException; use CakeDC\Users\Exception\MissingEmailException; @@ -276,11 +278,15 @@ public function generateUniqueUsername($username) * @param array $options Find options with email key. * @return \Cake\ORM\Query */ - public function findExistingForSocialLogin(\Cake\ORM\Query $query, array $options) + public function findExistingForSocialLogin(Query $query, array $options) { - return $query->where([ - $this->_table->aliasField('email') => $options['email'], - ]); + if (!array_key_exists('email', $options)) { + throw new MissingEmailException(__d('cake_d_c/users', 'Email not present')); + } + + return $query->where(fn(QueryExpression $expression) => $expression + ->eq($this->_table->aliasField('email'), $options['email']) + ); } /** From 76293422987a968c4766d97ef20e720fc44f836d Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 4 Sep 2024 11:10:06 +0200 Subject: [PATCH 2/5] #1096: updated exception message --- src/Model/Behavior/SocialBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index d6e35f80..84e1f783 100644 --- a/src/Model/Behavior/SocialBehavior.php +++ b/src/Model/Behavior/SocialBehavior.php @@ -281,7 +281,7 @@ public function generateUniqueUsername($username) public function findExistingForSocialLogin(Query $query, array $options) { if (!array_key_exists('email', $options)) { - throw new MissingEmailException(__d('cake_d_c/users', 'Email not present')); + throw new MissingEmailException(__d('cake_d_c/users', 'Missing `email` option in option array')); } return $query->where(fn(QueryExpression $expression) => $expression From 299bfcd26c2fc647adb36b87a4b54a93b6de8657 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 4 Sep 2024 11:10:25 +0200 Subject: [PATCH 3/5] #1096: updated exception message --- src/Model/Behavior/SocialBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index 84e1f783..e15c0169 100644 --- a/src/Model/Behavior/SocialBehavior.php +++ b/src/Model/Behavior/SocialBehavior.php @@ -281,7 +281,7 @@ public function generateUniqueUsername($username) public function findExistingForSocialLogin(Query $query, array $options) { if (!array_key_exists('email', $options)) { - throw new MissingEmailException(__d('cake_d_c/users', 'Missing `email` option in option array')); + throw new MissingEmailException(__d('cake_d_c/users', 'Missing `email` option in options array')); } return $query->where(fn(QueryExpression $expression) => $expression From 916e1581ce25e778283c31fa72d1ab1688f89cc0 Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Wed, 4 Sep 2024 20:30:13 +0200 Subject: [PATCH 4/5] #1096: fixed cs --- src/Model/Behavior/SocialBehavior.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index e15c0169..218d7e39 100644 --- a/src/Model/Behavior/SocialBehavior.php +++ b/src/Model/Behavior/SocialBehavior.php @@ -285,8 +285,7 @@ public function findExistingForSocialLogin(Query $query, array $options) } return $query->where(fn(QueryExpression $expression) => $expression - ->eq($this->_table->aliasField('email'), $options['email']) - ); + ->eq($this->_table->aliasField('email'), $options['email'])); } /** From 7c27286420ea5539db9f1fc6b62fe36f0e606ccc Mon Sep 17 00:00:00 2001 From: Adam Rusinowski Date: Thu, 5 Sep 2024 09:49:39 +0200 Subject: [PATCH 5/5] #1096: simplified finder --- src/Model/Behavior/SocialBehavior.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Model/Behavior/SocialBehavior.php b/src/Model/Behavior/SocialBehavior.php index 218d7e39..097955fa 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; @@ -140,7 +139,6 @@ protected function _createSocialUser($data, $options = []) $useEmail = $options['use_email'] ?? null; $validateEmail = $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')); @@ -283,9 +281,13 @@ public function findExistingForSocialLogin(Query $query, array $options) if (!array_key_exists('email', $options)) { throw new MissingEmailException(__d('cake_d_c/users', 'Missing `email` option in options array')); } + if (!$options['email']) { + return $query->where('1 != 1'); + } - return $query->where(fn(QueryExpression $expression) => $expression - ->eq($this->_table->aliasField('email'), $options['email'])); + return $query->where([ + $this->_table->aliasField('email') => $options['email'], + ]); } /**