MigrateUserConfig.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019 Janis Köhr <janiskoehr@icloud.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Janis Köhr <janis.koehr@novatec-gmbh.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Theming\Migration;
  26. use OCA\Theming\AppInfo\Application;
  27. use OCA\Theming\Service\ThemesService;
  28. use OCA\Theming\Themes\DarkHighContrastTheme;
  29. use OCA\Theming\Themes\DarkTheme;
  30. use OCA\Theming\Themes\DyslexiaFont;
  31. use OCA\Theming\Themes\HighContrastTheme;
  32. use OCP\IConfig;
  33. use OCP\IUser;
  34. use OCP\IUserManager;
  35. use OCP\Migration\IOutput;
  36. use OCP\Migration\IRepairStep;
  37. class MigrateUserConfig implements IRepairStep {
  38. protected IUserManager $userManager;
  39. protected IConfig $config;
  40. protected ThemesService $themesService;
  41. protected DarkTheme $darkTheme;
  42. protected DarkHighContrastTheme $darkHighContrastTheme;
  43. protected HighContrastTheme $highContrastTheme;
  44. protected DyslexiaFont $dyslexiaFont;
  45. /**
  46. * MigrateUserConfig constructor.
  47. */
  48. public function __construct(IConfig $config,
  49. IUserManager $userManager,
  50. ThemesService $themesService,
  51. DarkTheme $darkTheme,
  52. DarkHighContrastTheme $darkHighContrastTheme,
  53. HighContrastTheme $highContrastTheme,
  54. DyslexiaFont $dyslexiaFont) {
  55. $this->config = $config;
  56. $this->userManager = $userManager;
  57. $this->themesService = $themesService;
  58. $this->darkTheme = $darkTheme;
  59. $this->darkHighContrastTheme = $darkHighContrastTheme;
  60. $this->highContrastTheme = $highContrastTheme;
  61. $this->dyslexiaFont = $dyslexiaFont;
  62. }
  63. /**
  64. * Returns the step's name
  65. *
  66. * @return string
  67. * @since 25.0.0
  68. */
  69. public function getName() {
  70. return 'Migrate old user accessibility config';
  71. }
  72. /**
  73. * Run repair step.
  74. * Must throw exception on error.
  75. *
  76. * @param IOutput $output
  77. * @throws \Exception in case of failure
  78. * @since 25.0.0
  79. */
  80. public function run(IOutput $output) {
  81. $output->startProgress();
  82. $this->userManager->callForSeenUsers(function (IUser $user) use ($output) {
  83. $config = [];
  84. $font = $this->config->getUserValue($user->getUID(), 'accessibility', 'font', false);
  85. $highcontrast = $this->config->getUserValue($user->getUID(), 'accessibility', 'highcontrast', false);
  86. $theme = $this->config->getUserValue($user->getUID(), 'accessibility', 'theme', false);
  87. if ($highcontrast || $theme) {
  88. if ($theme === 'dark' && $highcontrast === 'highcontrast') {
  89. $config[] = $this->darkHighContrastTheme->getId();
  90. } else if ($theme === 'dark') {
  91. $config[] = $this->darkTheme->getId();
  92. } else if ($highcontrast === 'highcontrast') {
  93. $config[] = $this->highContrastTheme->getId();
  94. }
  95. }
  96. if ($font === 'fontdyslexic') {
  97. $config[] = $this->dyslexiaFont->getId();
  98. }
  99. if (!empty($config)) {
  100. $this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique($config)));
  101. }
  102. $output->advance();
  103. });
  104. $this->config->deleteAppFromAllUsers('accessibility');
  105. $output->finishProgress();
  106. }
  107. }