BeforeTemplateRenderedListener.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  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\UserStatus\Listener;
  26. use OC\Profile\ProfileManager;
  27. use OCA\UserStatus\AppInfo\Application;
  28. use OCA\UserStatus\Service\JSDataService;
  29. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  30. use OCP\AppFramework\Http\TemplateResponse;
  31. use OCP\EventDispatcher\Event;
  32. use OCP\EventDispatcher\IEventListener;
  33. use OCP\IInitialStateService;
  34. use OCP\IUserSession;
  35. /** @template-implements IEventListener<BeforeTemplateRenderedEvent> */
  36. class BeforeTemplateRenderedListener implements IEventListener {
  37. /** @var ProfileManager */
  38. private $profileManager;
  39. /** @var IUserSession */
  40. private $userSession;
  41. /** @var IInitialStateService */
  42. private $initialState;
  43. /** @var JSDataService */
  44. private $jsDataService;
  45. /**
  46. * BeforeTemplateRenderedListener constructor.
  47. *
  48. * @param ProfileManager $profileManager
  49. * @param IUserSession $userSession
  50. * @param IInitialStateService $initialState
  51. * @param JSDataService $jsDataService
  52. */
  53. public function __construct(
  54. ProfileManager $profileManager,
  55. IUserSession $userSession,
  56. IInitialStateService $initialState,
  57. JSDataService $jsDataService
  58. ) {
  59. $this->profileManager = $profileManager;
  60. $this->userSession = $userSession;
  61. $this->initialState = $initialState;
  62. $this->jsDataService = $jsDataService;
  63. }
  64. /**
  65. * @inheritDoc
  66. */
  67. public function handle(Event $event): void {
  68. $user = $this->userSession->getUser();
  69. if ($user === null) {
  70. return;
  71. }
  72. if (!($event instanceof BeforeTemplateRenderedEvent)) {
  73. // Unrelated
  74. return;
  75. }
  76. if (!$event->isLoggedIn() || $event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_USER) {
  77. return;
  78. }
  79. $this->initialState->provideLazyInitialState(Application::APP_ID, 'status', function () {
  80. return $this->jsDataService;
  81. });
  82. $this->initialState->provideLazyInitialState(Application::APP_ID, 'profileEnabled', function () use ($user) {
  83. return ['profileEnabled' => $this->profileManager->isProfileEnabled($user)];
  84. });
  85. \OCP\Util::addScript('user_status', 'menu');
  86. \OCP\Util::addStyle('user_status', 'user-status-menu');
  87. }
  88. }