ProfilePageController.php 3.8 KB

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