1
0

ProfilePageController.php 3.4 KB

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