PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/classes/controller/admin/login.ctrl.php

https://github.com/jay3/core
PHP | 123 lines | 97 code | 15 blank | 11 comment | 11 complexity | 80a785da877b8b3da24b2de8e1e4505e MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * NOVIUS OS - Web OS for digital communication
  4. *
  5. * @copyright 2011 Novius
  6. * @license GNU Affero General Public License v3 or (at your option) any later version
  7. * http://www.gnu.org/licenses/agpl-3.0.html
  8. * @link http://www.novius-os.org
  9. */
  10. namespace Nos;
  11. class Controller_Admin_Login extends Controller
  12. {
  13. public $template = 'nos::admin/html';
  14. public function before()
  15. {
  16. parent::before();
  17. // If user is already logged in, proceed
  18. if (\Nos\Auth::check()) {
  19. $this->redirect();
  20. }
  21. I18n::setLocale(\Input::get('lang', \Config::get('novius-os.default_locale', 'en_GB')));
  22. I18n::current_dictionary('nos::common');
  23. }
  24. protected function redirect()
  25. {
  26. \Response::redirect(urldecode(\Input::get('redirect', 'admin/')));
  27. exit();
  28. }
  29. public function action_popup()
  30. {
  31. $error_msg = '';
  32. if (\Input::method() == 'POST') {
  33. $error_msg = $this->post_login();
  34. if ($error_msg === true) {
  35. \Response::json(
  36. array(
  37. 'closeDialog' => true,
  38. 'notify' => strtr(__('Welcome back, {{user}}.'), array(
  39. '{{user}}' => \Session::user()->user_firstname,
  40. )),
  41. )
  42. );
  43. } else {
  44. \Response::json(
  45. array(
  46. 'error' => $error_msg,
  47. )
  48. );
  49. }
  50. }
  51. // Bypass the template
  52. return \View::forge('admin/login_popup', array(
  53. 'lang' => \Input::get('lang', false),
  54. ), false);
  55. }
  56. public function action_index()
  57. {
  58. $error_msg = '';
  59. if (\Input::method() == 'POST') {
  60. $error_msg = $this->post_login();
  61. if ($error_msg === true) {
  62. $this->redirect();
  63. }
  64. }
  65. \Asset::add_path('static/novius-os/admin/novius-os/');
  66. \Asset::css('login.css', array(), 'css');
  67. $this->template->body = \View::forge('admin/login', array(
  68. 'error' => $error_msg,
  69. ));
  70. return $this->template;
  71. }
  72. public function action_reset()
  73. {
  74. // Bypass the template
  75. return \Response::forge(\View::forge('admin/login_reset'));
  76. }
  77. public function after($response)
  78. {
  79. foreach (array(
  80. 'title' => 'Administration',
  81. 'base' => \Uri::base(false),
  82. 'require' => 'static/novius-os/admin/vendor/requirejs/require.js',
  83. ) as $var => $default) {
  84. if (empty($this->template->$var)) {
  85. $this->template->$var = $default;
  86. }
  87. }
  88. $ret = parent::after($response);
  89. $this->template->set(array(
  90. 'css' => \Asset::render('css'),
  91. 'js' => \Asset::render('js'),
  92. ), false, false);
  93. return $ret;
  94. }
  95. protected function post_login()
  96. {
  97. if (\Nos\Auth::login($_POST['email'], $_POST['password'], (bool) \Input::post('remember_me', false))) {
  98. if (\Event::has_events('user_login')) {
  99. \Log::deprecated('Event "user_login" is deprecated, use "admin.loginSuccess" instead.', 'Chiba.2');
  100. \Event::trigger('user_login');
  101. }
  102. \Event::trigger('admin.loginSuccess');
  103. return true;
  104. }
  105. \Event::trigger('admin.loginFail');
  106. return __('These details won’t get you in. Are you sure you’ve typed the correct email address and password? Please try again.');
  107. }
  108. }