ProfilePageController.php 3.7 KB

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