From 374b056754ff1150168bf1be097c52f492ba9ee4 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 19 Oct 2021 12:41:52 +0000 Subject: [PATCH 1/2] - Made workable with external instances, was previously too contrained to allow for such events --- src/Lib/Authenticate.php | 50 ++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/Lib/Authenticate.php b/src/Lib/Authenticate.php index e876d31..45e2b67 100644 --- a/src/Lib/Authenticate.php +++ b/src/Lib/Authenticate.php @@ -30,15 +30,13 @@ class Authenticate { ) { $this->session = $session; $this->cookie = $cookie; - $this->authenticationEvent = $authenticationEvent ?: function(bool $authenticated, string $message, ? User $user, array $data = []) : bool {return false;} ; + $this->authenticationEvent = $authenticationEvent ?: function(bool $authenticated, string $message, ? User $user, array $data = []) : ? bool {return null;} ; } public function rememberMe(\Ulmus\Repository $repository) : ? User { $logUser = function(int $id) use ($repository) { if ( null === ( $user = $repository->loadFromPk($id) ) ) { - $this->logout(); - throw new \Exception("User not found."); } @@ -63,7 +61,7 @@ class Authenticate { $this->session->destroy(); } - public function authenticate(\Ulmus\Repository $repository, array $userLogin, ? string $password) : User + public function authenticate(\Ulmus\Repository $repository, array $userLogin, string $password) : User { foreach($userLogin as $field => $value) { $repository->or($field, $value); @@ -78,23 +76,35 @@ class Authenticate { } } - if ( $this->user->isLoaded() && ( call_user_func_array($this->authenticationEvent, [ false, 'verifyPassword', $this->user, [ 'password' => $password ] ]) || $this->user->verifyPassword($password) ) ) { - $this->user->logged = true; - - if ( $this->session ) { - $this->session->set("user.id", $this->user->id); - } - - if ( $this->cookie ) { - $this->cookie->set("user.id", $this->user->id); - } - - call_user_func_array($this->authenticationEvent, [ true, 'success', $this->user ]); + if ( ! $this->user->isLoaded() ) { + call_user_func_array($this->authenticationEvent, [ false, 'userNotFound', $this->user, [ 'user_login' => $userLogin, 'password' => $password ] ]); } - else { - $this->user->logged = false; - call_user_func_array($this->authenticationEvent, [ false, 'invalidPassword', $this->user ]); + if ($this->user->isLoaded()) { + $response = call_user_func_array($this->authenticationEvent, [ false, 'verifyPassword', $this->user, [ 'password' => $password ] ]); + + if ( $response !== null ? $response : $this->user->verifyPassword($password) ) { + $this->user->logged = true; + + if ( $this->session ) { + $this->session->set("user.id", $this->user->id); + } + + if ( $this->cookie ) { + $this->cookie->set("user.id", $this->user->id); + } + + call_user_func_array($this->authenticationEvent, [ true, 'success', $this->user ]); + } + else { + $this->user->logged = false; + + call_user_func_array($this->authenticationEvent, [ false, 'invalidPassword', $this->user ]); + } + } + + if ( ! $this->user->isLoaded() ) { + call_user_func_array($this->authenticationEvent, [ false, 'authenticationFailed', $this->user, [ 'user_login' => $userLogin, 'password' => $password ] ]); } return $this->user; @@ -113,7 +123,7 @@ class Authenticate { $this->cookie->delete('user.id'); } - $this->user && $this->user->logged = false; + $this->user->logged = false; return $this; } From 267fe5cadd44b17bacc354fd4a4124becba2ebd7 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 10 Oct 2022 14:27:55 +0000 Subject: [PATCH 2/2] Fixed some loading problems with loaded user and ajusted the Email allowed length (to 255) --- src/Entity/User.php | 2 +- src/Lib/Authenticate.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Entity/User.php b/src/Entity/User.php index 9de20d0..578350d 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -22,7 +22,7 @@ class User { public ? string $lastName; /** - * @Field("size" => 150) + * @Field("size" => 255) */ public string $email; diff --git a/src/Lib/Authenticate.php b/src/Lib/Authenticate.php index 45e2b67..f38aaee 100644 --- a/src/Lib/Authenticate.php +++ b/src/Lib/Authenticate.php @@ -35,8 +35,8 @@ class Authenticate { public function rememberMe(\Ulmus\Repository $repository) : ? User { - $logUser = function(int $id) use ($repository) { - if ( null === ( $user = $repository->loadFromPk($id) ) ) { + $logUser = function(? int $id) use ($repository) { + if ( $id === null || null === ( $user = $repository->loadFromPk($id) ) ) { throw new \Exception("User not found."); }