commit d7010b6c3b7e6471f8842c3931ed7cf41e083782 Author: Dave M Date: Tue Oct 6 15:59:08 2020 +0000 - First commit of ulmus user package diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2881c9f --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "mcnd/ulmus-user", + "description": "A simple user entity library easing privileges managements.", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Dave Mc Nicoll", + "email": "mcndave@gmail.com" + } + ], + "require": { + "mcnd/notes": "master-dev" + }, + "autoload": { + "psr-4": { + "Ulmus\\User\\": "src/" + } + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php new file mode 100644 index 0000000..f41af29 --- /dev/null +++ b/src/Entity/User.php @@ -0,0 +1,109 @@ + true) + */ + public int $id; + + /** + * @Field("size" => 35, "name" => "first_name") + */ + public ? string $firstName; + + /** + * @Field("size" => 35, "name" => "last_name") + */ + public ? string $lastName; + + /** + * @Field("size" => 150) + */ + public string $email; + + /** + * @Field("size" => 150) + */ + public ? string $address; + + /** + * @Field("size" => 15, 'name' => "zip_code") + */ + public ? string $zipCode; + + /** + * @Field("size" => 45) + */ + public ? string $province; + + /** + * @Field("size" => 3) + */ + public ? string $country; + + /** + * @Field("size" => 15) + */ + public ? string $phone; + + /** + * @Field("size" => 15) + */ + public ? string $ext; + + /** + * @Field("size" => 15) + */ + public ? string $mobile; + + /** + * @Field("size" => 255) + */ + public ? string $username; + + /** + * @Field + */ + public string $password; + + /** + * @UpdatedAt('readonly' => true, 'name' => "updated_at") + */ + public ? Datetime $updatedAt; + + /** + * @CreatedAt('readonly' => true, 'name' => "created_at") + */ + public Datetime $createdAt; + + public bool $logged = false; + + public function setPassword($password) : self + { + $this->password = $password; + + return $this->hashPassword(); + } + + public function hashPassword() : self + { + $this->password = password_hash($this->password, PASSWORD_DEFAULT); + + return $this; + } + + public function verifyPassword(string $password) : bool + { + return password_verify($password, $this->password ); + } + + public function fullname() : string + { + return trim( ( $this->firstName ?? "" ) . " " . ( $this->lastName ?? "" ) ); + } +} diff --git a/src/Exception/InvalidLoginCredentialsException.php b/src/Exception/InvalidLoginCredentialsException.php new file mode 100644 index 0000000..5c39500 --- /dev/null +++ b/src/Exception/InvalidLoginCredentialsException.php @@ -0,0 +1,5 @@ +session = $session; + $this->cookie = $cookie; + $this->authenticationEvent = $authenticationEvent ?: function(bool $authenticated, string $message, ? User $user) : void {} ; + } + + public function rememberMe(\Ulmus\Repository $repository) : ? User + { + $logUser = function(int $id) use ($repository) { + if ( null === ( $user = $repository->loadOne($id) ) ) { + throw new \Exception("User not found."); + } + + $user->logged = true; + + return $user; + }; + + if ( $this->session && $this->session->has("user.id") ) { + return $logUser($this->session->get("user.id")); + } + + if ( $this->cookie && $this->cookie->has("user.id") ) { + return $logUser($this->cookie->get("user.id")); + } + + return null; + } + + public function forgetMe() { + $this->cookie->delete("user.id"); + $this->session->destroy(); + } + + public function authenticate(\Ulmus\Repository $repository, array $userLogin, string $password) : User + { + foreach($userLogin as $field => $value) { + $repository->or($field, $value); + } + + try { + $this->user = $repository->loadOne(); + } + catch(Exception\EmptyDatasetException $e) { + call_user_func_array($this->authenticationEvent, [ false, 'userNotFound', $this->user ]); + + return $repository->instanciateEntity(); + } + + if ( $this->user && $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 = $repository->instanciateEntityCollection(); + $this->user->logged = false; + + call_user_func_array($this->authenticationEvent, [ false, 'invalidPassword', $this->user ]); + } + + return $this->user; + } + + /** + * Force user disconnection and handle memory trashing + */ + public function logout() : self + { + if ( $this->session ) { + $this->session->delete('user.id'); + } + + if ( $this->cookie ) { + $this->cookie->delete('user.id'); + } + + $this->user->logged = false; + + return $this; + } +}