Skip to content

Commit b3d6ce3

Browse files
authored
Merge pull request #32772 from nextcloud/backport/32697/stable22
[stable22] Fix get avatar authorization
2 parents a7f32cd + 312a208 commit b3d6ce3

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

lib/private/Avatar/AvatarManager.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,23 @@ public function getAvatar(string $userId) : IAvatar {
135135
$avatarScope = '';
136136
}
137137

138-
if (
138+
switch ($avatarScope) {
139139
// v2-private scope hides the avatar from public access and from unknown users
140-
$avatarScope === IAccountManager::SCOPE_PRIVATE
141-
&& (
142-
// accessing from public link
143-
$requestingUser === null
144-
// logged in, but unknown to user
145-
|| !$this->knownUserService->isKnownToUser($requestingUser->getUID(), $userId)
146-
)) {
147-
// use a placeholder avatar which caches the generated images
148-
return new PlaceholderAvatar($folder, $user, $this->logger);
140+
case IAccountManager::SCOPE_PRIVATE:
141+
if ($requestingUser !== null && $this->knownUserService->isKnownToUser($requestingUser->getUID(), $userId)) {
142+
return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
143+
}
144+
break;
145+
case IAccountManager::SCOPE_LOCAL:
146+
case IAccountManager::SCOPE_FEDERATED:
147+
case IAccountManager::SCOPE_PUBLISHED:
148+
return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
149+
default:
150+
// use a placeholder avatar which caches the generated images
151+
return new PlaceholderAvatar($folder, $user, $this->logger);
149152
}
150153

151-
return new UserAvatar($folder, $this->l, $user, $this->logger, $this->config);
154+
return new PlaceholderAvatar($folder, $user, $this->logger);
152155
}
153156

154157
/**

tests/lib/Avatar/AvatarManagerTest.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,33 +161,56 @@ public function testGetAvatarValidUserDifferentCasing() {
161161
->method('getUID')
162162
->willReturn('valid-user');
163163

164+
$this->userSession->expects($this->once())
165+
->method('getUser')
166+
->willReturn($user);
167+
164168
$folder = $this->createMock(ISimpleFolder::class);
165169
$this->appData
166170
->expects($this->once())
167171
->method('getFolder')
168172
->with('valid-user')
169173
->willReturn($folder);
170174

175+
$account = $this->createMock(IAccount::class);
176+
$this->accountManager->expects($this->once())
177+
->method('getAccount')
178+
->with($user)
179+
->willReturn($account);
180+
181+
$property = $this->createMock(IAccountProperty::class);
182+
$account->expects($this->once())
183+
->method('getProperty')
184+
->with(IAccountManager::PROPERTY_AVATAR)
185+
->willReturn($property);
186+
187+
$property->expects($this->once())
188+
->method('getScope')
189+
->willReturn(IAccountManager::SCOPE_FEDERATED);
190+
171191
$expected = new UserAvatar($folder, $this->l10n, $user, $this->logger, $this->config);
172192
$this->assertEquals($expected, $this->avatarManager->getAvatar('vaLid-USER'));
173193
}
174194

175-
public function knownUnknownProvider() {
195+
public function dataGetAvatarScopes() {
176196
return [
177-
[IAccountManager::SCOPE_LOCAL, false, false, false],
178-
[IAccountManager::SCOPE_LOCAL, true, false, false],
179-
180197
// public access cannot see real avatar
181198
[IAccountManager::SCOPE_PRIVATE, true, false, true],
182199
// unknown users cannot see real avatar
183200
[IAccountManager::SCOPE_PRIVATE, false, false, true],
184201
// known users can see real avatar
185202
[IAccountManager::SCOPE_PRIVATE, false, true, false],
203+
[IAccountManager::SCOPE_LOCAL, false, false, false],
204+
[IAccountManager::SCOPE_LOCAL, true, false, false],
205+
[IAccountManager::SCOPE_FEDERATED, false, false, false],
206+
[IAccountManager::SCOPE_FEDERATED, true, false, false],
207+
[IAccountManager::SCOPE_PUBLISHED, false, false, false],
208+
[IAccountManager::SCOPE_PUBLISHED, true, false, false],
186209
];
187210
}
188211

189212
/**
190-
* @dataProvider knownUnknownProvider
213+
* @dataProvider dataGetAvatarScopes
191214
*/
192215
public function testGetAvatarScopes($avatarScope, $isPublicCall, $isKnownUser, $expectedPlaceholder) {
193216
if ($isPublicCall) {

0 commit comments

Comments
 (0)