AdminController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\UpdateNotification\Controller;
  28. use OCA\UpdateNotification\BackgroundJob\ResetToken;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\DataResponse;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. use OCP\BackgroundJob\IJobList;
  34. use OCP\IAppConfig;
  35. use OCP\IConfig;
  36. use OCP\IL10N;
  37. use OCP\IRequest;
  38. use OCP\Security\ISecureRandom;
  39. use OCP\Util;
  40. class AdminController extends Controller {
  41. public function __construct(
  42. string $appName,
  43. IRequest $request,
  44. private IJobList $jobList,
  45. private ISecureRandom $secureRandom,
  46. private IConfig $config,
  47. private IAppConfig $appConfig,
  48. private ITimeFactory $timeFactory,
  49. private IL10N $l10n,
  50. ) {
  51. parent::__construct($appName, $request);
  52. }
  53. private function isUpdaterEnabled() {
  54. return !$this->config->getSystemValue('upgrade.disable-web', false);
  55. }
  56. /**
  57. * @param string $channel
  58. * @return DataResponse
  59. */
  60. public function setChannel(string $channel): DataResponse {
  61. Util::setChannel($channel);
  62. $this->appConfig->setValueInt('core', 'lastupdatedat', 0);
  63. return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]);
  64. }
  65. /**
  66. * @return DataResponse
  67. */
  68. public function createCredentials(): DataResponse {
  69. if (!$this->isUpdaterEnabled()) {
  70. return new DataResponse(['status' => 'error', 'message' => $this->l10n->t('Web updater is disabled')], Http::STATUS_FORBIDDEN);
  71. }
  72. // Create a new job and store the creation date
  73. $this->jobList->add(ResetToken::class);
  74. $this->appConfig->setValueInt('core', 'updater.secret.created', $this->timeFactory->getTime());
  75. // Create a new token
  76. $newToken = $this->secureRandom->generate(64);
  77. $this->config->setSystemValue('updater.secret', password_hash($newToken, PASSWORD_DEFAULT));
  78. return new DataResponse($newToken);
  79. }
  80. }