ProfilePageController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Profile\ProfileManager;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http\Attribute\AnonRateLimit;
  11. use OCP\AppFramework\Http\Attribute\BruteForceProtection;
  12. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  13. use OCP\AppFramework\Http\Attribute\OpenAPI;
  14. use OCP\AppFramework\Http\Attribute\UserRateLimit;
  15. use OCP\AppFramework\Http\TemplateResponse;
  16. use OCP\AppFramework\Services\IInitialState;
  17. use OCP\EventDispatcher\IEventDispatcher;
  18. use OCP\INavigationManager;
  19. use OCP\IRequest;
  20. use OCP\IUserManager;
  21. use OCP\IUserSession;
  22. use OCP\Profile\BeforeTemplateRenderedEvent;
  23. use OCP\Share\IManager as IShareManager;
  24. use OCP\UserStatus\IManager as IUserStatusManager;
  25. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  26. class ProfilePageController extends Controller {
  27. public function __construct(
  28. string $appName,
  29. IRequest $request,
  30. private IInitialState $initialStateService,
  31. private ProfileManager $profileManager,
  32. private IShareManager $shareManager,
  33. private IUserManager $userManager,
  34. private IUserSession $userSession,
  35. private IUserStatusManager $userStatusManager,
  36. private INavigationManager $navigationManager,
  37. private IEventDispatcher $eventDispatcher,
  38. ) {
  39. parent::__construct($appName, $request);
  40. }
  41. /**
  42. * @PublicPage
  43. * @NoCSRFRequired
  44. * @NoAdminRequired
  45. * @NoSubAdminRequired
  46. */
  47. #[FrontpageRoute(verb: 'GET', url: '/u/{targetUserId}')]
  48. #[BruteForceProtection(action: 'user')]
  49. #[UserRateLimit(limit: 30, period: 120)]
  50. #[AnonRateLimit(limit: 30, period: 120)]
  51. public function index(string $targetUserId): TemplateResponse {
  52. $profileNotFoundTemplate = new TemplateResponse(
  53. 'core',
  54. '404-profile',
  55. [],
  56. TemplateResponse::RENDER_AS_GUEST,
  57. );
  58. $targetUser = $this->userManager->get($targetUserId);
  59. if ($targetUser === null) {
  60. $profileNotFoundTemplate->throttle();
  61. return $profileNotFoundTemplate;
  62. }
  63. if (!$targetUser->isEnabled()) {
  64. return $profileNotFoundTemplate;
  65. }
  66. $visitingUser = $this->userSession->getUser();
  67. if (!$this->profileManager->isProfileEnabled($targetUser)) {
  68. return $profileNotFoundTemplate;
  69. }
  70. // Run user enumeration checks only if viewing another user's profile
  71. if ($targetUser !== $visitingUser) {
  72. if (!$this->shareManager->currentUserCanEnumerateTargetUser($visitingUser, $targetUser)) {
  73. return $profileNotFoundTemplate;
  74. }
  75. }
  76. if ($visitingUser !== null) {
  77. $userStatuses = $this->userStatusManager->getUserStatuses([$targetUserId]);
  78. $status = $userStatuses[$targetUserId] ?? null;
  79. if ($status !== null) {
  80. $this->initialStateService->provideInitialState('status', [
  81. 'icon' => $status->getIcon(),
  82. 'message' => $status->getMessage(),
  83. ]);
  84. }
  85. }
  86. $this->initialStateService->provideInitialState(
  87. 'profileParameters',
  88. $this->profileManager->getProfileFields($targetUser, $visitingUser),
  89. );
  90. if ($targetUser === $visitingUser) {
  91. $this->navigationManager->setActiveEntry('profile');
  92. }
  93. $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($targetUserId));
  94. \OCP\Util::addScript('core', 'profile');
  95. return new TemplateResponse(
  96. 'core',
  97. 'profile',
  98. [],
  99. $this->userSession->isLoggedIn() ? TemplateResponse::RENDER_AS_USER : TemplateResponse::RENDER_AS_PUBLIC,
  100. );
  101. }
  102. }