ProfilePageController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Controller;
  25. use OC\Profile\ProfileManager;
  26. use OCP\Profile\BeforeTemplateRenderedEvent;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\AppFramework\Services\IInitialState;
  30. use OCP\IRequest;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use OCP\Share\IManager as IShareManager;
  35. use OCP\UserStatus\IManager as IUserStatusManager;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. class ProfilePageController extends Controller {
  38. public function __construct(
  39. string $appName,
  40. IRequest $request,
  41. private IInitialState $initialStateService,
  42. private ProfileManager $profileManager,
  43. private IShareManager $shareManager,
  44. private IUserManager $userManager,
  45. private IUserSession $userSession,
  46. private IUserStatusManager $userStatusManager,
  47. private IEventDispatcher $eventDispatcher,
  48. ) {
  49. parent::__construct($appName, $request);
  50. }
  51. /**
  52. * @PublicPage
  53. * @NoCSRFRequired
  54. * @NoAdminRequired
  55. * @NoSubAdminRequired
  56. */
  57. public function index(string $targetUserId): TemplateResponse {
  58. $profileNotFoundTemplate = new TemplateResponse(
  59. 'core',
  60. '404-profile',
  61. [],
  62. TemplateResponse::RENDER_AS_GUEST,
  63. );
  64. $targetUser = $this->userManager->get($targetUserId);
  65. if (!($targetUser instanceof IUser) || !$targetUser->isEnabled()) {
  66. return $profileNotFoundTemplate;
  67. }
  68. $visitingUser = $this->userSession->getUser();
  69. if (!$this->profileManager->isProfileEnabled($targetUser)) {
  70. return $profileNotFoundTemplate;
  71. }
  72. // Run user enumeration checks only if viewing another user's profile
  73. if ($targetUser !== $visitingUser) {
  74. if (!$this->shareManager->currentUserCanEnumerateTargetUser($visitingUser, $targetUser)) {
  75. return $profileNotFoundTemplate;
  76. }
  77. }
  78. if ($visitingUser !== null) {
  79. $userStatuses = $this->userStatusManager->getUserStatuses([$targetUserId]);
  80. $status = $userStatuses[$targetUserId] ?? null;
  81. if ($status !== null) {
  82. $this->initialStateService->provideInitialState('status', [
  83. 'icon' => $status->getIcon(),
  84. 'message' => $status->getMessage(),
  85. ]);
  86. }
  87. }
  88. $this->initialStateService->provideInitialState(
  89. 'profileParameters',
  90. $this->profileManager->getProfileParams($targetUser, $visitingUser),
  91. );
  92. $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($targetUserId));
  93. \OCP\Util::addScript('core', 'profile');
  94. return new TemplateResponse(
  95. 'core',
  96. 'profile',
  97. [],
  98. $this->userSession->isLoggedIn() ? TemplateResponse::RENDER_AS_USER : TemplateResponse::RENDER_AS_PUBLIC,
  99. );
  100. }
  101. }