- First commit of ulmus user package

This commit is contained in:
mcndave committed 2020-10-06 15:59:08 +00:00
commit d7010b6c3b
4 files changed
+253

No files matched your search

+119
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;
}
}