- First commit of ulmus user package

This commit is contained in:
Dave M. 2020-10-06 15:59:08 +00:00
commit d7010b6c3b
4 changed files with 253 additions and 0 deletions

20
composer.json Normal file
View File

@ -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/"
}
}
}

109
src/Entity/User.php Normal file
View File

@ -0,0 +1,109 @@
<?php
namespace Ulmus\User\Entity;
use Ulmus\Entity\Field\Datetime;
class User {
/**
* @Id('readonly' => 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 ?? "" ) );
}
}

View File

@ -0,0 +1,5 @@
<?php
namespace Ulmus\User\Exception;
class InvalidLoginCredentialsException extends \Exception {}

119
src/Lib/Authenticate.php Normal file
View File

@ -0,0 +1,119 @@
<?php
namespace Ulmus\User\Lib;
use Storage\{Session, Cookie};
use \Closure;
use Ulmus\User\Entity\User;
use Ulmus\Exception;
class Authenticate {
protected ? Session $session;
protected ? Cookie $cookie;
protected bool $logged = false;
protected Closure $authenticationEvent;
public ? User $user = null;
public string $connection_fields = 'email';
public function __construct(
? Session $session = null,
? Cookie $cookie = null,
? Closure $authenticationEvent = null
) {
$this->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;
}
}