Passed
Push — master ( 67f90c...bc4091 )
by Blizzz
09:54 queued 10s
created

UserContext::setUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Files_External\Config;
25
26
use OCP\IRequest;
27
use OCP\IUser;
28
use OCP\IUserManager;
29
use OCP\IUserSession;
30
use OCP\Share\Exceptions\ShareNotFound;
31
use OCP\Share\IManager as ShareManager;
32
33
class UserContext {
34
35
	/** @var IUserSession */
36
	private $session;
37
38
	/** @var ShareManager */
39
	private $shareManager;
40
41
	/** @var IRequest */
42
	private $request;
43
44
	/** @var string */
45
	private $userId;
46
47
	/** @var IUserManager */
48
	private $userManager;
49
50
	public function __construct(IUserSession $session, ShareManager $manager, IRequest $request, IUserManager $userManager) {
51
		$this->session = $session;
52
		$this->shareManager = $manager;
53
		$this->request = $request;
54
		$this->userManager = $userManager;
55
	}
56
57
	public function getSession(): IUserSession {
58
		return $this->session;
59
	}
60
61
	public function setUserId(string $userId): void {
62
		$this->userId = $userId;
63
	}
64
65
	protected function getUserId(): ?string {
66
		if ($this->userId !== null) {
67
			return $this->userId;
68
		}
69
		if($this->session && $this->session->getUser() !== null) {
70
			return $this->session->getUser()->getUID();
71
		}
72
		try {
73
			$shareToken = $this->request->getParam('token');
74
			$share = $this->shareManager->getShareByToken($shareToken);
75
			return $share->getShareOwner();
76
		} catch (ShareNotFound $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
77
78
		return null;
79
	}
80
81
	protected function getUser(): ?IUser {
82
		$userId = $this->getUserId();
83
		if($userId !== null) {
84
			return $this->userManager->get($userId);
85
		}
86
		return null;
87
	}
88
89
}
90