PageRenderTime 36ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ZfcUser/Controller/UserController.php

https://gitlab.com/my-application.bjoernbartels.earth/ZfcUser
PHP | 457 lines | 304 code | 73 blank | 80 comment | 39 complexity | 3f3263ebe7bd56af2224ba81adb93aae MD5 | raw file
  1. <?php
  2. namespace ZfcUser\Controller;
  3. use Zend\Form\FormInterface;
  4. use Zend\Mvc\Controller\AbstractActionController;
  5. use Zend\Stdlib\ResponseInterface as Response;
  6. use Zend\Stdlib\Parameters;
  7. use Zend\View\Model\ViewModel;
  8. use ZfcUser\Service\User as UserService;
  9. use ZfcUser\Options\UserControllerOptionsInterface;
  10. class UserController extends AbstractActionController
  11. {
  12. const ROUTE_CHANGEPASSWD = 'zfcuser/changepassword';
  13. const ROUTE_LOGIN = 'zfcuser/login';
  14. const ROUTE_REGISTER = 'zfcuser/register';
  15. const ROUTE_CHANGEEMAIL = 'zfcuser/changeemail';
  16. const CONTROLLER_NAME = 'zfcuser';
  17. /**
  18. * @var UserService
  19. */
  20. protected $userService;
  21. /**
  22. * @var FormInterface
  23. */
  24. protected $loginForm;
  25. /**
  26. * @var FormInterface
  27. */
  28. protected $registerForm;
  29. /**
  30. * @var FormInterface
  31. */
  32. protected $changePasswordForm;
  33. /**
  34. * @var FormInterface
  35. */
  36. protected $changeEmailForm;
  37. /**
  38. * @todo Make this dynamic / translation-friendly
  39. * @var string
  40. */
  41. protected $failedLoginMessage = 'Authentication failed. Please try again.';
  42. /**
  43. * @var UserControllerOptionsInterface
  44. */
  45. protected $options;
  46. /**
  47. * @var callable $redirectCallback
  48. */
  49. protected $redirectCallback;
  50. /**
  51. * @param callable $redirectCallback
  52. */
  53. public function __construct($redirectCallback)
  54. {
  55. if (!is_callable($redirectCallback)) {
  56. throw new \InvalidArgumentException('You must supply a callable redirectCallback');
  57. }
  58. $this->redirectCallback = $redirectCallback;
  59. }
  60. /**
  61. * User page
  62. */
  63. public function indexAction()
  64. {
  65. if (!$this->zfcUserAuthentication()->hasIdentity()) {
  66. return $this->redirect()->toRoute(static::ROUTE_LOGIN);
  67. }
  68. return new ViewModel();
  69. }
  70. /**
  71. * Login form
  72. */
  73. public function loginAction()
  74. {
  75. if ($this->zfcUserAuthentication()->hasIdentity()) {
  76. return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
  77. }
  78. $request = $this->getRequest();
  79. $form = $this->getLoginForm();
  80. if ($this->getOptions()->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) {
  81. $redirect = $request->getQuery()->get('redirect');
  82. } else {
  83. $redirect = false;
  84. }
  85. if (!$request->isPost()) {
  86. return array(
  87. 'loginForm' => $form,
  88. 'redirect' => $redirect,
  89. 'enableRegistration' => $this->getOptions()->getEnableRegistration(),
  90. );
  91. }
  92. $form->setData($request->getPost());
  93. if (!$form->isValid()) {
  94. $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
  95. return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN).($redirect ? '?redirect='. rawurlencode($redirect) : ''));
  96. }
  97. // clear adapters
  98. $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
  99. $this->zfcUserAuthentication()->getAuthService()->clearIdentity();
  100. return $this->forward()->dispatch(static::CONTROLLER_NAME, array('action' => 'authenticate'));
  101. }
  102. /**
  103. * Logout and clear the identity
  104. */
  105. public function logoutAction()
  106. {
  107. $this->zfcUserAuthentication()->getAuthAdapter()->resetAdapters();
  108. $this->zfcUserAuthentication()->getAuthAdapter()->logoutAdapters();
  109. $this->zfcUserAuthentication()->getAuthService()->clearIdentity();
  110. $redirect = $this->redirectCallback;
  111. return $redirect();
  112. }
  113. /**
  114. * General-purpose authentication action
  115. */
  116. public function authenticateAction()
  117. {
  118. if ($this->zfcUserAuthentication()->hasIdentity()) {
  119. return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
  120. }
  121. $adapter = $this->zfcUserAuthentication()->getAuthAdapter();
  122. $redirect = $this->params()->fromPost('redirect', $this->params()->fromQuery('redirect', false));
  123. $result = $adapter->prepareForAuthentication($this->getRequest());
  124. // Return early if an adapter returned a response
  125. if ($result instanceof Response) {
  126. return $result;
  127. }
  128. $auth = $this->zfcUserAuthentication()->getAuthService()->authenticate($adapter);
  129. if (!$auth->isValid()) {
  130. $this->flashMessenger()->setNamespace('zfcuser-login-form')->addMessage($this->failedLoginMessage);
  131. $adapter->resetAdapters();
  132. return $this->redirect()->toUrl(
  133. $this->url()->fromRoute(static::ROUTE_LOGIN) .
  134. ($redirect ? '?redirect='. rawurlencode($redirect) : '')
  135. );
  136. }
  137. $redirect = $this->redirectCallback;
  138. return $redirect();
  139. }
  140. /**
  141. * Register new user
  142. */
  143. public function registerAction()
  144. {
  145. // if the user is logged in, we don't need to register
  146. if ($this->zfcUserAuthentication()->hasIdentity()) {
  147. // redirect to the login redirect route
  148. return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
  149. }
  150. // if registration is disabled
  151. if (!$this->getOptions()->getEnableRegistration()) {
  152. return array('enableRegistration' => false);
  153. }
  154. $request = $this->getRequest();
  155. $service = $this->getUserService();
  156. $form = $this->getRegisterForm();
  157. if ($this->getOptions()->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) {
  158. $redirect = $request->getQuery()->get('redirect');
  159. } else {
  160. $redirect = false;
  161. }
  162. $redirectUrl = $this->url()->fromRoute(static::ROUTE_REGISTER)
  163. . ($redirect ? '?redirect=' . rawurlencode($redirect) : '');
  164. $prg = $this->prg($redirectUrl, true);
  165. if ($prg instanceof Response) {
  166. return $prg;
  167. } elseif ($prg === false) {
  168. return array(
  169. 'registerForm' => $form,
  170. 'enableRegistration' => $this->getOptions()->getEnableRegistration(),
  171. 'redirect' => $redirect,
  172. );
  173. }
  174. $post = $prg;
  175. $user = $service->register($post);
  176. $redirect = isset($prg['redirect']) ? $prg['redirect'] : null;
  177. if (!$user) {
  178. return array(
  179. 'registerForm' => $form,
  180. 'enableRegistration' => $this->getOptions()->getEnableRegistration(),
  181. 'redirect' => $redirect,
  182. );
  183. }
  184. if ($service->getOptions()->getLoginAfterRegistration()) {
  185. $identityFields = $service->getOptions()->getAuthIdentityFields();
  186. if (in_array('email', $identityFields)) {
  187. $post['identity'] = $user->getEmail();
  188. } elseif (in_array('username', $identityFields)) {
  189. $post['identity'] = $user->getUsername();
  190. }
  191. $post['credential'] = $post['password'];
  192. $request->setPost(new Parameters($post));
  193. return $this->forward()->dispatch(static::CONTROLLER_NAME, array('action' => 'authenticate'));
  194. }
  195. // TODO: Add the redirect parameter here...
  196. return $this->redirect()->toUrl($this->url()->fromRoute(static::ROUTE_LOGIN) . ($redirect ? '?redirect='. rawurlencode($redirect) : ''));
  197. }
  198. /**
  199. * Change the users password
  200. */
  201. public function changepasswordAction()
  202. {
  203. // if the user isn't logged in, we can't change password
  204. if (!$this->zfcUserAuthentication()->hasIdentity()) {
  205. // redirect to the login redirect route
  206. return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
  207. }
  208. $form = $this->getChangePasswordForm();
  209. $prg = $this->prg(static::ROUTE_CHANGEPASSWD);
  210. $fm = $this->flashMessenger()->setNamespace('change-password')->getMessages();
  211. if (isset($fm[0])) {
  212. $status = $fm[0];
  213. } else {
  214. $status = null;
  215. }
  216. if ($prg instanceof Response) {
  217. return $prg;
  218. } elseif ($prg === false) {
  219. return array(
  220. 'status' => $status,
  221. 'changePasswordForm' => $form,
  222. );
  223. }
  224. $form->setData($prg);
  225. if (!$form->isValid()) {
  226. return array(
  227. 'status' => false,
  228. 'changePasswordForm' => $form,
  229. );
  230. }
  231. if (!$this->getUserService()->changePassword($form->getData())) {
  232. return array(
  233. 'status' => false,
  234. 'changePasswordForm' => $form,
  235. );
  236. }
  237. $this->flashMessenger()->setNamespace('change-password')->addMessage(true);
  238. return $this->redirect()->toRoute(static::ROUTE_CHANGEPASSWD);
  239. }
  240. public function changeEmailAction()
  241. {
  242. // if the user isn't logged in, we can't change email
  243. if (!$this->zfcUserAuthentication()->hasIdentity()) {
  244. // redirect to the login redirect route
  245. return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
  246. }
  247. $form = $this->getChangeEmailForm();
  248. $request = $this->getRequest();
  249. $request->getPost()->set('identity', $this->getUserService()->getAuthService()->getIdentity()->getEmail());
  250. $fm = $this->flashMessenger()->setNamespace('change-email')->getMessages();
  251. if (isset($fm[0])) {
  252. $status = $fm[0];
  253. } else {
  254. $status = null;
  255. }
  256. $prg = $this->prg(static::ROUTE_CHANGEEMAIL);
  257. if ($prg instanceof Response) {
  258. return $prg;
  259. } elseif ($prg === false) {
  260. return array(
  261. 'status' => $status,
  262. 'changeEmailForm' => $form,
  263. );
  264. }
  265. $form->setData($prg);
  266. if (!$form->isValid()) {
  267. return array(
  268. 'status' => false,
  269. 'changeEmailForm' => $form,
  270. );
  271. }
  272. $change = $this->getUserService()->changeEmail($prg);
  273. if (!$change) {
  274. $this->flashMessenger()->setNamespace('change-email')->addMessage(false);
  275. return array(
  276. 'status' => false,
  277. 'changeEmailForm' => $form,
  278. );
  279. }
  280. $this->flashMessenger()->setNamespace('change-email')->addMessage(true);
  281. return $this->redirect()->toRoute(static::ROUTE_CHANGEEMAIL);
  282. }
  283. /**
  284. * Getters/setters for DI stuff
  285. */
  286. public function getUserService()
  287. {
  288. if (!$this->userService) {
  289. $this->userService = $this->getServiceLocator()->get('zfcuser_user_service');
  290. }
  291. return $this->userService;
  292. }
  293. public function setUserService(UserService $userService)
  294. {
  295. $this->userService = $userService;
  296. return $this;
  297. }
  298. public function getRegisterForm()
  299. {
  300. if (!$this->registerForm) {
  301. $this->setRegisterForm($this->getServiceLocator()->get('zfcuser_register_form'));
  302. }
  303. return $this->registerForm;
  304. }
  305. public function setRegisterForm(FormInterface$registerForm)
  306. {
  307. $this->registerForm = $registerForm;
  308. }
  309. public function getLoginForm()
  310. {
  311. if (!$this->loginForm) {
  312. $this->setLoginForm($this->getServiceLocator()->get('zfcuser_login_form'));
  313. }
  314. return $this->loginForm;
  315. }
  316. public function setLoginForm(FormInterface $loginForm)
  317. {
  318. $this->loginForm = $loginForm;
  319. $fm = $this->flashMessenger()->setNamespace('zfcuser-login-form')->getMessages();
  320. if (isset($fm[0])) {
  321. $this->loginForm->setMessages(
  322. array('identity' => array($fm[0]))
  323. );
  324. }
  325. return $this;
  326. }
  327. public function getChangePasswordForm()
  328. {
  329. if (!$this->changePasswordForm) {
  330. $this->setChangePasswordForm($this->getServiceLocator()->get('zfcuser_change_password_form'));
  331. }
  332. return $this->changePasswordForm;
  333. }
  334. public function setChangePasswordForm(FormInterface $changePasswordForm)
  335. {
  336. $this->changePasswordForm = $changePasswordForm;
  337. return $this;
  338. }
  339. /**
  340. * set options
  341. *
  342. * @param UserControllerOptionsInterface $options
  343. * @return UserController
  344. */
  345. public function setOptions(UserControllerOptionsInterface $options)
  346. {
  347. $this->options = $options;
  348. return $this;
  349. }
  350. /**
  351. * get options
  352. *
  353. * @return UserControllerOptionsInterface
  354. */
  355. public function getOptions()
  356. {
  357. if (!$this->options instanceof UserControllerOptionsInterface) {
  358. $this->setOptions($this->getServiceLocator()->get('zfcuser_module_options'));
  359. }
  360. return $this->options;
  361. }
  362. /**
  363. * Get changeEmailForm.
  364. * @return ChangeEmailForm
  365. */
  366. public function getChangeEmailForm()
  367. {
  368. if (!$this->changeEmailForm) {
  369. $this->setChangeEmailForm($this->getServiceLocator()->get('zfcuser_change_email_form'));
  370. }
  371. return $this->changeEmailForm;
  372. }
  373. /**
  374. * Set changeEmailForm.
  375. *
  376. * @param $changeEmailForm - the value to set.
  377. * @return $this
  378. */
  379. public function setChangeEmailForm($changeEmailForm)
  380. {
  381. $this->changeEmailForm = $changeEmailForm;
  382. return $this;
  383. }
  384. }