LoginController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Controller;
  23. use OC;
  24. use OC\User\Session;
  25. use OC_App;
  26. use OC_Util;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\RedirectResponse;
  29. use OCP\AppFramework\Http\TemplateResponse;
  30. use OCP\IConfig;
  31. use OCP\IRequest;
  32. use OCP\ISession;
  33. use OCP\IURLGenerator;
  34. use OCP\IUser;
  35. use OCP\IUserManager;
  36. class LoginController extends Controller {
  37. /** @var IUserManager */
  38. private $userManager;
  39. /** @var IConfig */
  40. private $config;
  41. /** @var ISession */
  42. private $session;
  43. /** @var Session */
  44. private $userSession;
  45. /** @var IURLGenerator */
  46. private $urlGenerator;
  47. /**
  48. * @param string $appName
  49. * @param IRequest $request
  50. * @param IUserManager $userManager
  51. * @param IConfig $config
  52. * @param ISession $session
  53. * @param Session $userSession
  54. * @param IURLGenerator $urlGenerator
  55. */
  56. function __construct($appName, IRequest $request, IUserManager $userManager, IConfig $config, ISession $session,
  57. Session $userSession, IURLGenerator $urlGenerator) {
  58. parent::__construct($appName, $request);
  59. $this->userManager = $userManager;
  60. $this->config = $config;
  61. $this->session = $session;
  62. $this->userSession = $userSession;
  63. $this->urlGenerator = $urlGenerator;
  64. }
  65. /**
  66. * @NoAdminRequired
  67. * @UseSession
  68. *
  69. * @return RedirectResponse
  70. */
  71. public function logout() {
  72. $loginToken = $this->request->getCookie('oc_token');
  73. if (!is_null($loginToken)) {
  74. $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken);
  75. }
  76. $this->userSession->logout();
  77. return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm'));
  78. }
  79. /**
  80. * @PublicPage
  81. * @NoCSRFRequired
  82. * @UseSession
  83. *
  84. * @param string $user
  85. * @param string $redirect_url
  86. * @param string $remember_login
  87. *
  88. * @return TemplateResponse|RedirectResponse
  89. */
  90. public function showLoginForm($user, $redirect_url, $remember_login) {
  91. if ($this->userSession->isLoggedIn()) {
  92. return new RedirectResponse(OC_Util::getDefaultPageUrl());
  93. }
  94. $parameters = array();
  95. $loginMessages = $this->session->get('loginMessages');
  96. $errors = [];
  97. $messages = [];
  98. if (is_array($loginMessages)) {
  99. list($errors, $messages) = $loginMessages;
  100. }
  101. $this->session->remove('loginMessages');
  102. foreach ($errors as $value) {
  103. $parameters[$value] = true;
  104. }
  105. $parameters['messages'] = $messages;
  106. if (!is_null($user) && $user !== '') {
  107. $parameters['loginName'] = $user;
  108. $parameters['user_autofocus'] = false;
  109. } else {
  110. $parameters['loginName'] = '';
  111. $parameters['user_autofocus'] = true;
  112. }
  113. if (!empty($redirect_url)) {
  114. $parameters['redirect_url'] = $redirect_url;
  115. }
  116. $parameters['canResetPassword'] = true;
  117. if (!$this->config->getSystemValue('lost_password_link')) {
  118. if (!is_null($user) && $user !== '') {
  119. $userObj = $this->userManager->get($user);
  120. if ($userObj instanceof IUser) {
  121. $parameters['canResetPassword'] = $userObj->canChangePassword();
  122. }
  123. }
  124. }
  125. $parameters['alt_login'] = OC_App::getAlternativeLogIns();
  126. $parameters['rememberLoginAllowed'] = OC_Util::rememberLoginAllowed();
  127. $parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0;
  128. if (!is_null($user) && $user !== '') {
  129. $parameters['loginName'] = $user;
  130. $parameters['user_autofocus'] = false;
  131. } else {
  132. $parameters['loginName'] = '';
  133. $parameters['user_autofocus'] = true;
  134. }
  135. return new TemplateResponse(
  136. $this->appName, 'login', $parameters, 'guest'
  137. );
  138. }
  139. /**
  140. * @PublicPage
  141. * @UseSession
  142. *
  143. * @param string $user
  144. * @param string $password
  145. * @param string $redirect_url
  146. * @return RedirectResponse
  147. */
  148. public function tryLogin($user, $password, $redirect_url) {
  149. // TODO: Add all the insane error handling
  150. $loginResult = $this->userManager->checkPassword($user, $password);
  151. if ($loginResult === false) {
  152. $users = $this->userManager->getByEmail($user);
  153. // we only allow login by email if unique
  154. if (count($users) === 1) {
  155. $user = $users[0]->getUID();
  156. $loginResult = $this->userManager->checkPassword($user, $password);
  157. }
  158. }
  159. if ($loginResult === false) {
  160. $this->session->set('loginMessages', [
  161. ['invalidpassword']
  162. ]);
  163. // Read current user and append if possible
  164. $args = !is_null($user) ? ['user' => $user] : [];
  165. return new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args));
  166. }
  167. $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $password);
  168. if (!is_null($redirect_url) && $this->userSession->isLoggedIn()) {
  169. $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirect_url));
  170. // Deny the redirect if the URL contains a @
  171. // This prevents unvalidated redirects like ?redirect_url=:user@domain.com
  172. if (strpos($location, '@') === false) {
  173. return new RedirectResponse($location);
  174. }
  175. }
  176. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index'));
  177. }
  178. }