UserLoginEvent.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2017 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\Common\User\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. $this->offsetSet('user', $user);
  90. if (Login::DEBUG) {
  91. if ($user->exists()) {
  92. Login::addDebugMessage('Login user:', $user);
  93. } else {
  94. Login::addDebugMessage("Login: user '{$this['credentials']['username']}' not found");
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * @return bool
  101. */
  102. public function isSuccess(): bool
  103. {
  104. $status = $this->offsetGet('status');
  105. $failure = static::AUTHENTICATION_FAILURE | static::AUTHENTICATION_CANCELLED | static::AUTHORIZATION_EXPIRED
  106. | static::AUTHORIZATION_DENIED;
  107. return ($status & static::AUTHENTICATION_SUCCESS) && !($status & $failure);
  108. }
  109. /**
  110. * @return bool
  111. */
  112. public function isDelayed(): bool
  113. {
  114. return $this->isSuccess() && ($this->offsetGet('status') & static::AUTHORIZATION_DELAYED);
  115. }
  116. /**
  117. * @return bool
  118. */
  119. public function isChallenged(): bool
  120. {
  121. $status = $this->offsetGet('status');
  122. return (bool)($status & static::AUTHORIZATION_CHALLENGE);
  123. }
  124. /**
  125. * @return int
  126. */
  127. public function getStatus(): int
  128. {
  129. return (int)$this->offsetGet('status');
  130. }
  131. /**
  132. * @param int $status
  133. * @return $this
  134. */
  135. public function setStatus($status): self
  136. {
  137. $this->offsetSet('status', $this->offsetGet('status') | (int)$status);
  138. return $this;
  139. }
  140. /**
  141. * @return array
  142. */
  143. public function getCredentials(): array
  144. {
  145. return $this->offsetGet('credentials') + ['username' => '', 'password' => ''];
  146. }
  147. /**
  148. * @param string $name
  149. * @return mixed
  150. */
  151. public function getCredential($name)
  152. {
  153. return $this->items['credentials'][$name] ?? null;
  154. }
  155. /**
  156. * @param string $name
  157. * @param mixed $value
  158. * @return $this
  159. */
  160. public function setCredential($name, $value): self
  161. {
  162. $this->items['credentials'][$name] = $value;
  163. return $this;
  164. }
  165. /**
  166. * @return array
  167. */
  168. public function getOptions(): array
  169. {
  170. return $this->offsetGet('options');
  171. }
  172. /**
  173. * @param string $name
  174. * @return mixed
  175. */
  176. public function getOption($name)
  177. {
  178. return $this->items['options'][$name] ?? null;
  179. }
  180. /**
  181. * @param string $name
  182. * @param mixed $value
  183. * @return $this
  184. */
  185. public function setOption($name, $value): self
  186. {
  187. $this->items['options'][$name] = $value;
  188. return $this;
  189. }
  190. /**
  191. * @return SessionInterface|Session|null
  192. */
  193. public function getSession(): ?SessionInterface
  194. {
  195. return $this->offsetGet('session');
  196. }
  197. /**
  198. * @return UserInterface
  199. */
  200. public function getUser(): UserInterface
  201. {
  202. return $this->offsetGet('user');
  203. }
  204. /**
  205. * @param UserInterface $user
  206. * @return $this
  207. */
  208. public function setUser(UserInterface $user): self
  209. {
  210. $this->offsetSet('user', $user);
  211. return $this;
  212. }
  213. /**
  214. * @return array
  215. */
  216. public function getAuthorize(): array
  217. {
  218. return (array)$this->offsetGet('authorize');
  219. }
  220. /**
  221. * @return string|null
  222. */
  223. public function getMessage(): ?string
  224. {
  225. return !empty($this->items['message'][0]) ? (string)$this->items['message'][0] : null;
  226. }
  227. /**
  228. * @return string
  229. */
  230. public function getMessageType(): string
  231. {
  232. return !empty($this->items['message'][1]) ? (string)$this->items['message'][1] : 'info';
  233. }
  234. /**
  235. * @param string $message
  236. * @param string|null $type
  237. * @return $this
  238. */
  239. public function setMessage($message, $type = null): self
  240. {
  241. $this->items['message'] = $message ? [$message, $type] : null;
  242. return $this;
  243. }
  244. /**
  245. * @param string $message
  246. * @param string|null $type
  247. * @return $this
  248. */
  249. public function defMessage($message, $type = null): self
  250. {
  251. if ($message && !isset($this->items['message'])) {
  252. $this->setMessage($message, $type);
  253. }
  254. return $this;
  255. }
  256. /**
  257. * @return string|null
  258. */
  259. public function getRedirect(): ?string
  260. {
  261. return $this->items['redirect'] ?? null;
  262. }
  263. /**
  264. * @return int
  265. */
  266. public function getRedirectCode(): int
  267. {
  268. return (int)($this->items['redirect_code'] ?? 303);
  269. }
  270. /**
  271. * @param string $path
  272. * @param int $code
  273. * @return $this
  274. */
  275. public function setRedirect($path, $code = 303): self
  276. {
  277. $this->items['redirect'] = $path ?: null;
  278. $this->items['redirect_code'] = (int)$code;
  279. return $this;
  280. }
  281. /**
  282. * @param string $path
  283. * @param int $code
  284. * @return $this
  285. */
  286. public function defRedirect($path, $code = 303): self
  287. {
  288. if ($path && !isset($this->items['redirect'])) {
  289. $this->setRedirect($path, $code);
  290. }
  291. return $this;
  292. }
  293. /**
  294. * Magic setter method
  295. *
  296. * @param mixed $offset Asset name value
  297. * @param mixed $value Asset value
  298. */
  299. public function __set($offset, $value): void
  300. {
  301. $this->offsetSet($offset, $value);
  302. }
  303. /**
  304. * Magic getter method
  305. *
  306. * @param mixed $offset Asset name value
  307. * @return mixed Asset value
  308. */
  309. public function __get($offset)
  310. {
  311. return $this->offsetGet($offset);
  312. }
  313. /**
  314. * Magic method to determine if the attribute is set
  315. *
  316. * @param mixed $offset Asset name value
  317. * @return boolean True if the value is set
  318. */
  319. public function __isset($offset): bool
  320. {
  321. return $this->offsetExists($offset);
  322. }
  323. /**
  324. * Magic method to unset the attribute
  325. *
  326. * @param mixed $offset The name value to unset
  327. */
  328. public function __unset($offset): void
  329. {
  330. $this->offsetUnset($offset);
  331. }
  332. /**
  333. * @return array
  334. */
  335. public function __debugInfo(): array
  336. {
  337. return get_object_vars($this);
  338. }
  339. }