Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BeforePreferenceListener.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Theming\Listener;
  25. use OCA\Theming\AppInfo\Application;
  26. use OCP\App\IAppManager;
  27. use OCP\Config\BeforePreferenceDeletedEvent;
  28. use OCP\Config\BeforePreferenceSetEvent;
  29. use OCP\EventDispatcher\Event;
  30. use OCP\EventDispatcher\IEventListener;
  31. /** @template-implements IEventListener<BeforePreferenceDeletedEvent|BeforePreferenceSetEvent> */
  32. class BeforePreferenceListener implements IEventListener {
  33. public function __construct(
  34. private IAppManager $appManager,
  35. ) {
  36. }
  37. public function handle(Event $event): void {
  38. if (!$event instanceof BeforePreferenceSetEvent
  39. && !$event instanceof BeforePreferenceDeletedEvent) {
  40. // Invalid event type
  41. return;
  42. }
  43. switch ($event->getAppId()) {
  44. case Application::APP_ID: $this->handleThemingValues($event);
  45. break;
  46. case 'core': $this->handleCoreValues($event);
  47. break;
  48. }
  49. }
  50. private function handleThemingValues(BeforePreferenceSetEvent|BeforePreferenceDeletedEvent $event): void {
  51. if ($event->getConfigKey() !== 'shortcuts_disabled') {
  52. // Not allowed config key
  53. return;
  54. }
  55. if ($event instanceof BeforePreferenceSetEvent) {
  56. $event->setValid($event->getConfigValue() === 'yes');
  57. return;
  58. }
  59. $event->setValid(true);
  60. }
  61. private function handleCoreValues(BeforePreferenceSetEvent|BeforePreferenceDeletedEvent $event): void {
  62. if ($event->getConfigKey() !== 'apporder') {
  63. // Not allowed config key
  64. return;
  65. }
  66. if ($event instanceof BeforePreferenceDeletedEvent) {
  67. $event->setValid(true);
  68. return;
  69. }
  70. $value = json_decode($event->getConfigValue(), true, flags:JSON_THROW_ON_ERROR);
  71. if (!is_array(($value))) {
  72. // Must be an array
  73. return;
  74. }
  75. foreach ($value as $id => $info) {
  76. // required format: [ navigation_id: string => [ order: int, app?: string ] ]
  77. if (!is_string($id) || !is_array($info) || empty($info) || !isset($info['order']) || !is_numeric($info['order']) || (isset($info['app']) && !$this->appManager->isEnabledForUser($info['app']))) {
  78. // Invalid config value, refuse the change
  79. return;
  80. }
  81. }
  82. $event->setValid(true);
  83. }
  84. }