Repository navigation
Expand file tree
/
Copy pathCurrentUser.php
More file actions
158 lines (130 loc) · 3.84 KB
/
Copy pathCurrentUser.php
File metadata and controls
158 lines (130 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Activity;
use OCP\Activity\IManager as IActivityManager;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IShare;
class CurrentUser {
private bool $publicShareLoaded = false;
private ?IShare $publicShare = null;
public function __construct(
protected readonly IUserSession $userSession,
protected readonly IRequest $request,
protected readonly IManager $shareManager,
protected readonly IFactory $l10nFactory,
protected readonly IActivityManager $activityManager,
) {
}
public function getUser(): ?IUser {
return $this->userSession->getUser();
}
/**
* Get an identifier for the user, session or token
*/
public function getUserIdentifier(): string {
$uid = $this->getUID();
if ($uid !== null) {
return $uid;
}
$cloudId = $this->getCloudIDFromToken();
if ($cloudId !== null) {
return $cloudId;
}
$nickname = htmlspecialchars($this->request->getHeader('X-NC-Nickname'));
if ($nickname !== '') {
return $nickname . ' (' . $this->l10nFactory->get('comments')->t('remote user') . ')';
}
// Nothing worked, fallback to empty string
return '';
}
/**
* Get the current user id
*
* Apps can override who an action is attributed to with
* IManager::setCurrentUserId(). That is the only way to name an actor when the
* action happens outside of that user's session, e.g. from a background job.
* Without an override the manager reads the session, and on a request without
* a session the owner of the activity feed token.
*/
public function getUID(): ?string {
try {
$userId = $this->activityManager->getCurrentUserId();
} catch (\UnexpectedValueException) {
// No override, no session and no valid feed token
return null;
}
return $userId === '' ? null : $userId;
}
/**
* Get the current user cloud id from the session
*/
public function getCloudId(): ?string {
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
return $user->getCloudId();
}
return $this->getCloudIDFromToken();
}
/**
* Check if the current request is via a public share link
*/
public function isPublicShareToken(): bool {
return $this->getPublicShare() !== null;
}
/**
* Get the cloud ID from the sharing token
*/
protected function getCloudIDFromToken(): ?string {
$share = $this->getPublicShare();
if ($share === null || $share->getShareType() !== IShare::TYPE_REMOTE) {
return null;
}
return $share->getSharedWith();
}
protected function getPublicShare(): ?IShare {
if (!$this->publicShareLoaded) {
$this->publicShare = $this->loadPublicShare();
$this->publicShareLoaded = true;
}
return $this->publicShare;
}
private function loadPublicShare(): ?IShare {
if (basename($this->request->getScriptName()) !== 'public.php') {
return null;
}
$token = $this->getShareToken();
if ($token === null) {
return null;
}
try {
return $this->shareManager->getShareByToken($token);
} catch (ShareNotFound) {
return null;
}
}
protected function getShareToken(): ?string {
// The legacy public endpoint receive the share token in the HTTP basic auth header.
/** @psalm-suppress NoInterfaceProperties */
$authUser = (string)($this->request->server['PHP_AUTH_USER'] ?? '');
if ($authUser !== '') {
return $authUser;
}
// The current public endpoint receives the share token in the path.
// Copied from apps/dav/lib/Connector/Sabre/PublicAuth::getToken()
$path = $this->request->getPathInfo() ?: '';
// ['', 'dav', 'files', 'token']
$splittedPath = explode('/', $path);
if (count($splittedPath) < 4 || $splittedPath[3] === '') {
return null;
}
return $splittedPath[3];
}
}