ProfilePageController.php 3.5 KB

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