ProfilePageController.php 3.7 KB

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