1<?php
2
3declare(strict_types=1);
4
5/**
6 * @copyright 2021 Christopher Ng <chrng8@gmail.com>
7 *
8 * @author Christopher Ng <chrng8@gmail.com>
9 *
10 * @license GNU AGPL version 3 or any later version
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Affero General Public License as
14 * published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27namespace OC\Core\Controller;
28
29use OC\KnownUser\KnownUserService;
30use OC\Profile\ProfileManager;
31use OCP\Accounts\IAccountManager;
32use OCP\AppFramework\Controller;
33use OCP\AppFramework\Http\TemplateResponse;
34use OCP\AppFramework\Services\IInitialState;
35use OCP\IGroupManager;
36use OCP\IRequest;
37use OCP\IUser;
38use OCP\IUserManager;
39use OCP\IUserSession;
40use OCP\Share\IManager as IShareManager;
41use OCP\UserStatus\IManager as IUserStatusManager;
42
43class ProfilePageController extends Controller {
44	use \OC\Profile\TProfileHelper;
45
46	/** @var IInitialState */
47	private $initialStateService;
48
49	/** @var IAccountManager */
50	private $accountManager;
51
52	/** @var ProfileManager */
53	private $profileManager;
54
55	/** @var IShareManager */
56	private $shareManager;
57
58	/** @var IGroupManager */
59	private $groupManager;
60
61	/** @var KnownUserService */
62	private $knownUserService;
63
64	/** @var IUserManager */
65	private $userManager;
66
67	/** @var IUserSession */
68	private $userSession;
69
70	/** @var IUserStatusManager */
71	private $userStatusManager;
72
73	public function __construct(
74		$appName,
75		IRequest $request,
76		IInitialState $initialStateService,
77		IAccountManager $accountManager,
78		ProfileManager $profileManager,
79		IShareManager $shareManager,
80		IGroupManager $groupManager,
81		KnownUserService $knownUserService,
82		IUserManager $userManager,
83		IUserSession $userSession,
84		IUserStatusManager $userStatusManager
85	) {
86		parent::__construct($appName, $request);
87		$this->initialStateService = $initialStateService;
88		$this->accountManager = $accountManager;
89		$this->profileManager = $profileManager;
90		$this->shareManager = $shareManager;
91		$this->groupManager = $groupManager;
92		$this->knownUserService = $knownUserService;
93		$this->userManager = $userManager;
94		$this->userSession = $userSession;
95		$this->userStatusManager = $userStatusManager;
96	}
97
98	/**
99	 * @PublicPage
100	 * @NoCSRFRequired
101	 * @NoAdminRequired
102	 * @NoSubAdminRequired
103	 */
104	public function index(string $targetUserId): TemplateResponse {
105		$profileNotFoundTemplate = new TemplateResponse(
106			'core',
107			'404-profile',
108			[],
109			TemplateResponse::RENDER_AS_GUEST,
110		);
111
112		$targetUser = $this->userManager->get($targetUserId);
113		if (!$targetUser instanceof IUser) {
114			return $profileNotFoundTemplate;
115		}
116		$visitingUser = $this->userSession->getUser();
117		$targetAccount = $this->accountManager->getAccount($targetUser);
118
119		if (!$this->isProfileEnabled($targetAccount)) {
120			return $profileNotFoundTemplate;
121		}
122
123		// Run user enumeration checks only if viewing another user's profile
124		if ($targetUser !== $visitingUser) {
125			if (!$this->shareManager->currentUserCanEnumerateTargetUser($visitingUser, $targetUser)) {
126				return $profileNotFoundTemplate;
127			}
128		}
129
130		if ($visitingUser !== null) {
131			$userStatuses = $this->userStatusManager->getUserStatuses([$targetUserId]);
132			$status = $userStatuses[$targetUserId] ?? null;
133			if ($status !== null) {
134				$this->initialStateService->provideInitialState('status', [
135					'icon' => $status->getIcon(),
136					'message' => $status->getMessage(),
137				]);
138			}
139		}
140
141		$this->initialStateService->provideInitialState(
142			'profileParameters',
143			$this->profileManager->getProfileParams($targetUser, $visitingUser),
144		);
145
146		\OCP\Util::addScript('core', 'dist/profile');
147
148		return new TemplateResponse(
149			'core',
150			'profile',
151			[],
152			$this->userSession->isLoggedIn() ? TemplateResponse::RENDER_AS_USER : TemplateResponse::RENDER_AS_PUBLIC,
153		);
154	}
155}
156