AdminController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\UpdateNotification\Controller;
  8. use OCA\UpdateNotification\BackgroundJob\ResetToken;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\DataResponse;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\BackgroundJob\IJobList;
  14. use OCP\IAppConfig;
  15. use OCP\IConfig;
  16. use OCP\IL10N;
  17. use OCP\IRequest;
  18. use OCP\Security\ISecureRandom;
  19. use OCP\Util;
  20. class AdminController extends Controller {
  21. public function __construct(
  22. string $appName,
  23. IRequest $request,
  24. private IJobList $jobList,
  25. private ISecureRandom $secureRandom,
  26. private IConfig $config,
  27. private IAppConfig $appConfig,
  28. private ITimeFactory $timeFactory,
  29. private IL10N $l10n,
  30. ) {
  31. parent::__construct($appName, $request);
  32. }
  33. private function isUpdaterEnabled() {
  34. return !$this->config->getSystemValue('upgrade.disable-web', false);
  35. }
  36. /**
  37. * @param string $channel
  38. * @return DataResponse
  39. */
  40. public function setChannel(string $channel): DataResponse {
  41. Util::setChannel($channel);
  42. $this->appConfig->setValueInt('core', 'lastupdatedat', 0);
  43. return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]);
  44. }
  45. /**
  46. * @return DataResponse
  47. */
  48. public function createCredentials(): DataResponse {
  49. if (!$this->isUpdaterEnabled()) {
  50. return new DataResponse(['status' => 'error', 'message' => $this->l10n->t('Web updater is disabled')], Http::STATUS_FORBIDDEN);
  51. }
  52. // Create a new job and store the creation date
  53. $this->jobList->add(ResetToken::class);
  54. $this->appConfig->setValueInt('core', 'updater.secret.created', $this->timeFactory->getTime());
  55. // Create a new token
  56. $newToken = $this->secureRandom->generate(64);
  57. $this->config->setSystemValue('updater.secret', password_hash($newToken, PASSWORD_DEFAULT));
  58. return new DataResponse($newToken);
  59. }
  60. }