123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- /**
- * @package Grav\Plugin\Login
- *
- * @copyright Copyright (C) 2014 - 2017 RocketTheme, LLC. All rights reserved.
- * @license MIT License; see LICENSE file for details.
- */
- namespace Grav\Plugin\Login\Events;
- use Grav\Common\Grav;
- use Grav\Common\Session;
- use Grav\Common\User\Interfaces\UserCollectionInterface;
- use Grav\Common\User\Interfaces\UserInterface;
- use Grav\Framework\Session\SessionInterface;
- use Grav\Plugin\Login\Login;
- use RocketTheme\Toolbox\Event\Event;
- /**
- * Class UserLoginEvent
- * @package Grav\Common\User\Events
- *
- * @property int $status
- * @property array $credentials
- * @property string|string[] $authorize
- * @property array $options
- * @property Session $session
- * @property UserInterface $user
- * @property string $message
- *
- */
- class UserLoginEvent extends Event
- {
- /**
- * Undefined event state.
- */
- public const AUTHENTICATION_UNDEFINED = 0;
- /**
- * onUserAuthenticate success.
- */
- public const AUTHENTICATION_SUCCESS = 1;
- /**
- * onUserAuthenticate fails on bad username/password.
- */
- public const AUTHENTICATION_FAILURE = 2;
- /**
- * onUserAuthenticate fails on auth cancellation.
- */
- public const AUTHENTICATION_CANCELLED = 4;
- /**
- * onUserAuthorizeLogin fails on expired account.
- */
- public const AUTHORIZATION_EXPIRED = 8;
- /**
- * onUserAuthorizeLogin is delayed until user has performed AUTHORIZATION_CHALLENGE.
- */
- public const AUTHORIZATION_DELAYED = 16;
- /**
- * onUserAuthorizeLogin fails for other reasons.
- */
- public const AUTHORIZATION_DENIED = 32;
- /**
- * onUserAuthorizeLogin was challenged, combine with AUTHENTICATION_SUCCESS, AUTHENTICATION_FAILURE or AUTHENTICATION_CANCELLED.
- */
- public const AUTHORIZATION_CHALLENGE = 64;
- /**
- * UserLoginEvent constructor.
- * @param array $items
- */
- public function __construct(array $items = [])
- {
- $items += [
- 'credentials' => [],
- 'options' => [],
- 'authorize' => 'site.login',
- 'status' => static::AUTHENTICATION_UNDEFINED,
- 'session' => null,
- 'user' => null,
- 'message' => null,
- 'redirect' => null,
- 'redirect_code' => 303
- ];
- $items['credentials'] += ['username' => '', 'password' => ''];
- parent::__construct($items);
- if (!$this->offsetExists('session') && isset(Grav::instance()['session'])) {
- $this->offsetSet('session', Grav::instance()['session']);
- }
- if (!$this->offsetExists('user')) {
- /** @var UserCollectionInterface $users */
- $users = Grav::instance()['accounts'];
- $user = $users->load($this['credentials']['username']);
- $this->offsetSet('user', $user);
- if (Login::DEBUG) {
- if ($user->exists()) {
- Login::addDebugMessage('Login user:', $user);
- } else {
- Login::addDebugMessage("Login: user '{$this['credentials']['username']}' not found");
- }
- }
- }
- }
- /**
- * @return bool
- */
- public function isSuccess(): bool
- {
- $status = $this->offsetGet('status');
- $failure = static::AUTHENTICATION_FAILURE | static::AUTHENTICATION_CANCELLED | static::AUTHORIZATION_EXPIRED
- | static::AUTHORIZATION_DENIED;
- return ($status & static::AUTHENTICATION_SUCCESS) && !($status & $failure);
- }
- /**
- * @return bool
- */
- public function isDelayed(): bool
- {
- return $this->isSuccess() && ($this->offsetGet('status') & static::AUTHORIZATION_DELAYED);
- }
- /**
- * @return bool
- */
- public function isChallenged(): bool
- {
- $status = $this->offsetGet('status');
- return (bool)($status & static::AUTHORIZATION_CHALLENGE);
- }
- /**
- * @return int
- */
- public function getStatus(): int
- {
- return (int)$this->offsetGet('status');
- }
- /**
- * @param int $status
- * @return $this
- */
- public function setStatus($status): self
- {
- $this->offsetSet('status', $this->offsetGet('status') | (int)$status);
- return $this;
- }
- /**
- * @return array
- */
- public function getCredentials(): array
- {
- return $this->offsetGet('credentials') + ['username' => '', 'password' => ''];
- }
- /**
- * @param string $name
- * @return mixed
- */
- public function getCredential($name)
- {
- return $this->items['credentials'][$name] ?? null;
- }
- /**
- * @param string $name
- * @param mixed $value
- * @return $this
- */
- public function setCredential($name, $value): self
- {
- $this->items['credentials'][$name] = $value;
- return $this;
- }
- /**
- * @return array
- */
- public function getOptions(): array
- {
- return $this->offsetGet('options');
- }
- /**
- * @param string $name
- * @return mixed
- */
- public function getOption($name)
- {
- return $this->items['options'][$name] ?? null;
- }
- /**
- * @param string $name
- * @param mixed $value
- * @return $this
- */
- public function setOption($name, $value): self
- {
- $this->items['options'][$name] = $value;
- return $this;
- }
- /**
- * @return SessionInterface|Session|null
- */
- public function getSession(): ?SessionInterface
- {
- return $this->offsetGet('session');
- }
- /**
- * @return UserInterface
- */
- public function getUser(): UserInterface
- {
- return $this->offsetGet('user');
- }
- /**
- * @param UserInterface $user
- * @return $this
- */
- public function setUser(UserInterface $user): self
- {
- $this->offsetSet('user', $user);
- return $this;
- }
- /**
- * @return array
- */
- public function getAuthorize(): array
- {
- return (array)$this->offsetGet('authorize');
- }
- /**
- * @return string|null
- */
- public function getMessage(): ?string
- {
- return !empty($this->items['message'][0]) ? (string)$this->items['message'][0] : null;
- }
- /**
- * @return string
- */
- public function getMessageType(): string
- {
- return !empty($this->items['message'][1]) ? (string)$this->items['message'][1] : 'info';
- }
- /**
- * @param string $message
- * @param string|null $type
- * @return $this
- */
- public function setMessage($message, $type = null): self
- {
- $this->items['message'] = $message ? [$message, $type] : null;
- return $this;
- }
- /**
- * @param string $message
- * @param string|null $type
- * @return $this
- */
- public function defMessage($message, $type = null): self
- {
- if ($message && !isset($this->items['message'])) {
- $this->setMessage($message, $type);
- }
- return $this;
- }
- /**
- * @return string|null
- */
- public function getRedirect(): ?string
- {
- return $this->items['redirect'] ?? null;
- }
- /**
- * @return int
- */
- public function getRedirectCode(): int
- {
- return (int)($this->items['redirect_code'] ?? 303);
- }
- /**
- * @param string $path
- * @param int $code
- * @return $this
- */
- public function setRedirect($path, $code = 303): self
- {
- $this->items['redirect'] = $path ?: null;
- $this->items['redirect_code'] = (int)$code;
- return $this;
- }
- /**
- * @param string $path
- * @param int $code
- * @return $this
- */
- public function defRedirect($path, $code = 303): self
- {
- if ($path && !isset($this->items['redirect'])) {
- $this->setRedirect($path, $code);
- }
- return $this;
- }
- /**
- * Magic setter method
- *
- * @param mixed $offset Asset name value
- * @param mixed $value Asset value
- */
- public function __set($offset, $value): void
- {
- $this->offsetSet($offset, $value);
- }
- /**
- * Magic getter method
- *
- * @param mixed $offset Asset name value
- * @return mixed Asset value
- */
- public function __get($offset)
- {
- return $this->offsetGet($offset);
- }
- /**
- * Magic method to determine if the attribute is set
- *
- * @param mixed $offset Asset name value
- * @return boolean True if the value is set
- */
- public function __isset($offset): bool
- {
- return $this->offsetExists($offset);
- }
- /**
- * Magic method to unset the attribute
- *
- * @param mixed $offset The name value to unset
- */
- public function __unset($offset): void
- {
- $this->offsetUnset($offset);
- }
- /**
- * @return array
- */
- public function __debugInfo(): array
- {
- return get_object_vars($this);
- }
- }
|