AddMissingSecretJob.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Repair\NC25;
  8. use OCP\HintException;
  9. use OCP\IConfig;
  10. use OCP\Migration\IOutput;
  11. use OCP\Migration\IRepairStep;
  12. use OCP\Security\ISecureRandom;
  13. class AddMissingSecretJob implements IRepairStep {
  14. private IConfig $config;
  15. private ISecureRandom $random;
  16. public function __construct(IConfig $config, ISecureRandom $random) {
  17. $this->config = $config;
  18. $this->random = $random;
  19. }
  20. public function getName(): string {
  21. return 'Add possibly missing system config';
  22. }
  23. public function run(IOutput $output): void {
  24. $passwordSalt = $this->config->getSystemValueString('passwordsalt', '');
  25. if ($passwordSalt === '') {
  26. try {
  27. $this->config->setSystemValue('passwordsalt', $this->random->generate(30));
  28. } catch (HintException $e) {
  29. $output->warning('passwordsalt is missing from your config.php and your config.php is read only. Please fix it manually.');
  30. }
  31. }
  32. $secret = $this->config->getSystemValueString('secret', '');
  33. if ($secret === '') {
  34. try {
  35. $this->config->setSystemValue('secret', $this->random->generate(48));
  36. } catch (HintException $e) {
  37. $output->warning('secret is missing from your config.php and your config.php is read only. Please fix it manually.');
  38. }
  39. }
  40. }
  41. }