CreateSessionTokenCommand.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. * @author J0WI <J0WI@users.noreply.github.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Authentication\Login;
  27. use OC\Authentication\Token\IToken;
  28. use OC\User\Session;
  29. use OCP\IConfig;
  30. class CreateSessionTokenCommand extends ALoginCommand {
  31. /** @var IConfig */
  32. private $config;
  33. /** @var Session */
  34. private $userSession;
  35. public function __construct(IConfig $config,
  36. Session $userSession) {
  37. $this->config = $config;
  38. $this->userSession = $userSession;
  39. }
  40. public function process(LoginData $loginData): LoginResult {
  41. $tokenType = IToken::REMEMBER;
  42. if ($this->config->getSystemValueInt('remember_login_cookie_lifetime', 60 * 60 * 24 * 15) === 0) {
  43. $loginData->setRememberLogin(false);
  44. $tokenType = IToken::DO_NOT_REMEMBER;
  45. }
  46. if ($loginData->getPassword() === '') {
  47. $this->userSession->createSessionToken(
  48. $loginData->getRequest(),
  49. $loginData->getUser()->getUID(),
  50. $loginData->getUsername(),
  51. null,
  52. $tokenType
  53. );
  54. $this->userSession->updateTokens(
  55. $loginData->getUser()->getUID(),
  56. ''
  57. );
  58. } else {
  59. $this->userSession->createSessionToken(
  60. $loginData->getRequest(),
  61. $loginData->getUser()->getUID(),
  62. $loginData->getUsername(),
  63. $loginData->getPassword(),
  64. $tokenType
  65. );
  66. $this->userSession->updateTokens(
  67. $loginData->getUser()->getUID(),
  68. $loginData->getPassword()
  69. );
  70. }
  71. return $this->processNextOrFinishSuccessfully($loginData);
  72. }
  73. }