Skip to content

Commit c41f839

Browse files
committed
use a lazy user for the file owner when listing a directory
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 9a76f06 commit c41f839

4 files changed

Lines changed: 159 additions & 8 deletions

File tree

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,6 +1542,7 @@
15421542
'OC\\User\\Backend' => $baseDir . '/lib/private/User/Backend.php',
15431543
'OC\\User\\Database' => $baseDir . '/lib/private/User/Database.php',
15441544
'OC\\User\\DisplayNameCache' => $baseDir . '/lib/private/User/DisplayNameCache.php',
1545+
'OC\\User\\LazyUser' => $baseDir . '/lib/private/User/LazyUser.php',
15451546
'OC\\User\\LoginException' => $baseDir . '/lib/private/User/LoginException.php',
15461547
'OC\\User\\Manager' => $baseDir . '/lib/private/User/Manager.php',
15471548
'OC\\User\\NoUserException' => $baseDir . '/lib/private/User/NoUserException.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
15711571
'OC\\User\\Backend' => __DIR__ . '/../../..' . '/lib/private/User/Backend.php',
15721572
'OC\\User\\Database' => __DIR__ . '/../../..' . '/lib/private/User/Database.php',
15731573
'OC\\User\\DisplayNameCache' => __DIR__ . '/../../..' . '/lib/private/User/DisplayNameCache.php',
1574+
'OC\\User\\LazyUser' => __DIR__ . '/../../..' . '/lib/private/User/LazyUser.php',
15741575
'OC\\User\\LoginException' => __DIR__ . '/../../..' . '/lib/private/User/LoginException.php',
15751576
'OC\\User\\Manager' => __DIR__ . '/../../..' . '/lib/private/User/Manager.php',
15761577
'OC\\User\\NoUserException' => __DIR__ . '/../../..' . '/lib/private/User/NoUserException.php',

lib/private/Files/View.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
use Icewind\Streams\CallbackWrapper;
5050
use OC\Files\Mount\MoveableMount;
5151
use OC\Files\Storage\Storage;
52+
use OC\User\DisplayNameCache;
53+
use OC\User\LazyUser;
5254
use OC\User\User;
5355
use OCA\Files_Sharing\SharedMount;
5456
use OCP\Constants;
@@ -102,6 +104,8 @@ class View {
102104
/** @var \OCP\ILogger */
103105
private $logger;
104106

107+
private DisplayNameCache $displayNameCache;
108+
105109
/**
106110
* @param string $root
107111
* @throws \Exception If $root contains an invalid path
@@ -118,6 +122,7 @@ public function __construct($root = '') {
118122
$this->lockingProvider = \OC::$server->getLockingProvider();
119123
$this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
120124
$this->userManager = \OC::$server->getUserManager();
125+
$this->displayNameCache = \OC::$server->get(DisplayNameCache::class);
121126
$this->logger = \OC::$server->getLogger();
122127
}
123128

@@ -1312,15 +1317,10 @@ public function hasUpdated($path, $time) {
13121317

13131318
/**
13141319
* @param string $ownerId
1315-
* @return \OC\User\User
1320+
* @return IUser
13161321
*/
1317-
private function getUserObjectForOwner($ownerId) {
1318-
$owner = $this->userManager->get($ownerId);
1319-
if ($owner instanceof IUser) {
1320-
return $owner;
1321-
} else {
1322-
return new User($ownerId, null, \OC::$server->getEventDispatcher());
1323-
}
1322+
private function getUserObjectForOwner(string $ownerId) {
1323+
return new LazyUser($ownerId, $this->displayNameCache, $this->userManager);
13241324
}
13251325

13261326
/**

lib/private/User/LazyUser.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2022 Robin Appelman <robin@icewind.nl>
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 OC\User;
25+
26+
use OCP\IUser;
27+
use OCP\IUserManager;
28+
29+
class LazyUser implements IUser {
30+
private ?IUser $user = null;
31+
private DisplayNameCache $displayNameCache;
32+
private string $uid;
33+
private IUserManager $userManager;
34+
35+
public function __construct(string $uid, DisplayNameCache $displayNameCache, IUserManager $userManager) {
36+
$this->displayNameCache = $displayNameCache;
37+
$this->uid = $uid;
38+
$this->userManager = $userManager;
39+
}
40+
41+
private function getUser(): IUser {
42+
if ($this->user === null) {
43+
$this->user = $this->userManager->get($this->uid);
44+
}
45+
return $this->user;
46+
}
47+
48+
public function getUID() {
49+
return $this->uid;
50+
}
51+
52+
public function getDisplayName() {
53+
return $this->displayNameCache->getDisplayName($this->uid);
54+
}
55+
56+
public function setDisplayName($displayName) {
57+
return $this->getUser()->setDisplayName($displayName);
58+
}
59+
60+
public function getLastLogin() {
61+
return $this->getUser()->getLastLogin();
62+
}
63+
64+
public function updateLastLoginTimestamp() {
65+
return $this->getUser()->updateLastLoginTimestamp();
66+
}
67+
68+
public function delete() {
69+
return $this->getUser()->delete();
70+
}
71+
72+
public function setPassword($password, $recoveryPassword = null) {
73+
return $this->getUser()->setPassword($password, $recoveryPassword);
74+
}
75+
76+
public function getHome() {
77+
return $this->getUser()->getHome();
78+
}
79+
80+
public function getBackendClassName() {
81+
return $this->getUser()->getBackendClassName();
82+
}
83+
84+
public function getBackend() {
85+
return $this->getUser()->getBackend();
86+
}
87+
88+
public function canChangeAvatar() {
89+
return $this->getUser()->canChangeAvatar();
90+
}
91+
92+
public function canChangePassword() {
93+
return $this->getUser()->canChangePassword();
94+
}
95+
96+
public function canChangeDisplayName() {
97+
return $this->getUser()->canChangeDisplayName();
98+
}
99+
100+
public function isEnabled() {
101+
return $this->getUser()->isEnabled();
102+
}
103+
104+
public function setEnabled(bool $enabled = true) {
105+
return $this->getUser()->setEnabled($enabled);
106+
}
107+
108+
public function getEMailAddress() {
109+
return $this->getUser()->getEMailAddress();
110+
}
111+
112+
public function getSystemEMailAddress(): ?string {
113+
return $this->getUser()->getSystemEMailAddress();
114+
}
115+
116+
public function getPrimaryEMailAddress(): ?string {
117+
return $this->getUser()->getPrimaryEMailAddress();
118+
}
119+
120+
public function getAvatarImage($size) {
121+
return $this->getUser()->getAvatarImage($size);
122+
}
123+
124+
public function getCloudId() {
125+
return $this->getUser()->getCloudId();
126+
}
127+
128+
public function setEMailAddress($mailAddress) {
129+
$this->getUser()->setEMailAddress($mailAddress);
130+
}
131+
132+
public function setSystemEMailAddress(string $mailAddress): void {
133+
$this->getUser()->setSystemEMailAddress($mailAddress);
134+
}
135+
136+
public function setPrimaryEMailAddress(string $mailAddress): void {
137+
$this->getUser()->setPrimaryEMailAddress($mailAddress);
138+
}
139+
140+
public function getQuota() {
141+
return $this->getUser()->getQuota();
142+
}
143+
144+
public function setQuota($quota) {
145+
$this->getUser()->setQuota($quota);
146+
}
147+
148+
149+
}

0 commit comments

Comments
 (0)