diff --git a/README.md b/README.md index 2c97643..2ac8d03 100644 --- a/README.md +++ b/README.md @@ -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` имеет следущий вид: diff --git a/src/Guard/Session.php b/src/Guard/Session.php index e1330d9..633b0a2 100644 --- a/src/Guard/Session.php +++ b/src/Guard/Session.php @@ -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 @@ -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; @@ -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()); @@ -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); + } } /** @@ -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; } @@ -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(); @@ -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; } diff --git a/src/ManagerFactory.php b/src/ManagerFactory.php index 13e5002..7c1eba5 100644 --- a/src/ManagerFactory.php +++ b/src/ManagerFactory.php @@ -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 @@ -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 @@ -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; @@ -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; } /**