EmailLoginCommand.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Authentication\Login;
  25. use OCP\IUserManager;
  26. class EmailLoginCommand extends ALoginCommand {
  27. /** @var IUserManager */
  28. private $userManager;
  29. public function __construct(IUserManager $userManager) {
  30. $this->userManager = $userManager;
  31. }
  32. public function process(LoginData $loginData): LoginResult {
  33. if ($loginData->getUser() === false) {
  34. if (!filter_var($loginData->getUsername(), FILTER_VALIDATE_EMAIL)) {
  35. return $this->processNextOrFinishSuccessfully($loginData);
  36. }
  37. $users = $this->userManager->getByEmail($loginData->getUsername());
  38. // we only allow login by email if unique
  39. if (count($users) === 1) {
  40. // FIXME: This is a workaround to still stick to configured LDAP login filters
  41. // this can be removed once the email login is properly implemented in the local user backend
  42. // as described in https://github.com/nextcloud/server/issues/5221
  43. if ($users[0]->getBackendClassName() === 'LDAP') {
  44. return $this->processNextOrFinishSuccessfully($loginData);
  45. }
  46. $username = $users[0]->getUID();
  47. if ($username !== $loginData->getUsername()) {
  48. $user = $this->userManager->checkPassword(
  49. $username,
  50. $loginData->getPassword()
  51. );
  52. if ($user !== false) {
  53. $loginData->setUser($user);
  54. $loginData->setUsername($username);
  55. }
  56. }
  57. }
  58. }
  59. return $this->processNextOrFinishSuccessfully($loginData);
  60. }
  61. }