UserLoginEvent.php 7.6 KB

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