PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/application/admin/controllers/AuthController.php

https://github.com/rjdjohnston/core
PHP | 204 lines | 123 code | 37 blank | 44 comment | 17 complexity | e2c1bc20d091d7ec54e8b0eb98d23623 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2008-2009 Laurent Eschenauer and Alard Weisscher
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. class Admin_AuthController extends Zend_Controller_Action
  19. {
  20. protected $_application;
  21. protected $_bookmarklet = false;
  22. public function init()
  23. {
  24. $this->_application = Stuffpress_Application::getInstance();
  25. // If request is from a bookmarklet, we use another layout
  26. if ($this->_hasParam('bookmarklet') && $this->_getParam('bookmarklet')) {
  27. $this->_helper->layout->setlayout('bookmarklet');
  28. $this->_bookmarklet = true;
  29. $this->view->bookmarklet = true;
  30. }
  31. }
  32. public function indexAction()
  33. {
  34. $target = $this->_getParam("target");
  35. $form = $this->getForm();
  36. if ($target) {
  37. $form->getElement('target')->setValue($target);
  38. }
  39. if (!isset($this->view->form)) {
  40. $this->view->form = $form;
  41. }
  42. }
  43. public function loginAction()
  44. {
  45. // This should be a post request
  46. if (!$this->getRequest()->isPost()) {
  47. return $this->_forward('index', 'index', 'admin');
  48. }
  49. // Whatever happens from here, we first clear all identity
  50. $this->_application->user = false;
  51. $this->_application->role = 'guest';
  52. // Validate the form
  53. $form = $this->getForm();
  54. if (!$form->isValid($_POST)) {
  55. // Failed validation; redisplay form
  56. $this->view->failedValidation = true;
  57. $this->view->form = $form;
  58. return $this->_forward('index');
  59. }
  60. // Get (and maybe we should also clean) the values
  61. $values = $form->getValues();
  62. $username = $values['username'];
  63. $password = $values['password'];
  64. $remember = $values['remember'];
  65. // Get the user
  66. $users = new Users();
  67. if (!$user = $users->getUserFromUsername($username)) {
  68. $this->view->failedAuthentication = true;
  69. $this->view->form = $form;
  70. return $this->_forward('index');
  71. }
  72. // Validate the password
  73. if ($user->password != md5($password)) {
  74. $this->view->failedAuthentication = true;
  75. $this->view->form = $form;
  76. return $this->_forward('index');
  77. }
  78. // Is the user verified ?
  79. if (!$user->verified) {
  80. $this->view->unverified=true;
  81. $this->view->failedAuthentication = true;
  82. $this->view->form = $form;
  83. return $this->_forward('index');
  84. }
  85. // We assing arole
  86. $role = 'member';
  87. // Everything ok, we can log in and assign role
  88. $this->_application->user = $user;
  89. $this->_application->role = $role;
  90. // We can also hit the login stats
  91. $users->hitLogin($user->id);
  92. // Send the cookie with the authentication data
  93. $cookie = new Stuffpress_Cookie($user->id);
  94. $cookie->set($remember);
  95. $config = Zend_Registry::get('configuration');
  96. $domain = trim($config->web->host, " /");
  97. $path = trim($config->web->path, " /");
  98. // If we have a special target
  99. if ($values['target'] == 'user_page') {
  100. // If single user, we go back to the host
  101. if (isset($config->app->user)) {
  102. $url = "http://$domain/$path";
  103. } else {
  104. $url = "http://{$user->username}.$domain/$path";
  105. };
  106. return $this->_redirect($url);
  107. }
  108. else if ($values['target']) {
  109. $target = trim($values['target'], " /");
  110. return $this->_redirect("http://$domain/$target");
  111. }
  112. // Otherwise we go back to the home page
  113. return $this->_helper->redirector('index', 'index', 'admin');
  114. }
  115. public function logoutAction()
  116. {
  117. // We clear the identity immediately
  118. $this->_application->user = false;
  119. $this->_application->role = 'guest';
  120. // Clear the cookie
  121. $cookie = new Stuffpress_Cookie();
  122. $cookie->logout();
  123. // Get the request parameters
  124. $target = $this->_getParam("target");
  125. // If we have a target, we go there
  126. if ($target) {
  127. return $this->_redirect($target);
  128. }
  129. // Otherwise we go back to the home page
  130. return $this->_redirect('/');
  131. }
  132. private function getForm() {
  133. $form = new Stuffpress_Form();
  134. // Add the form element details
  135. $form->setAction('admin/auth/login');
  136. $form->setMethod('post');
  137. $form->setName('formLoginMain');
  138. // Create and configure username element:
  139. $username = $form->createElement('text', 'username', array('label' => 'Username:', 'decorators' => $form->noDecorators));
  140. $username->addValidator('alnum');
  141. $username->addValidator('stringLength', false, array(4, 20));
  142. $username->setRequired(true);
  143. $username->addFilter('StringToLower');
  144. $form->addElement($username);
  145. // Create and configure password element:
  146. $password = $form->createElement('password', 'password', array('label' => 'Password:', 'decorators' => $form->noDecorators));
  147. $password->addValidator('StringLength', false, array(6, 20));
  148. $password->setRequired(true);
  149. $form->addElement($password);
  150. // Remember me
  151. $element = $form->createElement('checkbox', 'remember', array('label' => 'Remember:', 'decorators' => $form->noDecorators, 'class' => 'remember'));
  152. $element->setRequired(true);
  153. $form->addElement($element);
  154. // Add a hidden element with a target url
  155. $target = $form->createElement('hidden', 'target');
  156. $target->setDecorators(array(array('ViewHelper')));
  157. // Add a hidden element with a bookmarklet flag
  158. $bk = $form->createElement('hidden', 'bookmarklet');
  159. $bk->setDecorators(array(array('ViewHelper')));
  160. $bk->setValue($this->_bookmarklet);
  161. // Add elements to form:
  162. $form->addElement($target);
  163. $form->addElement($bk);
  164. $form->addElement('submit', 'login', array('label' => 'Sign in', 'decorators' => $form->noDecorators));
  165. return $form;
  166. }
  167. }