Issues (2553)

apps/files_external/lib/Config/UserContext.php (1 issue)

1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Julius Härtl <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Julius Härtl <[email protected]>
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 OCA\Files_External\Config;
26
27
use OCP\IRequest;
28
use OCP\IUser;
29
use OCP\IUserManager;
30
use OCP\IUserSession;
31
use OCP\Share\Exceptions\ShareNotFound;
32
use OCP\Share\IManager as ShareManager;
33
34
class UserContext {
35
36
	/** @var IUserSession */
37
	private $session;
38
39
	/** @var ShareManager */
40
	private $shareManager;
41
42
	/** @var IRequest */
43
	private $request;
44
45
	/** @var string */
46
	private $userId;
47
48
	/** @var IUserManager */
49
	private $userManager;
50
51
	public function __construct(IUserSession $session, ShareManager $manager, IRequest $request, IUserManager $userManager) {
52
		$this->session = $session;
53
		$this->shareManager = $manager;
54
		$this->request = $request;
55
		$this->userManager = $userManager;
56
	}
57
58
	public function getSession(): IUserSession {
59
		return $this->session;
60
	}
61
62
	public function setUserId(string $userId): void {
63
		$this->userId = $userId;
64
	}
65
66
	protected function getUserId(): ?string {
67
		if ($this->userId !== null) {
68
			return $this->userId;
69
		}
70
		if ($this->session && $this->session->getUser() !== null) {
71
			return $this->session->getUser()->getUID();
72
		}
73
		try {
74
			$shareToken = $this->request->getParam('token');
75
			$share = $this->shareManager->getShareByToken($shareToken);
76
			return $share->getShareOwner();
77
		} catch (ShareNotFound $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
78
		}
79
80
		return null;
81
	}
82
83
	protected function getUser(): ?IUser {
84
		$userId = $this->getUserId();
85
		if ($userId !== null) {
86
			return $this->userManager->get($userId);
87
		}
88
		return null;
89
	}
90
}
91