UserLoginEvent.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2022 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Login\Events;
  9. use Grav\Common\Grav;
  10. use Grav\Common\Session;
  11. use Grav\Common\User\Interfaces\UserCollectionInterface;
  12. use Grav\Common\User\Interfaces\UserInterface;
  13. use Grav\Framework\Session\SessionInterface;
  14. use Grav\Plugin\Login\Login;
  15. use RocketTheme\Toolbox\Event\Event;
  16. /**
  17. * Class UserLoginEvent
  18. * @package Grav\Plugin\Login\Events
  19. *
  20. * @property int $status
  21. * @property array $credentials
  22. * @property string|string[] $authorize
  23. * @property array $options
  24. * @property Session $session
  25. * @property UserInterface $user
  26. * @property string $message
  27. *
  28. */
  29. class UserLoginEvent extends Event
  30. {
  31. /**
  32. * Undefined event state.
  33. */
  34. public const AUTHENTICATION_UNDEFINED = 0;
  35. /**
  36. * onUserAuthenticate success.
  37. */
  38. public const AUTHENTICATION_SUCCESS = 1;
  39. /**
  40. * onUserAuthenticate fails on bad username/password.
  41. */
  42. public const AUTHENTICATION_FAILURE = 2;
  43. /**
  44. * onUserAuthenticate fails on auth cancellation.
  45. */
  46. public const AUTHENTICATION_CANCELLED = 4;
  47. /**
  48. * onUserAuthorizeLogin fails on expired account.
  49. */
  50. public const AUTHORIZATION_EXPIRED = 8;
  51. /**
  52. * onUserAuthorizeLogin is delayed until user has performed AUTHORIZATION_CHALLENGE.
  53. */
  54. public const AUTHORIZATION_DELAYED = 16;
  55. /**
  56. * onUserAuthorizeLogin fails for other reasons.
  57. */
  58. public const AUTHORIZATION_DENIED = 32;
  59. /**
  60. * onUserAuthorizeLogin was challenged, combine with AUTHENTICATION_SUCCESS, AUTHENTICATION_FAILURE or AUTHENTICATION_CANCELLED.
  61. */
  62. public const AUTHORIZATION_CHALLENGE = 64;
  63. /**
  64. * UserLoginEvent constructor.
  65. * @param array $items
  66. */
  67. public function __construct(array $items = [])
  68. {
  69. $items += [
  70. 'credentials' => [],
  71. 'options' => [],
  72. 'authorize' => 'site.login',
  73. 'status' => static::AUTHENTICATION_UNDEFINED,
  74. 'session' => null,
  75. 'user' => null,
  76. 'message' => null,
  77. 'redirect' => null,
  78. 'redirect_code' => 303
  79. ];
  80. $items['credentials'] += ['username' => '', 'password' => ''];
  81. parent::__construct($items);
  82. if (!$this->offsetExists('session') && isset(Grav::instance()['session'])) {
  83. $this->offsetSet('session', Grav::instance()['session']);
  84. }
  85. if (!$this->offsetExists('user')) {
  86. /** @var UserCollectionInterface $users */
  87. $users = Grav::instance()['accounts'];
  88. $user = $users->load($this['credentials']['username']);
  89. if (is_callable([$user, 'refresh'])) {
  90. $user->refresh(true);
  91. }
  92. $this->offsetSet('user', $user);
  93. if (Login::DEBUG) {
  94. if ($user->exists()) {
  95. Login::addDebugMessage('Login user:', $user);
  96. } else {
  97. Login::addDebugMessage("Login: user '{$this['credentials']['username']}' not found");
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * @return bool
  104. */
  105. public function isSuccess(): bool
  106. {
  107. $status = $this->offsetGet('status');
  108. $failure = static::AUTHENTICATION_FAILURE | static::AUTHENTICATION_CANCELLED | static::AUTHORIZATION_EXPIRED
  109. | static::AUTHORIZATION_DENIED;
  110. return ($status & static::AUTHENTICATION_SUCCESS) && !($status & $failure);
  111. }
  112. /**
  113. * @return bool
  114. */
  115. public function isDelayed(): bool
  116. {
  117. return $this->isSuccess() && ($this->offsetGet('status') & static::AUTHORIZATION_DELAYED);
  118. }
  119. /**
  120. * @return bool
  121. */
  122. public function isChallenged(): bool
  123. {
  124. $status = $this->offsetGet('status');
  125. return (bool)($status & static::AUTHORIZATION_CHALLENGE);
  126. }
  127. /**
  128. * @return int
  129. */
  130. public function getStatus(): int
  131. {
  132. return (int)$this->offsetGet('status');
  133. }
  134. /**
  135. * @param int $status
  136. * @return $this
  137. */
  138. public function setStatus($status): self
  139. {
  140. $this->offsetSet('status', $this->offsetGet('status') | (int)$status);
  141. return $this;
  142. }
  143. /**
  144. * @return array
  145. */
  146. public function getCredentials(): array
  147. {
  148. return $this->offsetGet('credentials') + ['username' => '', 'password' => ''];
  149. }
  150. /**
  151. * @param string $name
  152. * @return mixed
  153. */
  154. public function getCredential($name)
  155. {
  156. return $this->items['credentials'][$name] ?? null;
  157. }
  158. /**
  159. * @param string $name
  160. * @param mixed $value
  161. * @return $this
  162. */
  163. public function setCredential($name, $value): self
  164. {
  165. $this->items['credentials'][$name] = $value;
  166. return $this;
  167. }
  168. /**
  169. * @return array
  170. */
  171. public function getOptions(): array
  172. {
  173. return $this->offsetGet('options');
  174. }
  175. /**
  176. * @param string $name
  177. * @return mixed
  178. */
  179. public function getOption($name)
  180. {
  181. return $this->items['options'][$name] ?? null;
  182. }
  183. /**
  184. * @param string $name
  185. * @param mixed $value
  186. * @return $this
  187. */
  188. public function setOption($name, $value): self
  189. {
  190. $this->items['options'][$name] = $value;
  191. return $this;
  192. }
  193. /**
  194. * @return SessionInterface|Session|null
  195. */
  196. public function getSession(): ?SessionInterface
  197. {
  198. return $this->offsetGet('session');
  199. }
  200. /**
  201. * @return UserInterface
  202. */
  203. public function getUser(): UserInterface
  204. {
  205. return $this->offsetGet('user');
  206. }
  207. /**
  208. * @param UserInterface $user
  209. * @return $this
  210. */
  211. public function setUser(UserInterface $user): self
  212. {
  213. $this->offsetSet('user', $user);
  214. return $this;
  215. }
  216. /**
  217. * @return array
  218. */
  219. public function getAuthorize(): array
  220. {
  221. return (array)$this->offsetGet('authorize');
  222. }
  223. /**
  224. * @return string|null
  225. */
  226. public function getMessage(): ?string
  227. {
  228. return !empty($this->items['message'][0]) ? (string)$this->items['message'][0] : null;
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getMessageType(): string
  234. {
  235. return !empty($this->items['message'][1]) ? (string)$this->items['message'][1] : 'info';
  236. }
  237. /**
  238. * @param string $message
  239. * @param string|null $type
  240. * @return $this
  241. */
  242. public function setMessage($message, $type = null): self
  243. {
  244. $this->items['message'] = $message ? [$message, $type] : null;
  245. return $this;
  246. }
  247. /**
  248. * @param string $message
  249. * @param string|null $type
  250. * @return $this
  251. */
  252. public function defMessage($message, $type = null): self
  253. {
  254. if ($message && !isset($this->items['message'])) {
  255. $this->setMessage($message, $type);
  256. }
  257. return $this;
  258. }
  259. /**
  260. * @return string|null
  261. */
  262. public function getRedirect(): ?string
  263. {
  264. return $this->items['redirect'] ?? null;
  265. }
  266. /**
  267. * @return int
  268. */
  269. public function getRedirectCode(): int
  270. {
  271. return (int)($this->items['redirect_code'] ?? 303);
  272. }
  273. /**
  274. * @param string $path
  275. * @param int $code
  276. * @return $this
  277. */
  278. public function setRedirect($path, $code = 303): self
  279. {
  280. $this->items['redirect'] = $path ?: null;
  281. $this->items['redirect_code'] = (int)$code;
  282. return $this;
  283. }
  284. /**
  285. * @param string $path
  286. * @param int $code
  287. * @return $this
  288. */
  289. public function defRedirect($path, $code = 303): self
  290. {
  291. if ($path && !isset($this->items['redirect'])) {
  292. $this->setRedirect($path, $code);
  293. }
  294. return $this;
  295. }
  296. /**
  297. * Magic setter method
  298. *
  299. * @param mixed $offset Asset name value
  300. * @param mixed $value Asset value
  301. */
  302. public function __set($offset, $value): void
  303. {
  304. $this->offsetSet($offset, $value);
  305. }
  306. /**
  307. * Magic getter method
  308. *
  309. * @param mixed $offset Asset name value
  310. * @return mixed Asset value
  311. */
  312. public function __get($offset)
  313. {
  314. return $this->offsetGet($offset);
  315. }
  316. /**
  317. * Magic method to determine if the attribute is set
  318. *
  319. * @param mixed $offset Asset name value
  320. * @return boolean True if the value is set
  321. */
  322. public function __isset($offset): bool
  323. {
  324. return $this->offsetExists($offset);
  325. }
  326. /**
  327. * Magic method to unset the attribute
  328. *
  329. * @param mixed $offset The name value to unset
  330. */
  331. public function __unset($offset): void
  332. {
  333. $this->offsetUnset($offset);
  334. }
  335. /**
  336. * @return array
  337. */
  338. public function __debugInfo(): array
  339. {
  340. return get_object_vars($this);
  341. }
  342. }