PageRenderTime 41ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/users/actions/block/front/LoginAction.class.php

http://pagizer-cms.googlecode.com/
PHP | 185 lines | 136 code | 32 blank | 17 comment | 18 complexity | f63499b00082c72c6e49507431f0bf63 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * This file is part of the Pagizer package.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. *
  8. * @copyright Copyright (c) 2010 Advisa (http://www.advisa.fr)
  9. * @author Pagizer Core Team <team@pagizer.org>
  10. * @package pagizer
  11. * @subpackage users
  12. */
  13. class m_users_actions_block_front_LoginAction extends f_core_Action
  14. {
  15. private $user;
  16. private $groups;
  17. private $cookie;
  18. public function execute()
  19. {
  20. $pageId = $this->getBlockParameter("pageRedirect");
  21. $this->getAuthorizedGroups();
  22. $this->cookie = f_core_Cookie::getInstance()->setName("login")->setExpiration(time()+365*24*3600);
  23. $cookie = $this->cookie->getCookie();
  24. if(!is_null($cookie) && is_null($this->getUser()))
  25. {
  26. $this->logIn($cookie);
  27. }
  28. $user = $this->getUser();
  29. $access = $this->isAuthorized($user);
  30. if(!$access)
  31. {
  32. $errors = $this->logIn();
  33. $this->addToModel("errors", $errors);
  34. }
  35. else
  36. {
  37. $this->addToModel("user", $user);
  38. }
  39. $this->addToModel("blockParams", $this->getBlockContext()->getParameters());
  40. $this->addToModel("access", $access);
  41. $this->setView('success');
  42. }
  43. public function validate()
  44. {
  45. return true;
  46. }
  47. public function errorHandler()
  48. {
  49. $this->setView('error');
  50. }
  51. private function logIn($cookie = false)
  52. {
  53. $key = m_backoffice_services_BackOfficeService::getKey();
  54. if(!$cookie)
  55. {
  56. $values = $this->getFormValues("FormLogin");
  57. if(!is_null($values))
  58. {
  59. $values["password"] = md5($values['password'].$key);
  60. }
  61. }
  62. else
  63. {
  64. $values = $cookie;
  65. }
  66. if(!is_null($values))
  67. {
  68. $errors = $this->checkLoginForm($values);
  69. if(count($errors) == 0)
  70. {
  71. $user = $this->user;
  72. if(isset($values["remember"]))
  73. {
  74. $this->cookie->setCookie(array("login" => $user->getLogin(), "password" => $values["password"]));
  75. }
  76. $rights = m_rights_services_RightsService::getInstance()->getUserRights($user);
  77. $this->getUserContext()->removeRights()->setUser($user);
  78. foreach($rights as $right)
  79. {
  80. $this->getUserContext()->addRight($right);
  81. }
  82. $this->redirect();
  83. }
  84. return $errors;
  85. }
  86. return false;
  87. }
  88. /**
  89. * Check login form
  90. *
  91. * @param $values
  92. * @return array
  93. */
  94. private function checkLoginForm($values)
  95. {
  96. $documentProvider = f_document_Provider::getInstance();
  97. $loginSys = $this->getBlockParameter("loginSys", "login");
  98. $user = $documentProvider->loadDocuments("modules_users/user")
  99. ->whereIsEqual($loginSys, $values['login'])
  100. ->whereIsEqual("password", $values['password'])
  101. ->byPublicationStatus("PUBLISHED")
  102. ->retrieveFirstDocument();
  103. $valid = $this->isAuthorized($user);
  104. if($valid == false)
  105. {
  106. $errors["errors"] = $this->getLocale("users.locales.badLogPass");
  107. }
  108. return $errors;
  109. }
  110. private function redirect()
  111. {
  112. $pageIds = explode(",",$this->getBlockParameter("pageRedirect"));
  113. $url = URL_ABSOLUTE;
  114. if(is_array($pageIds) && is_numeric($this->groups))
  115. {
  116. $url = $this->getDocumentProvider()->getByUniqueId($pageIds[$this->groups], $this->getBlockParameter("pageLang"))->getUrl();
  117. }
  118. $this->getController()->redirect($url);
  119. }
  120. private function getAuthorizedGroups()
  121. {
  122. if(!is_null($this->getBlockParameter("group")))
  123. {
  124. $groups = explode(",",$this->getBlockParameter("group"));
  125. foreach($groups as $groupId)
  126. {
  127. $this->groups[] = $this->getDocumentProvider()->getByModelAndUniqueId("modules_users/group", $groupId, $this->getBlockParameter("pageLang"));
  128. }
  129. }
  130. else
  131. {
  132. $this->groups = array();
  133. }
  134. }
  135. private function isAuthorized($user)
  136. {
  137. $valid = false;
  138. if(!is_null($user) && is_object($user))
  139. {
  140. foreach($this->groups as $key => $group)
  141. {
  142. $valid = $user->belongsGroup($group);
  143. if($valid == true)
  144. {
  145. $this->user = $user;
  146. $this->groups = $key;
  147. break;
  148. }
  149. }
  150. }
  151. return $valid;
  152. }
  153. }