1
0

ProfilePageController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. * @author Kate Döen <kate.doeen@nextcloud.com>
  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 OC\Core\Controller;
  26. use OC\Profile\ProfileManager;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\Attribute\AnonRateLimit;
  29. use OCP\AppFramework\Http\Attribute\BruteForceProtection;
  30. use OCP\AppFramework\Http\Attribute\OpenAPI;
  31. use OCP\AppFramework\Http\Attribute\UserRateLimit;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\AppFramework\Services\IInitialState;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\INavigationManager;
  36. use OCP\IRequest;
  37. use OCP\IUserManager;
  38. use OCP\IUserSession;
  39. use OCP\Profile\BeforeTemplateRenderedEvent;
  40. use OCP\Share\IManager as IShareManager;
  41. use OCP\UserStatus\IManager as IUserStatusManager;
  42. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  43. class ProfilePageController extends Controller {
  44. public function __construct(
  45. string $appName,
  46. IRequest $request,
  47. private IInitialState $initialStateService,
  48. private ProfileManager $profileManager,
  49. private IShareManager $shareManager,
  50. private IUserManager $userManager,
  51. private IUserSession $userSession,
  52. private IUserStatusManager $userStatusManager,
  53. private INavigationManager $navigationManager,
  54. private IEventDispatcher $eventDispatcher,
  55. ) {
  56. parent::__construct($appName, $request);
  57. }
  58. /**
  59. * @PublicPage
  60. * @NoCSRFRequired
  61. * @NoAdminRequired
  62. * @NoSubAdminRequired
  63. */
  64. #[BruteForceProtection(action: 'user')]
  65. #[UserRateLimit(limit: 30, period: 120)]
  66. #[AnonRateLimit(limit: 30, period: 120)]
  67. public function index(string $targetUserId): TemplateResponse {
  68. $profileNotFoundTemplate = new TemplateResponse(
  69. 'core',
  70. '404-profile',
  71. [],
  72. TemplateResponse::RENDER_AS_GUEST,
  73. );
  74. $targetUser = $this->userManager->get($targetUserId);
  75. if ($targetUser === null) {
  76. $profileNotFoundTemplate->throttle();
  77. return $profileNotFoundTemplate;
  78. }
  79. if (!$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->getProfileFields($targetUser, $visitingUser),
  105. );
  106. if ($targetUser === $visitingUser) {
  107. $this->navigationManager->setActiveEntry('profile');
  108. }
  109. $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($targetUserId));
  110. \OCP\Util::addScript('core', 'profile');
  111. return new TemplateResponse(
  112. 'core',
  113. 'profile',
  114. [],
  115. $this->userSession->isLoggedIn() ? TemplateResponse::RENDER_AS_USER : TemplateResponse::RENDER_AS_PUBLIC,
  116. );
  117. }
  118. }