AdminController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Vincent Petry <vincent@nextcloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  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, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\UpdateNotification\Controller;
  27. use OCA\UpdateNotification\ResetTokenBackgroundJob;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\IConfig;
  34. use OCP\IL10N;
  35. use OCP\IRequest;
  36. use OCP\Security\ISecureRandom;
  37. use OCP\Util;
  38. class AdminController extends Controller {
  39. /** @var IJobList */
  40. private $jobList;
  41. /** @var ISecureRandom */
  42. private $secureRandom;
  43. /** @var IConfig */
  44. private $config;
  45. /** @var ITimeFactory */
  46. private $timeFactory;
  47. /** @var IL10N */
  48. private $l10n;
  49. /**
  50. * @param string $appName
  51. * @param IRequest $request
  52. * @param IJobList $jobList
  53. * @param ISecureRandom $secureRandom
  54. * @param IConfig $config
  55. * @param ITimeFactory $timeFactory
  56. * @param IL10N $l10n
  57. */
  58. public function __construct($appName,
  59. IRequest $request,
  60. IJobList $jobList,
  61. ISecureRandom $secureRandom,
  62. IConfig $config,
  63. ITimeFactory $timeFactory,
  64. IL10N $l10n) {
  65. parent::__construct($appName, $request);
  66. $this->jobList = $jobList;
  67. $this->secureRandom = $secureRandom;
  68. $this->config = $config;
  69. $this->timeFactory = $timeFactory;
  70. $this->l10n = $l10n;
  71. }
  72. private function isUpdaterEnabled() {
  73. return !$this->config->getSystemValue('upgrade.disable-web', false);
  74. }
  75. /**
  76. * @param string $channel
  77. * @return DataResponse
  78. */
  79. public function setChannel(string $channel): DataResponse {
  80. Util::setChannel($channel);
  81. $this->config->setAppValue('core', 'lastupdatedat', 0);
  82. return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]);
  83. }
  84. /**
  85. * @return DataResponse
  86. */
  87. public function createCredentials(): DataResponse {
  88. if (!$this->isUpdaterEnabled()) {
  89. return new DataResponse(['status' => 'error', 'message' => $this->l10n->t('Web updater is disabled')], Http::STATUS_FORBIDDEN);
  90. }
  91. // Create a new job and store the creation date
  92. $this->jobList->add(ResetTokenBackgroundJob::class);
  93. $this->config->setAppValue('core', 'updater.secret.created', $this->timeFactory->getTime());
  94. // Create a new token
  95. $newToken = $this->secureRandom->generate(64);
  96. $this->config->setSystemValue('updater.secret', password_hash($newToken, PASSWORD_DEFAULT));
  97. return new DataResponse($newToken);
  98. }
  99. }