AdminController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  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\DataResponse;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\BackgroundJob\IJobList;
  32. use OCP\IConfig;
  33. use OCP\IL10N;
  34. use OCP\IRequest;
  35. use OCP\Security\ISecureRandom;
  36. use OCP\Util;
  37. class AdminController extends Controller {
  38. /** @var IJobList */
  39. private $jobList;
  40. /** @var ISecureRandom */
  41. private $secureRandom;
  42. /** @var IConfig */
  43. private $config;
  44. /** @var ITimeFactory */
  45. private $timeFactory;
  46. /** @var IL10N */
  47. private $l10n;
  48. /**
  49. * @param string $appName
  50. * @param IRequest $request
  51. * @param IJobList $jobList
  52. * @param ISecureRandom $secureRandom
  53. * @param IConfig $config
  54. * @param ITimeFactory $timeFactory
  55. * @param IL10N $l10n
  56. */
  57. public function __construct($appName,
  58. IRequest $request,
  59. IJobList $jobList,
  60. ISecureRandom $secureRandom,
  61. IConfig $config,
  62. ITimeFactory $timeFactory,
  63. IL10N $l10n) {
  64. parent::__construct($appName, $request);
  65. $this->jobList = $jobList;
  66. $this->secureRandom = $secureRandom;
  67. $this->config = $config;
  68. $this->timeFactory = $timeFactory;
  69. $this->l10n = $l10n;
  70. }
  71. /**
  72. * @param string $channel
  73. * @return DataResponse
  74. */
  75. public function setChannel(string $channel): DataResponse {
  76. Util::setChannel($channel);
  77. $this->config->setAppValue('core', 'lastupdatedat', 0);
  78. return new DataResponse(['status' => 'success', 'data' => ['message' => $this->l10n->t('Channel updated')]]);
  79. }
  80. /**
  81. * @return DataResponse
  82. */
  83. public function createCredentials(): DataResponse {
  84. // Create a new job and store the creation date
  85. $this->jobList->add(ResetTokenBackgroundJob::class);
  86. $this->config->setAppValue('core', 'updater.secret.created', $this->timeFactory->getTime());
  87. // Create a new token
  88. $newToken = $this->secureRandom->generate(64);
  89. $this->config->setSystemValue('updater.secret', password_hash($newToken, PASSWORD_DEFAULT));
  90. return new DataResponse($newToken);
  91. }
  92. }