ProfilePageController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.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 OC\Core\Controller;
  25. use OC\Profile\ProfileManager;
  26. use OCP\Profile\BeforeTemplateRenderedEvent;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\AppFramework\Services\IInitialState;
  30. use OCP\IRequest;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use OCP\Share\IManager as IShareManager;
  35. use OCP\UserStatus\IManager as IUserStatusManager;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. class ProfilePageController extends Controller {
  38. private IInitialState $initialStateService;
  39. private ProfileManager $profileManager;
  40. private IShareManager $shareManager;
  41. private IUserManager $userManager;
  42. private IUserSession $userSession;
  43. private IUserStatusManager $userStatusManager;
  44. private IEventDispatcher $eventDispatcher;
  45. public function __construct(
  46. $appName,
  47. IRequest $request,
  48. IInitialState $initialStateService,
  49. ProfileManager $profileManager,
  50. IShareManager $shareManager,
  51. IUserManager $userManager,
  52. IUserSession $userSession,
  53. IUserStatusManager $userStatusManager,
  54. IEventDispatcher $eventDispatcher
  55. ) {
  56. parent::__construct($appName, $request);
  57. $this->initialStateService = $initialStateService;
  58. $this->profileManager = $profileManager;
  59. $this->shareManager = $shareManager;
  60. $this->userManager = $userManager;
  61. $this->userSession = $userSession;
  62. $this->userStatusManager = $userStatusManager;
  63. $this->eventDispatcher = $eventDispatcher;
  64. }
  65. /**
  66. * @PublicPage
  67. * @NoCSRFRequired
  68. * @NoAdminRequired
  69. * @NoSubAdminRequired
  70. */
  71. public function index(string $targetUserId): TemplateResponse {
  72. $profileNotFoundTemplate = new TemplateResponse(
  73. 'core',
  74. '404-profile',
  75. [],
  76. TemplateResponse::RENDER_AS_GUEST,
  77. );
  78. $targetUser = $this->userManager->get($targetUserId);
  79. if (!($targetUser instanceof IUser) || !$targetUser->isEnabled()) {
  80. return $profileNotFoundTemplate;
  81. }
  82. $visitingUser = $this->userSession->getUser();
  83. if (!$this->profileManager->isProfileEnabled($targetUser)) {
  84. return $profileNotFoundTemplate;
  85. }
  86. // Run user enumeration checks only if viewing another user's profile
  87. if ($targetUser !== $visitingUser) {
  88. if (!$this->shareManager->currentUserCanEnumerateTargetUser($visitingUser, $targetUser)) {
  89. return $profileNotFoundTemplate;
  90. }
  91. }
  92. if ($visitingUser !== null) {
  93. $userStatuses = $this->userStatusManager->getUserStatuses([$targetUserId]);
  94. $status = $userStatuses[$targetUserId] ?? null;
  95. if ($status !== null) {
  96. $this->initialStateService->provideInitialState('status', [
  97. 'icon' => $status->getIcon(),
  98. 'message' => $status->getMessage(),
  99. ]);
  100. }
  101. }
  102. $this->initialStateService->provideInitialState(
  103. 'profileParameters',
  104. $this->profileManager->getProfileParams($targetUser, $visitingUser),
  105. );
  106. $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($targetUserId));
  107. \OCP\Util::addScript('core', 'profile');
  108. return new TemplateResponse(
  109. 'core',
  110. 'profile',
  111. [],
  112. $this->userSession->isLoggedIn() ? TemplateResponse::RENDER_AS_USER : TemplateResponse::RENDER_AS_PUBLIC,
  113. );
  114. }
  115. }