UserLoginEvent.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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\User\User;
  10. use RocketTheme\Toolbox\Event\Event;
  11. /**
  12. * Class UserLoginEvent
  13. * @package Grav\Common\User\Events
  14. *
  15. * @property int $status
  16. * @property array $credentials
  17. * @property string|string[] $authorize
  18. * @property array $options
  19. * @property User $user
  20. * @property string $message
  21. *
  22. */
  23. class UserLoginEvent extends Event
  24. {
  25. /**
  26. * Undefined event state.
  27. */
  28. const AUTHENTICATION_UNDEFINED = 0;
  29. /**
  30. * onUserAuthenticate success.
  31. */
  32. const AUTHENTICATION_SUCCESS = 1;
  33. /**
  34. * onUserAuthenticate fails on bad username/password.
  35. */
  36. const AUTHENTICATION_FAILURE = 2;
  37. /**
  38. * onUserAuthenticate fails on auth cancellation.
  39. */
  40. const AUTHENTICATION_CANCELLED = 4;
  41. /**
  42. * onUserAuthorizeLogin fails on expired account.
  43. */
  44. const AUTHORIZATION_EXPIRED = 8;
  45. /**
  46. * onUserAuthorizeLogin is delayed until user has performed extra action(s).
  47. */
  48. const AUTHORIZATION_DELAYED = 16;
  49. /**
  50. * onUserAuthorizeLogin fails for other reasons.
  51. */
  52. const AUTHORIZATION_DENIED = 32;
  53. /**
  54. * UserLoginEvent constructor.
  55. * @param array $items
  56. */
  57. public function __construct(array $items = [])
  58. {
  59. $items += [
  60. 'credentials' =>
  61. (isset($items['credentials']) ? (array)$items['credentials'] : []) + ['username' => '', 'password' => ''],
  62. 'options' => [],
  63. 'authorize' => 'site.login',
  64. 'status' => static::AUTHENTICATION_UNDEFINED,
  65. 'user' => null,
  66. 'message' => ''
  67. ];
  68. parent::__construct($items);
  69. if (!$this->offsetExists('user')) {
  70. $this->offsetSet('user', User::load($this['credentials']['username']));
  71. }
  72. }
  73. public function isSuccess()
  74. {
  75. $status = $this->offsetGet('status');
  76. $failure = static::AUTHENTICATION_FAILURE | static::AUTHENTICATION_CANCELLED | static::AUTHORIZATION_EXPIRED
  77. | static::AUTHORIZATION_DENIED;
  78. return ($status & static::AUTHENTICATION_SUCCESS) && !($status & $failure);
  79. }
  80. public function isDelayed()
  81. {
  82. return $this->isSuccess() && ($this->offsetGet('status') & static::AUTHORIZATION_DELAYED);
  83. }
  84. /**
  85. * @return int
  86. */
  87. public function getStatus()
  88. {
  89. return (int)$this->offsetGet('status');
  90. }
  91. /**
  92. * @param int $status
  93. * @return $this
  94. */
  95. public function setStatus($status)
  96. {
  97. $this->offsetSet('status', $this->offsetGet('status') | (int)$status);
  98. return $this;
  99. }
  100. /**
  101. * @return array
  102. */
  103. public function getCredentials()
  104. {
  105. return $this->offsetGet('credentials') + ['username' => '', 'password' => ''];
  106. }
  107. /**
  108. * @param string $name
  109. * @return mixed
  110. */
  111. public function getCredential($name)
  112. {
  113. return isset($this->items['credentials'][$name]) ? $this->items['credentials'][$name] : null;
  114. }
  115. /**
  116. * @param string $name
  117. * @param mixed $value
  118. * @return $this
  119. */
  120. public function setCredential($name, $value)
  121. {
  122. $this->items['credentials'][$name] = $value;
  123. return $this;
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function getOptions()
  129. {
  130. return $this->offsetGet('options');
  131. }
  132. /**
  133. * @param string $name
  134. * @return mixed
  135. */
  136. public function getOption($name)
  137. {
  138. return isset($this->items['options'][$name]) ? $this->items['options'][$name] : null;
  139. }
  140. /**
  141. * @param string $name
  142. * @param mixed $value
  143. * @return $this
  144. */
  145. public function setOption($name, $value)
  146. {
  147. $this->items['options'][$name] = $value;
  148. return $this;
  149. }
  150. /**
  151. * @return User
  152. */
  153. public function getUser()
  154. {
  155. return $this->offsetGet('user');
  156. }
  157. /**
  158. * @param User $user
  159. * @return $this
  160. */
  161. public function setUser(User $user)
  162. {
  163. $this->offsetSet('user', $user);
  164. return $this;
  165. }
  166. /**
  167. * @return array
  168. */
  169. public function getAuthorize()
  170. {
  171. return (array)$this->offsetGet('authorize');
  172. }
  173. /**
  174. * @param string $message
  175. * @return $this
  176. */
  177. public function setMessage($message)
  178. {
  179. $this->items['message'] = (string)$message;
  180. return $this;
  181. }
  182. /**
  183. * Magic setter method
  184. *
  185. * @param mixed $offset Asset name value
  186. * @param mixed $value Asset value
  187. */
  188. public function __set($offset, $value)
  189. {
  190. $this->offsetSet($offset, $value);
  191. }
  192. /**
  193. * Magic getter method
  194. *
  195. * @param mixed $offset Asset name value
  196. * @return mixed Asset value
  197. */
  198. public function __get($offset)
  199. {
  200. return $this->offsetGet($offset);
  201. }
  202. /**
  203. * Magic method to determine if the attribute is set
  204. *
  205. * @param mixed $offset Asset name value
  206. * @return boolean True if the value is set
  207. */
  208. public function __isset($offset)
  209. {
  210. return $this->offsetExists($offset);
  211. }
  212. /**
  213. * Magic method to unset the attribute
  214. *
  215. * @param mixed $offset The name value to unset
  216. */
  217. public function __unset($offset)
  218. {
  219. $this->offsetUnset($offset);
  220. }
  221. }