|
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) {} |
|
|
|
|
|
|
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
|
|
|
|