|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\Activity; |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
use OCP\IRequest; |
|
26
|
|
|
use OCP\IUser; |
|
27
|
|
|
use OCP\IUserSession; |
|
28
|
|
|
use OCP\Share; |
|
29
|
|
|
use OCP\Share\Exceptions\ShareNotFound; |
|
30
|
|
|
use OCP\Share\IManager; |
|
31
|
|
|
|
|
32
|
|
|
class CurrentUser { |
|
33
|
|
|
|
|
34
|
|
|
/** @var IUserSession */ |
|
35
|
|
|
protected $userSession; |
|
36
|
|
|
/** @var IRequest */ |
|
37
|
|
|
protected $request; |
|
38
|
|
|
/** @var IManager */ |
|
39
|
|
|
protected $shareManager; |
|
40
|
|
|
|
|
41
|
|
|
/** @var string */ |
|
42
|
|
|
protected $identifier; |
|
43
|
|
|
/** @var string|null */ |
|
44
|
|
|
protected $cloudId; |
|
45
|
|
|
/** @var string|false|null */ |
|
46
|
|
|
protected $sessionUser; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param IUserSession $userSession |
|
50
|
|
|
* @param IRequest $request |
|
51
|
|
|
* @param IManager $shareManager |
|
52
|
|
|
*/ |
|
53
|
36 |
|
public function __construct(IUserSession $userSession, IRequest $request, IManager $shareManager) { |
|
54
|
36 |
|
$this->userSession = $userSession; |
|
55
|
36 |
|
$this->request = $request; |
|
56
|
36 |
|
$this->shareManager = $shareManager; |
|
57
|
36 |
|
$this->cloudId = false; |
|
|
|
|
|
|
58
|
36 |
|
$this->sessionUser = false; |
|
59
|
36 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get an identifier for the user, session or token |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function getUserIdentifier() { |
|
66
|
4 |
|
if ($this->identifier === null) { |
|
67
|
3 |
|
$this->identifier = $this->getUID(); |
|
68
|
|
|
|
|
69
|
3 |
|
if ($this->identifier === null) { |
|
70
|
2 |
|
$this->identifier = $this->getCloudIDFromToken(); |
|
71
|
|
|
|
|
72
|
2 |
|
if ($this->identifier === null) { |
|
73
|
|
|
// Nothing worked, fallback to empty string |
|
74
|
1 |
|
$this->identifier = ''; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
return $this->identifier; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the current user from the session |
|
84
|
|
|
* @return string|null |
|
85
|
|
|
*/ |
|
86
|
23 |
|
public function getUID() { |
|
87
|
23 |
|
if ($this->sessionUser === false) { |
|
88
|
23 |
|
$user = $this->userSession->getUser(); |
|
89
|
23 |
|
if ($user instanceof IUser) { |
|
|
|
|
|
|
90
|
2 |
|
$this->sessionUser = (string) $user->getUID(); |
|
91
|
|
|
} else { |
|
92
|
21 |
|
$this->sessionUser = null; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
23 |
|
return $this->sessionUser; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Get the current user from the session |
|
101
|
|
|
* @return string|null |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getCloudId() { |
|
104
|
|
|
if ($this->cloudId === false) { |
|
105
|
|
|
$user = $this->userSession->getUser(); |
|
106
|
|
|
if ($user instanceof IUser) { |
|
|
|
|
|
|
107
|
|
|
$this->cloudId = (string) $user->getCloudId(); |
|
108
|
|
|
} else { |
|
109
|
|
|
$this->cloudId = $this->getCloudIDFromToken(); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $this->cloudId; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the cloud ID from the sharing token |
|
118
|
|
|
* @return string|null |
|
119
|
|
|
*/ |
|
120
|
4 |
|
protected function getCloudIDFromToken() { |
|
121
|
4 |
|
if (!empty($this->request->server['PHP_AUTH_USER'])) { |
|
122
|
3 |
|
$token = $this->request->server['PHP_AUTH_USER']; |
|
123
|
|
|
try { |
|
124
|
3 |
|
$share = $this->shareManager->getShareByToken($token); |
|
125
|
2 |
|
if ($share->getShareType() === Share::SHARE_TYPE_REMOTE) { |
|
126
|
1 |
|
return $share->getSharedWith(); |
|
127
|
|
|
} |
|
128
|
1 |
|
} catch (ShareNotFound $e) { |
|
|
|
|
|
|
129
|
|
|
// No share, use the fallback |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
3 |
|
return null; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..