BeforeTemplateRenderedListener.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. class BeforeTemplateRenderedListener implements IEventListener {
  36. /** @var ProfileManager */
  37. private $profileManager;
  38. /** @var IUserSession */
  39. private $userSession;
  40. /** @var IInitialStateService */
  41. private $initialState;
  42. /** @var JSDataService */
  43. private $jsDataService;
  44. /**
  45. * BeforeTemplateRenderedListener constructor.
  46. *
  47. * @param ProfileManager $profileManager
  48. * @param IUserSession $userSession
  49. * @param IInitialStateService $initialState
  50. * @param JSDataService $jsDataService
  51. */
  52. public function __construct(
  53. ProfileManager $profileManager,
  54. IUserSession $userSession,
  55. IInitialStateService $initialState,
  56. JSDataService $jsDataService
  57. ) {
  58. $this->profileManager = $profileManager;
  59. $this->userSession = $userSession;
  60. $this->initialState = $initialState;
  61. $this->jsDataService = $jsDataService;
  62. }
  63. /**
  64. * @inheritDoc
  65. */
  66. public function handle(Event $event): void {
  67. $user = $this->userSession->getUser();
  68. if ($user === null) {
  69. return;
  70. }
  71. if (!($event instanceof BeforeTemplateRenderedEvent)) {
  72. // Unrelated
  73. return;
  74. }
  75. if (!$event->isLoggedIn() || $event->getResponse()->getRenderAs() !== TemplateResponse::RENDER_AS_USER) {
  76. return;
  77. }
  78. $this->initialState->provideLazyInitialState(Application::APP_ID, 'status', function () {
  79. return $this->jsDataService;
  80. });
  81. $this->initialState->provideLazyInitialState(Application::APP_ID, 'profileEnabled', function () use ($user) {
  82. return ['profileEnabled' => $this->profileManager->isProfileEnabled($user)];
  83. });
  84. \OCP\Util::addScript('user_status', 'menu');
  85. \OCP\Util::addStyle('user_status', 'user-status-menu');
  86. }
  87. }