From c2ada2b9af2f7e43761366c9f58599ec079a65c1 Mon Sep 17 00:00:00 2001 From: Gabriel Almeida Date: Sun, 20 Jul 2014 07:25:14 +0000 Subject: [PATCH] Improve default UserValidator behavior --- src/Zizaco/Confide/UserValidator.php | 31 ++++++++++++++-------- tests/Zizaco/Confide/UserValidatorTest.php | 8 +++--- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Zizaco/Confide/UserValidator.php b/src/Zizaco/Confide/UserValidator.php index b49aeec..c8ea445 100644 --- a/src/Zizaco/Confide/UserValidator.php +++ b/src/Zizaco/Confide/UserValidator.php @@ -65,9 +65,9 @@ public function validate(ConfideUserInterface $user, $ruleset = 'create') $this->repo = App::make('confide.repository'); // Validate object - $result = $this->validatePassword($user) && - $this->validateIsUnique($user) && - $this->validateAttributes($user, $ruleset); + $result = $this->validateAttributes($user, $ruleset) ? true : false; + $result = ($this->validatePassword($user) && $result) ? true : false; + $result = ($this->validateIsUnique($user) && $result) ? true : false; return $result; } @@ -113,20 +113,29 @@ public function validatePassword(ConfideUserInterface $user) public function validateIsUnique(ConfideUserInterface $user) { $identity = [ - 'username' => $user->username, 'email' => $user->email, + 'username' => $user->username, ]; - $similar = $this->repo->getUserByIdentity($identity); + foreach($identity as $attribute => $value) { + + $similar = $this->repo->getUserByIdentity([$attribute => $value]); + + if (!$similar || $similar->getKey() == $user->getKey()) { + unset($identity[$attribute]); + } else { + $this->attachErrorMsg( + $user, 'confide::confide.alerts.duplicated_credentials', $attribute + ); + } - if (!$similar || $similar->getKey() == $user->getKey()) { - return true; } - $this->attachErrorMsg( - $user, 'confide::confide.alerts.duplicated_credentials' - ); - return false; + if(!$identity) { + return true; + } + + return false; } /** diff --git a/tests/Zizaco/Confide/UserValidatorTest.php b/tests/Zizaco/Confide/UserValidatorTest.php index 3d09875..06282b7 100644 --- a/tests/Zizaco/Confide/UserValidatorTest.php +++ b/tests/Zizaco/Confide/UserValidatorTest.php @@ -175,14 +175,14 @@ public function testShouldValidateIsUnique() ->andReturn($userD->id); $repo->shouldReceive('getUserByIdentity') - ->andReturnUsing(function($user) use ($userB, $userC) { - if ($user['email'] == $userB->email) return $userB; - if ($user['email'] == $userC->email) return $userC; + ->andReturnUsing(function($user) use ($userB, $userC) { + if (isset($user['email']) && $user['email'] == $userB->email) return $userB; + if (isset($user['email']) && $user['email'] == $userC->email) return $userC; }); $validator->shouldReceive('attachErrorMsg') ->atLeast(1) - ->with(m::any(), 'confide::confide.alerts.duplicated_credentials'); + ->with(m::any(), 'confide::confide.alerts.duplicated_credentials', 'email'); /* |------------------------------------------------------------