Controller.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2021 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Login;
  9. use Grav\Common\Config\Config;
  10. use Grav\Common\Grav;
  11. use Grav\Common\Language\Language;
  12. use Grav\Common\Uri;
  13. use Grav\Common\User\Interfaces\UserCollectionInterface;
  14. use Grav\Common\User\Interfaces\UserInterface;
  15. use Grav\Common\Utils;
  16. use Grav\Plugin\Email\Utils as EmailUtils;
  17. use Grav\Plugin\Form\Form;
  18. use Grav\Plugin\Form\Forms;
  19. use Grav\Plugin\Login\Events\UserLoginEvent;
  20. use Grav\Plugin\Login\Invitations\Invitation;
  21. use Grav\Plugin\Login\Invitations\Invitations;
  22. use Grav\Plugin\Login\TwoFactorAuth\TwoFactorAuth;
  23. use Grav\Plugin\LoginPlugin;
  24. use RocketTheme\Toolbox\Event\Event;
  25. use RocketTheme\Toolbox\Session\Message;
  26. /**
  27. * Class Controller
  28. * @package Grav\Plugin\Login
  29. */
  30. class Controller
  31. {
  32. /**
  33. * @var Grav
  34. */
  35. public $grav;
  36. /**
  37. * @var string
  38. */
  39. public $action;
  40. /**
  41. * @var array
  42. */
  43. public $post;
  44. /**
  45. * @var string
  46. */
  47. protected $redirect;
  48. /**
  49. * @var int
  50. */
  51. protected $redirectCode;
  52. /**
  53. * @var string
  54. */
  55. protected $prefix = 'task';
  56. /**
  57. * @var Login
  58. */
  59. protected $login;
  60. /**
  61. * @param Grav $grav
  62. * @param string $action
  63. * @param array $post
  64. */
  65. public function __construct(Grav $grav, $action, $post = null)
  66. {
  67. $this->grav = $grav;
  68. $this->action = $action;
  69. $this->login = $this->grav['login'];
  70. $this->post = $post ? $this->getPost($post) : [];
  71. }
  72. /**
  73. * Performs an action.
  74. * @throws \RuntimeException
  75. */
  76. public function execute()
  77. {
  78. $messages = $this->grav['messages'];
  79. // Set redirect if available.
  80. $redirect = $this->post['_redirect'] ?? null;
  81. unset($this->post['_redirect']);
  82. $success = false;
  83. $method = $this->prefix . ucfirst($this->action);
  84. if (!method_exists($this, $method)) {
  85. throw new \RuntimeException('Page Not Found', 404);
  86. }
  87. try {
  88. $success = $this->{$method}();
  89. } catch (\RuntimeException $e) {
  90. $messages->add($e->getMessage(), 'error');
  91. $this->grav['log']->error('plugin.login: '. $e->getMessage());
  92. }
  93. if (!$this->redirect && $redirect) {
  94. $this->setRedirect($redirect, 303);
  95. }
  96. return $success;
  97. }
  98. /**
  99. * Handle login.
  100. *
  101. * @return bool True if the action was performed.
  102. */
  103. public function taskLogin()
  104. {
  105. /** @var Language $t */
  106. $t = $this->grav['language'];
  107. /** @var Message $messages */
  108. $messages = $this->grav['messages'];
  109. // Remove login nonce from the form.
  110. $form = array_diff_key($this->post, ['login-form-nonce' => true]);
  111. // Is twofa enabled?
  112. $twofa = $this->grav['config']->get('plugins.login.twofa_enabled', false);
  113. // Fire Login process.
  114. $event = $this->login->login($form, ['rate_limit' => true, 'remember_me' => true, 'twofa' => $twofa], ['return_event' => true]);
  115. $user = $event->getUser();
  116. $login_redirect = $this->login->getRoute('after_login');
  117. if ($user->authenticated) {
  118. if ($user->authorized) {
  119. $event->defMessage('PLUGIN_LOGIN.LOGIN_SUCCESSFUL', 'info');
  120. $event->defRedirect(
  121. $this->grav['session']->redirect_after_login ?:
  122. $login_redirect ?: $this->grav['uri']->referrer('/', '', true)
  123. );
  124. } else {
  125. $redirect_to_login = $this->grav['config']->get('plugins.login.redirect_to_login');
  126. $redirect_route = $redirect_to_login ? $this->login->getRoute('login') : null;
  127. $event->defRedirect($redirect_route ?? $this->grav['uri']->referrer('/', '', true));
  128. }
  129. } else {
  130. if ($user->authorized) {
  131. $event->defMessage('PLUGIN_LOGIN.ACCESS_DENIED', 'error');
  132. $event->defRedirect($this->login->getRoute('unauthorized') ?? '/');
  133. } else {
  134. $event->defMessage('PLUGIN_LOGIN.LOGIN_FAILED', 'error');
  135. }
  136. }
  137. $message = $event->getMessage();
  138. if ($message) {
  139. $messages->add($t->translate($message), $event->getMessageType());
  140. }
  141. $redirect = $event->getRedirect();
  142. if ($redirect) {
  143. $this->setRedirect($redirect, $event->getRedirectCode());
  144. }
  145. return true;
  146. }
  147. public function taskTwoFa()
  148. {
  149. /** @var Config $config */
  150. $config = $this->grav['config'];
  151. /** @var Language $t */
  152. $t = $this->grav['language'];
  153. /** @var Message $messages */
  154. $messages = $this->grav['messages'];
  155. if (!$config->get('plugins.login.twofa_enabled', false)) {
  156. $messages->add($t->translate('PLUGIN_LOGIN.2FA_FAILED'), 'error');
  157. return true;
  158. }
  159. $twoFa = $this->login->twoFactorAuth();
  160. $user = $this->grav['user'];
  161. $code = $this->post['2fa_code'] ?? null;
  162. $secret = $user->twofa_secret ?? null;
  163. $eventOptions = [
  164. 'credentials' => ['username' => $user->get('username')],
  165. 'options' => ['twofa' => true]
  166. ];
  167. // Attempt to authenticate the user.
  168. $event = new UserLoginEvent($eventOptions);
  169. $event->setUser($user);
  170. if (!$code || !$secret || !$twoFa->verifyCode($secret, $code)) {
  171. $event->setStatus(UserLoginEvent::AUTHENTICATION_FAILURE | UserLoginEvent::AUTHORIZATION_CHALLENGE);
  172. $event->setMessage($t->translate('PLUGIN_LOGIN.2FA_FAILED'), 'error');
  173. $this->grav->fireEvent('onUserLoginFailure', $event);
  174. // Make sure that event didn't mess up with the user authorization.
  175. $user = $event->getUser();
  176. $user->authenticated = false;
  177. $user->authorized = false;
  178. if (!$event->getRedirect()) {
  179. $redirect_to_login = $this->grav['config']->get('plugins.login.route_to_login');
  180. $login_route = $this->login->getRoute('login');
  181. $event->setRedirect(
  182. $redirect_to_login && $login_route ? $login_route : $this->getCurrentRedirect(),
  183. 303
  184. );
  185. }
  186. } else {
  187. $event->setStatus(UserLoginEvent::AUTHENTICATION_SUCCESS | UserLoginEvent::AUTHORIZATION_CHALLENGE);
  188. $event->setMessage($t->translate('PLUGIN_LOGIN.LOGIN_SUCCESSFUL'), 'info');
  189. $this->grav->fireEvent('onUserLoginAuthorized', $event);
  190. // Make sure that event didn't mess up with the user authorization.
  191. $user = $event->getUser();
  192. $user->authenticated = $event->isSuccess();
  193. $user->authorized = !$event->isDelayed();
  194. if (!$event->getRedirect()) {
  195. $login_redirect = $this->login->getRoute('after_login');
  196. $event->setRedirect(
  197. $this->grav['session']->redirect_after_login ?: $login_redirect ?: $this->grav['uri']->referrer('/', '', true),
  198. 303
  199. );
  200. }
  201. }
  202. /** @var Message $messages */
  203. $messages = $this->grav['messages'];
  204. $messages->add($event->getMessage(), $event->getMessageType());
  205. $redirect = $event->getRedirect() ?: $this->getCurrentRedirect();
  206. $this->setRedirect($redirect, $event->getRedirectCode());
  207. return true;
  208. }
  209. public function taskTwofa_cancel()
  210. {
  211. /** @var Config $config */
  212. $config = $this->grav['config'];
  213. /** @var Language $t */
  214. $t = $this->grav['language'];
  215. /** @var Message $messages */
  216. $messages = $this->grav['messages'];
  217. if (!$config->get('plugins.login.twofa_enabled', false)) {
  218. $messages->add($t->translate('PLUGIN_LOGIN.2FA_FAILED'), 'error');
  219. return true;
  220. }
  221. $user = $this->grav['user'];
  222. $eventOptions = [
  223. 'credentials' => ['username' => $user->get('username')],
  224. 'options' => ['twofa' => true]
  225. ];
  226. $event = new UserLoginEvent($eventOptions);
  227. $event->setStatus(UserLoginEvent::AUTHENTICATION_CANCELLED | UserLoginEvent::AUTHORIZATION_CHALLENGE);
  228. $event->setMessage($t->translate('PLUGIN_LOGIN.2FA_FAILED'), 'error');
  229. $this->grav->fireEvent('onUserLoginFailure', $event);
  230. // Make sure that event didn't mess up with the user authorization.
  231. $user = $event->getUser();
  232. $user->authenticated = false;
  233. $user->authorized = false;
  234. if (!$event->getRedirect()) {
  235. $redirect_to_login = $this->grav['config']->get('plugins.login.route_to_login');
  236. $login_route = $this->login->getRoute('login');
  237. $event->setRedirect(
  238. $redirect_to_login && $login_route ? $login_route : $this->getCurrentRedirect(),
  239. 303
  240. );
  241. }
  242. return true;
  243. }
  244. /**
  245. * Handle logout.
  246. *
  247. * @return bool True if the action was performed.
  248. */
  249. public function taskLogout()
  250. {
  251. $event = $this->login->logout(['remember_me' => true], ['return_event' => true]);
  252. $message = $event->getMessage();
  253. if ($message) {
  254. /** @var Language $t */
  255. $t = $this->grav['language'];
  256. $messages = $this->grav['messages'];
  257. $messages->add($t->translate($message), $event->getMessageType());
  258. }
  259. $logout_redirect = $this->login->getRoute('after_logout');
  260. $redirect = $event->getRedirect() ?: $logout_redirect ?: $this->getCurrentRedirect();
  261. if ($redirect) {
  262. $this->setRedirect($redirect, $event->getRedirectCode());
  263. }
  264. $this->grav['session']->setFlashCookieObject(LoginPlugin::TMP_COOKIE_NAME, ['message' => $this->grav['language']->translate('PLUGIN_LOGIN.LOGGED_OUT'),
  265. 'status' => 'info']);
  266. return true;
  267. }
  268. /**
  269. * Handle the email password recovery procedure.
  270. *
  271. * @return bool True if the action was performed.
  272. */
  273. protected function taskForgot()
  274. {
  275. $param_sep = $this->grav['config']->get('system.param_sep', ':');
  276. $data = $this->post;
  277. /** @var UserCollectionInterface $users */
  278. $users = $this->grav['accounts'];
  279. $email = $data['email'] ?? '';
  280. $user = !empty($email) ? $users->find($email, ['email']) : null;
  281. /** @var Language $language */
  282. $language = $this->grav['language'];
  283. $messages = $this->grav['messages'];
  284. if (!isset($this->grav['Email'])) {
  285. $messages->add($language->translate('PLUGIN_LOGIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
  286. $this->setRedirect($this->login->getRoute('forgot') ?? '/');
  287. return true;
  288. }
  289. if (!$user || !$user->exists()) {
  290. $messages->add($language->translate('PLUGIN_LOGIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL'), 'info');
  291. $this->setRedirect($this->login->getRoute('forgot') ?? '/');
  292. return true;
  293. }
  294. if (empty($user->email)) {
  295. $messages->add($language->translate(['PLUGIN_LOGIN.FORGOT_CANNOT_RESET_EMAIL_NO_EMAIL', $email]),
  296. 'error');
  297. $this->setRedirect($this->login->getRoute('forgot') ?? '/');
  298. return true;
  299. }
  300. if (empty($user->password) && empty($user->hashed_password)) {
  301. $messages->add($language->translate(['PLUGIN_LOGIN.FORGOT_CANNOT_RESET_EMAIL_NO_PASSWORD', $email]),
  302. 'error');
  303. $this->setRedirect($this->login->getRoute('forgot') ?? '/');
  304. return true;
  305. }
  306. $from = $this->grav['config']->get('plugins.email.from');
  307. if (empty($from)) {
  308. $messages->add($language->translate('PLUGIN_LOGIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
  309. $this->setRedirect($this->login->getRoute('forgot') ?? '/');
  310. return true;
  311. }
  312. $userKey = $user->username;
  313. $rateLimiter = $this->login->getRateLimiter('pw_resets');
  314. $rateLimiter->registerRateLimitedAction($userKey);
  315. if ($rateLimiter->isRateLimited($userKey)) {
  316. $messages->add($language->translate(['PLUGIN_LOGIN.FORGOT_CANNOT_RESET_IT_IS_BLOCKED', $email, $rateLimiter->getInterval()]), 'error');
  317. $this->setRedirect($this->login->getRoute('login') ?? '/');
  318. return true;
  319. }
  320. $token = md5(uniqid(mt_rand(), true));
  321. $expire = time() + 604800; // next week
  322. $user->reset = $token . '::' . $expire;
  323. $user->save();
  324. $author = $this->grav['config']->get('site.author.name', '');
  325. $fullname = $user->fullname ?: $user->username;
  326. if ($this->grav['language']->getDefault() != $this->grav['language']->getLanguage()) {
  327. $lang = '/'.$this->grav['language']->getLanguage();
  328. } else {
  329. $lang = '';
  330. }
  331. $resetRoute = $this->login->getRoute('reset');
  332. $reset_link = $this->grav['base_url_absolute'] . $lang . $resetRoute . '/task' . $param_sep . 'login.reset/token' . $param_sep . $token . '/user' . $param_sep . $user->username . '/nonce' . $param_sep . Utils::getNonce('reset-form');
  333. $sitename = $this->grav['config']->get('site.title', 'Website');
  334. $to = $user->email;
  335. $subject = $language->translate(['PLUGIN_LOGIN.FORGOT_EMAIL_SUBJECT', $sitename]);
  336. $content = $language->translate(['PLUGIN_LOGIN.FORGOT_EMAIL_BODY', $fullname, $reset_link, $author, $sitename]);
  337. $sent = EmailUtils::sendEmail($subject, $content, $to);
  338. if ($sent < 1) {
  339. $messages->add($language->translate('PLUGIN_LOGIN.FORGOT_FAILED_TO_EMAIL'), 'error');
  340. } else {
  341. $messages->add($language->translate('PLUGIN_LOGIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL'), 'info');
  342. }
  343. $this->setRedirect($this->login->getRoute('login') ?? '/');
  344. return true;
  345. }
  346. /**
  347. * Handle the reset password action.
  348. *
  349. * @return bool True if the action was performed.
  350. * @throws \Exception
  351. */
  352. public function taskReset()
  353. {
  354. $data = $this->post;
  355. $language = $this->grav['language'];
  356. $messages = $this->grav['messages'];
  357. if (isset($data['password'])) {
  358. /** @var UserCollectionInterface $users */
  359. $users = $this->grav['accounts'];
  360. $username = $data['username'] ?? null;
  361. $user = !empty($username) ? $users->find($username) : null;
  362. $password = $data['password'] ?? null;
  363. $token = $data['token'] ?? null;
  364. if ($user && !empty($user->reset) && $user->exists()) {
  365. [$good_token, $expire] = explode('::', $user->reset);
  366. if ($good_token === $token) {
  367. if (time() > $expire) {
  368. $messages->add($language->translate('PLUGIN_LOGIN.RESET_LINK_EXPIRED'), 'error');
  369. $this->grav->redirectLangSafe($this->login->getRoute('forgot') ?? '/');
  370. return true;
  371. }
  372. unset($user->hashed_password, $user->reset);
  373. $user->password = $password;
  374. $user->save();
  375. $messages->add($language->translate('PLUGIN_LOGIN.RESET_PASSWORD_RESET'), 'info');
  376. $this->setRedirect($this->login->getRoute('login') ?? '/');
  377. return true;
  378. }
  379. }
  380. $messages->add($language->translate('PLUGIN_LOGIN.RESET_INVALID_LINK'), 'error');
  381. $this->grav->redirectLangSafe($this->login->getRoute('forgot') ?? '/');
  382. return true;
  383. }
  384. $user = $this->grav['uri']->param('user');
  385. $token = $this->grav['uri']->param('token');
  386. if (!$user || !$token) {
  387. $messages->add($language->translate('PLUGIN_LOGIN.RESET_INVALID_LINK'), 'error');
  388. $this->grav->redirectLangSafe($this->login->getRoute('forgot') ?? '/');
  389. return true;
  390. }
  391. return true;
  392. }
  393. /**
  394. * @param null $secret
  395. * @return bool
  396. */
  397. public function taskRegenerate2FASecret()
  398. {
  399. try {
  400. /** @var UserInterface $user */
  401. $user = $this->grav['user'];
  402. if ($user->exists()) {
  403. /** @var TwoFactorAuth $twoFa */
  404. $twoFa = $this->grav['login']->twoFactorAuth();
  405. $secret = $twoFa->createSecret();
  406. $image = $twoFa->getQrImageData($user->username, $secret);
  407. // Change secret in the session.
  408. $user->twofa_secret = $secret;
  409. // Save secret into the user file.
  410. $user->save();
  411. $json_response = ['status' => 'success', 'image' => $image, 'secret' => trim(preg_replace('|(\w{4})|', '\\1 ', $secret))];
  412. } else {
  413. $json_response = ['status' => 'error', 'message' => 'user does not exist'];
  414. }
  415. } catch (\Exception $e) {
  416. $json_response = ['status' => 'error', 'message' => $e->getMessage()];
  417. }
  418. // Return JSON
  419. header('Content-Type: application/json');
  420. echo json_encode($json_response);
  421. exit;
  422. }
  423. /**
  424. * @return bool
  425. */
  426. public function taskInvite()
  427. {
  428. /** @var Forms $forms */
  429. $forms = $this->grav['forms'] ?? null;
  430. $form = $forms ? $forms->getActiveForm() : null;
  431. /** @var Language $t */
  432. $t = $this->grav['language'];
  433. if (null === $form) {
  434. $this->grav->fireEvent('onFormValidationError', new Event([
  435. 'form' => $form,
  436. 'message' => $t->translate("PLUGIN_LOGIN.INVALID_FORM"),
  437. ]));
  438. return false;
  439. }
  440. $data = $form->getData();
  441. $emails = $data['emails'] ?? null;
  442. $emails = array_unique(preg_split('/[\s,;]+/mu', $emails));
  443. $emails = array_filter($emails, static function ($str) { return $str && filter_var($str, FILTER_VALIDATE_EMAIL); });
  444. if (!$emails) {
  445. $this->grav->fireEvent('onFormValidationError', new Event([
  446. 'form' => $form,
  447. 'message' => $t->translate("PLUGIN_LOGIN.INVALID_INVITE_EMAILS"),
  448. ]));
  449. return false;
  450. }
  451. $message = $data['message'] ?? null;
  452. $defaults = [
  453. 'expiration' => 86400
  454. ];
  455. $invite = (array)($form->getBlueprint()->get('form/meta/invite')) + $defaults;
  456. /** @var UserInterface $user */
  457. $user = $this->grav['user'];
  458. $issuer = $user->email;
  459. $invitations = Invitations::getInstance();
  460. $list = [];
  461. foreach ($emails as $email) {
  462. $data = [
  463. 'email' => $email,
  464. 'created_by' => $issuer,
  465. 'created_timestamp' => time(),
  466. 'expiration_timestamp' => time() + $invite['expiration'],
  467. 'account' => $invite['account']
  468. ];
  469. $invitation = new Invitation($invitations->generateToken(), $data);
  470. $old = $invitations->getByEmail($email);
  471. if ($old) {
  472. $invitations->remove($old);
  473. }
  474. $invitations->add($invitation);
  475. $list[] = $invitation;
  476. }
  477. $invitations->save();
  478. foreach ($list as $invitation) {
  479. $this->login->sendInviteEmail($invitation, $message, $user);
  480. }
  481. return true;
  482. }
  483. /**
  484. * @return string
  485. */
  486. protected function getCurrentRedirect()
  487. {
  488. /** @var Uri $uri */
  489. $uri = $this->grav['uri'];
  490. $redirect = $uri->route();
  491. foreach ($uri->params(null, true) as $key => $value) {
  492. if (!in_array($key, ['task', 'nonce', 'login-nonce', 'logout-nonce'], true)) {
  493. $redirect .= $uri->params($key);
  494. }
  495. }
  496. return $redirect;
  497. }
  498. /**
  499. * Redirects an action
  500. */
  501. public function redirect()
  502. {
  503. if ($this->redirect) {
  504. $this->grav->redirectLangSafe($this->redirect, $this->redirectCode);
  505. }
  506. }
  507. /**
  508. * Set redirect.
  509. *
  510. * @param $path
  511. * @param int $code
  512. */
  513. public function setRedirect($path, $code = 303)
  514. {
  515. $this->redirect = $path;
  516. $this->redirectCode = $code;
  517. }
  518. /**
  519. * @return array Array containing [redirect, code].
  520. */
  521. public function getRedirect()
  522. {
  523. return [$this->redirect, $this->redirectCode];
  524. }
  525. /**
  526. * Prepare and return POST data.
  527. *
  528. * @param array $post
  529. *
  530. * @return array
  531. */
  532. protected function &getPost(array $post)
  533. {
  534. unset($post[$this->prefix]);
  535. // Decode JSON encoded fields and merge them to data.
  536. if (isset($post['_json'])) {
  537. $post = array_merge_recursive($post, $this->jsonDecode($post['_json']));
  538. unset($post['_json']);
  539. }
  540. return $post;
  541. }
  542. /**
  543. * Recursively JSON decode data.
  544. *
  545. * @param array $data
  546. *
  547. * @return array
  548. */
  549. protected function jsonDecode(array $data)
  550. {
  551. foreach ($data as &$value) {
  552. if (\is_array($value)) {
  553. $value = $this->jsonDecode($value);
  554. } else {
  555. $value = json_decode($value, true);
  556. }
  557. }
  558. return $data;
  559. }
  560. /**
  561. * Gets and sets the RememberMe class
  562. *
  563. * @param mixed $var A rememberMe instance to set
  564. *
  565. * @return RememberMe\RememberMe Returns the current rememberMe instance
  566. * @deprecated 2.5.0 Use $grav['login']->rememberMe() instead
  567. */
  568. public function rememberMe($var = null)
  569. {
  570. return $this->login->rememberMe($var);
  571. }
  572. /**
  573. * Check if user may use password reset functionality.
  574. *
  575. * @param UserInterface $user
  576. * @param $field
  577. * @param $count
  578. * @param $interval
  579. * @return bool
  580. * @deprecated 2.5.0 Use $grav['login']->getRateLimiter($context) instead. See Grav\Plugin\Login\RateLimiter class.
  581. */
  582. protected function isUserRateLimited(UserInterface $user, $field, $count, $interval)
  583. {
  584. return $this->login->isUserRateLimited($user, $field, $count, $interval);
  585. }
  586. /**
  587. * Reset the rate limit counter
  588. *
  589. * @param UserInterface $user
  590. * @param $field
  591. * @deprecated 2.5.0 Use $grav['login']->getRateLimiter($context) instead. See Grav\Plugin\Login\RateLimiter class.
  592. */
  593. protected function resetRateLimit(UserInterface $user, $field)
  594. {
  595. $this->login->resetRateLimit($user, $field);
  596. }
  597. /**
  598. * Authenticate user.
  599. *
  600. * @param array $form Form fields.
  601. *
  602. * @return bool
  603. * @deprecated 2.6.2 Will be removed without replacement.
  604. */
  605. protected function authenticate($form)
  606. {
  607. // Remove login nonce.
  608. $form = array_diff_key($form, ['login-form-nonce' => true]);
  609. return $this->login->login($form, ['remember_me' => true])->authenticated;
  610. }
  611. }