PageRenderTime 51ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Cockpit/Controller/Auth.php

https://github.com/agentejo/cockpit
PHP | 166 lines | 107 code | 51 blank | 8 comment | 25 complexity | f867ae47d3682f4a4593a749d0402e34 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file is part of the Cockpit project.
  4. *
  5. * (c) Artur Heinze - πŸ…°πŸ…ΆπŸ…΄πŸ…½πŸ†ƒπŸ…΄πŸ…ΉπŸ…Ύ, http://agentejo.com
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Cockpit\Controller;
  11. class Auth extends \LimeExtra\Controller {
  12. public function check() {
  13. if ($data = $this->param('auth')) {
  14. if (isset($data['user']) && $this->app->helper('utils')->isEmail($data['user'])) {
  15. $data['email'] = $data['user'];
  16. $data['user'] = '';
  17. }
  18. if (!$this->app->helper('csfr')->isValid('login', $this->param('csfr'), true)) {
  19. $this->app->trigger('cockpit.authentication.failed', [$data, 'Csfr validation failed']);
  20. return ['success' => false, 'error' => 'Csfr validation failed'];
  21. }
  22. $user = $this->module('cockpit')->authenticate($data);
  23. if ($user && !$this->module('cockpit')->hasaccess('cockpit', 'backend', @$user['group'])) {
  24. $user = null;
  25. }
  26. if ($user) {
  27. $this->app->trigger('cockpit.authentication.success', [&$user]);
  28. $this->module('cockpit')->setUser($user);
  29. } else {
  30. $this->app->trigger('cockpit.authentication.failed', [$data, 'User not found']);
  31. }
  32. if ($this->app->request->is('ajax')) {
  33. return $user ? ['success' => true, 'user' => $user, 'avatar'=> md5($user['email'])] : ['success' => false, 'error' => 'User not found'];
  34. } else {
  35. $this->reroute('/');
  36. }
  37. }
  38. return false;
  39. }
  40. public function login() {
  41. $redirectTo = '/';
  42. if ($this->param('to') && \substr($this->param('to'), 0, 1) == '/') {
  43. $redirectTo = $this->param('to');
  44. }
  45. return $this->render('cockpit:views/layouts/login.php', compact('redirectTo'));
  46. }
  47. public function logout() {
  48. $this->module('cockpit')->logout();
  49. if ($this->app->request->is('ajax')) {
  50. return ['logout' => true];
  51. } else {
  52. $this->reroute('/auth/login?logout=1');
  53. }
  54. }
  55. public function forgotpassword() {
  56. return $this->render('cockpit:views/layouts/forgotpassword.php');
  57. }
  58. public function requestreset() {
  59. if ($user = $this->param('user')) {
  60. $query = ['active' => true];
  61. if ($this->app->helper('utils')->isEmail($user)) {
  62. $query['email'] = $user;
  63. } else {
  64. $query['user'] = $user;
  65. }
  66. $user = $this->app->storage->findOne('cockpit/accounts', $query);
  67. if (!$user) {
  68. return $this->stop(['error' => $this('i18n')->get('User does not exist')], 404);
  69. }
  70. $token = uniqid('rp-'.bin2hex(random_bytes(16)));
  71. $target = $this->app->param('', $this->app->getSiteUrl(true).'/auth/newpassword');
  72. $data = ['_id' => $user['_id'], '_reset_token' => $token];
  73. $this->app->storage->save('cockpit/accounts', $data);
  74. $message = $this->app->view('cockpit:emails/recover.php', compact('user','token','target'));
  75. try {
  76. $response = $this->app->mailer->mail(
  77. $user['email'],
  78. $this->param('subject', $this->app->getSiteUrl().' - '.$this('i18n')->get('Password Recovery')),
  79. $message
  80. );
  81. } catch (\Exception $e) {
  82. $response = $e->getMessage();
  83. }
  84. if ($response !== true) {
  85. return $this->stop(['error' => $this('i18n')->get($response)], 404);
  86. }
  87. return ['message' => $this('i18n')->get('Recovery email sent')];
  88. }
  89. return $this->stop(['error' => $this('i18n')->get('User required')], 412);
  90. }
  91. public function newpassword() {
  92. if ($token = $this->param('token')) {
  93. $user = $this->app->storage->findOne('cockpit/accounts', ['_reset_token' => $token]);
  94. if (!$user) {
  95. return false;
  96. }
  97. $user['md5email'] = md5($user['email']);
  98. return $this->render('cockpit:views/layouts/newpassword.php', compact('user', 'token'));
  99. }
  100. return false;
  101. }
  102. public function resetpassword() {
  103. if ($token = $this->param('token')) {
  104. $user = $this->app->storage->findOne('cockpit/accounts', ['_reset_token' => $token]);
  105. $password = trim($this->param('password'));
  106. if (!$user || !$password) {
  107. return false;
  108. }
  109. $data = ['_id' => $user['_id'], 'password' =>$this->app->hash($password), '_reset_token' => null];
  110. $this->app->storage->save('cockpit/accounts', $data);
  111. return ['success' => true, 'message' => $this('i18n')->get('Password updated')];
  112. }
  113. return $this->stop(['error' => $this('i18n')->get('Token required')], 412);
  114. }
  115. }