Skip to content

Commit

Permalink
nullable eventManager
Browse files Browse the repository at this point in the history
  • Loading branch information
sinbadxiii committed Sep 9, 2022
1 parent c426d32 commit af4cea9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,31 @@ class RememberToken extends Model implements RememberTokenInterface
{
return $this->user_agent;
}

public function beforeValidationOnCreate()
{
$this->created_at = date(DATE_ATOM);
$this->updated_at = date(DATE_ATOM);
if (!$this->expired_at) {
$this->expired_at = date(DATE_ATOM);
}
}

public function beforeValidationOnSave()
{
if (!$this->created_at) {
$this->created_at = date(DATE_ATOM);
}
if (!$this->expired_at) {
$this->expired_at = date(DATE_ATOM);
}
$this->updated_at = date(DATE_ATOM);
}

public function beforeValidationOnUpdate()
{
$this->updated_at = date(DATE_ATOM);
}
}
```
Интерфейс `Sinbadxiii\PhalconAuth\AuthenticatableInterface` имеет следущий вид:
Expand Down
37 changes: 26 additions & 11 deletions src/Guard/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
use Phalcon\Session\ManagerInterface as SessionManagerInterface;
use Phalcon\Events\ManagerInterface as EventsManagerInterface;

use function date;
use function is_null;
use function json_encode;
use function sha1;

/**
* Class Session
Expand Down Expand Up @@ -66,7 +69,7 @@ class Session extends AbstractEventsAware implements

public function __construct(
AdapterInterface $adapter, SessionManagerInterface $session,
Cookies $cookies, Request $request, EventsManagerInterface $eventsManager)
Cookies $cookies, Request $request, ?EventsManagerInterface $eventsManager)
{
$this->adapter = $adapter;
$this->session = $session;
Expand Down Expand Up @@ -198,7 +201,9 @@ public function getRememberName(): string
*/
public function login(AuthenticatableInterface $user, bool $remember = false): void
{
$this->eventsManager->fire("auth:beforeLogin", $this);
if ($this->eventsManager) {
$this->eventsManager->fire("auth:beforeLogin", $this);
}

$this->updateSession($user->getAuthIdentifier());

Expand All @@ -214,7 +219,9 @@ public function login(AuthenticatableInterface $user, bool $remember = false): v

$this->setUser($user);

$this->eventsManager->fire("auth:afterLogin", $this);
if ($this->eventsManager) {
$this->eventsManager->fire("auth:afterLogin", $this);
}
}

/**
Expand All @@ -240,12 +247,16 @@ public function loginById($id, bool $remember = false)
*/
public function once(array $credentials = []): bool
{
$this->eventsManager->fire("auth:beforeLogin", $this);
if ($this->eventsManager) {
$this->eventsManager->fire("auth:beforeLogin", $this);
}

if ($this->validate($credentials)) {
$this->setUser($this->lastUserAttempted);

$this->eventsManager->fire("auth:afterLogin", $this);
if ($this->eventsManager) {
$this->eventsManager->fire("auth:afterLogin", $this);
}

return true;
}
Expand Down Expand Up @@ -300,9 +311,11 @@ public function logout(): void
{
$user = $this->user();

$this->eventsManager->fire("auth:beforeLogout", $this, [
"user" => $user
]);
if ($this->eventsManager) {
$this->eventsManager->fire("auth:beforeLogout", $this, [
"user" => $user
]);
}

$recaller = $this->recaller();

Expand All @@ -318,9 +331,11 @@ public function logout(): void

$this->session->remove($this->getName());

$this->eventsManager->fire("auth:afterLogout", $this, [
"user" => $user
]);
if ($this->eventsManager) {
$this->eventsManager->fire("auth:afterLogout", $this, [
"user" => $user
]);
}

$this->user = null;
}
Expand Down
8 changes: 5 additions & 3 deletions src/ManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use function class_exists;
use function is_null;
use function call_user_func;
use function sprintf;
use function ucfirst;

/**
* Class Factory
Expand Down Expand Up @@ -51,7 +53,7 @@ class ManagerFactory extends Manager implements EventsAwareInterface
protected SessionManagerInterface $session;
protected Cookies $cookies;
protected Request $request;
protected EventsManagerInterface $eventsManager;
protected ?EventsManagerInterface $eventsManager;

/**
* @param array $config
Expand All @@ -67,7 +69,7 @@ public function __construct(
SessionManagerInterface $session = null,
Cookies $cookies = null,
Request $request = null,
EventsManagerInterface $eventsManager = null
?EventsManagerInterface $eventsManager = null
) {
$this->config = $config;

Expand All @@ -88,7 +90,7 @@ public function __construct(
$this->session = $session ?? Di::getDefault()->getShared("session");
$this->cookies = $cookies ?? Di::getDefault()->getShared("cookies");
$this->request = $request ?? Di::getDefault()->getShared("request");
$this->eventsManager = $eventsManager ?? Di::getDefault()->getShared("eventsManager");
$this->eventsManager = $eventsManager;
}

/**
Expand Down

0 comments on commit af4cea9

Please sign in to comment.