PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Kwf/Controller/Action/User/BackendActivateController.php

https://github.com/koala-framework/koala-framework
PHP | 152 lines | 131 code | 18 blank | 3 comment | 22 complexity | 990098d50b6800cf53c70a5c5968aec3 MD5 | raw file
  1. <?php
  2. class Kwf_Controller_Action_User_BackendActivateController extends Kwf_Controller_Action
  3. {
  4. public function preDispatch()
  5. {
  6. $this->getHelper('viewRenderer')->setNoController(true);
  7. $this->getHelper('viewRenderer')->setViewScriptPathNoControllerSpec('user/:action.:suffix');
  8. if (!$this->_getParam('user') && $this->getRequest()->getActionName() != 'error') {
  9. $code = $this->_getParam('code');
  10. if (!preg_match('#^(.*)-(\w*)$#', $code, $m)) {
  11. $this->getRequest()->setParam('errorMessage', trlKwf("Activation code is invalid. Maybe the URL wasn't copied completely?"));
  12. $this->forward('error');
  13. } else {
  14. $userId = $m[1];
  15. $code = $m[2];
  16. $userModel = Kwf_Registry::get('userModel');
  17. $user = $userModel->getRow($userId);
  18. $this->getRequest()->setParam('user', $user);
  19. if (!$user) {
  20. $this->getRequest()->setParam('errorMessage', trlKwf("Activation code is invalid. Maybe the URL wasn't copied completely?"));
  21. $this->forward('error');
  22. } else if (!$user->validateActivationToken($code) && $user->isActivated()) {
  23. $this->getRequest()->setParam('errorMessage', trlKwf("This account has already been activated."));
  24. $this->forward('error');
  25. } else if (!$user->validateActivationToken($code)) {
  26. $this->getRequest()->setParam('errorMessage', trlKwf("Activation code is invalid. Maybe your account has already been activated, the URL was not copied completely, or the password has already been set?"));
  27. $this->forward('error');
  28. }
  29. }
  30. }
  31. $this->view->dep = 'Admin';
  32. parent::preDispatch();
  33. }
  34. protected function _isAllowedResource()
  35. {
  36. return true;
  37. }
  38. protected function _initFields()
  39. {
  40. parent::_initFields();
  41. $this->_form->setModel(new Kwf_Model_FnF());
  42. }
  43. public function indexAction()
  44. {
  45. $this->view->contentScript = $this->getHelper('viewRenderer')->getViewScript('activate');
  46. $this->view->email = $this->_getParam('user')->email;
  47. $this->view->isActivated = $this->_getParam('user')->isActivated();
  48. $users = Kwf_Registry::get('userModel');
  49. $showPassword = false;
  50. //is there a password auth?
  51. foreach ($users->getAuthMethods() as $auth) {
  52. if ($auth instanceof Kwf_User_Auth_Interface_Password) {
  53. $showPassword = true;
  54. }
  55. }
  56. //if a redirect auth doesn't allow password hide it
  57. foreach ($users->getAuthMethods() as $auth) {
  58. if ($auth instanceof Kwf_User_Auth_Interface_Redirect) {
  59. if (!$auth->allowPasswordForUser($this->getParam('user'))) {
  60. $showPassword = false;
  61. }
  62. }
  63. }
  64. $this->view->showPassword = $showPassword;
  65. if ($showPassword) {
  66. $url = $this->getFrontController()->getRouter()->assemble(array(
  67. 'controller' => 'backend-change-password',
  68. 'action' => 'index',
  69. ), 'kwf_user');
  70. $url .= '?code='.$this->_getParam('code');
  71. $this->view->passwordUrl = $url;
  72. }
  73. $this->view->redirects = array();
  74. foreach ($users->getAuthMethods() as $k=>$auth) {
  75. if ($auth instanceof Kwf_User_Auth_Interface_Redirect && $auth->showInBackend() && $auth->showForActivation()) {
  76. $url = $this->getFrontController()->getRouter()->assemble(array(
  77. 'controller' => 'backend-activate',
  78. 'action' => 'redirect',
  79. ), 'kwf_user');
  80. $label = $auth->getLoginRedirectLabel();
  81. $this->view->redirects[] = array(
  82. 'url' => $url,
  83. 'authMethod' => $k,
  84. 'code' => $this->_getParam('code'),
  85. 'redirect' => $_SERVER['REQUEST_URI'],
  86. 'name' => Kwf_Trl::getInstance()->trlStaticExecute($label['name']),
  87. 'icon' => isset($label['icon']) ? '/assets/'.$label['icon'] : false,
  88. 'formOptionsHtml' => Kwf_User_Auth_Helper::getRedirectFormOptionsHtml($auth->getLoginRedirectFormOptions()),
  89. );
  90. }
  91. }
  92. if (count($this->view->redirects) == 0 && $showPassword) {
  93. $this->redirect($this->view->passwordUrl, array('prependBase'=>false));
  94. }
  95. }
  96. private function _getRedirectBackUrl()
  97. {
  98. $redirectBackUrl = $this->getFrontController()->getRouter()->assemble(array(
  99. 'controller' => 'login',
  100. 'action' => 'redirect-callback',
  101. ), 'kwf_user');
  102. $redirectBackUrl = 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'
  103. .$_SERVER['HTTP_HOST']
  104. .$redirectBackUrl;
  105. return $redirectBackUrl;
  106. }
  107. public function redirectAction()
  108. {
  109. $authMethod = $this->_getParam('authMethod');
  110. $users = Kwf_Registry::get('userModel');
  111. $authMethods = $users->getAuthMethods();
  112. if (!isset($authMethods[$authMethod])) {
  113. throw new Kwf_Exception_NotFound();
  114. }
  115. $f = new Kwf_Filter_StrongRandom();
  116. $state = 'activate.'.$authMethod.'.'.$f->filter(null).'.'.$this->_getParam('code') . '.' . urlencode('/kwf/welcome');
  117. //save state in namespace to validate it later
  118. $ns = new Kwf_Session_Namespace('kwf-login-redirect');
  119. $ns->state = $state;
  120. $formValues = array();
  121. foreach ($authMethods[$authMethod]->getLoginRedirectFormOptions() as $option) {
  122. if ($option['type'] == 'select') {
  123. $formValues[$option['name']] = $this->_getParam($option['name']);
  124. }
  125. }
  126. $url = $authMethods[$authMethod]->getLoginRedirectUrl($this->_getRedirectBackUrl(), $state, $formValues);
  127. $this->redirect($url);
  128. }
  129. public function errorAction()
  130. {
  131. $this->view->contentScript = $this->getHelper('viewRenderer')->getViewScript('activate-error');
  132. $this->view->errorMessage = $this->_getParam('errorMessage');
  133. }
  134. }