Skip to content

Commit 686282e

Browse files
committed
feat(profilepicker): check fields visibility in reference provider
Signed-off-by: Julien Veyssier <[email protected]>
1 parent 4fb561f commit 686282e

File tree

2 files changed

+69
-17
lines changed

2 files changed

+69
-17
lines changed

lib/Reference/ProfilePickerReferenceProvider.php

+25-14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\IL10N;
1919
use OCP\IURLGenerator;
2020
use OCP\IUserManager;
21+
use OCP\Profile\IProfileManager;
2122

2223
class ProfilePickerReferenceProvider extends ADiscoverableReferenceProvider {
2324
public const RICH_OBJECT_TYPE = 'users_picker_profile';
@@ -27,6 +28,7 @@ public function __construct(
2728
private IURLGenerator $urlGenerator,
2829
private IUserManager $userManager,
2930
private IAccountManager $accountManager,
31+
private IProfileManager $profileManager,
3032
private ?string $userId,
3133
) {
3234
}
@@ -85,14 +87,25 @@ public function resolveReference(string $referenceText): ?IReference {
8587
return null;
8688
}
8789

90+
$currentUser = $this->userManager->get($this->userId);
91+
8892
$reference = new Reference($referenceText);
8993

9094
$userDisplayName = $user->getDisplayName();
9195
$userEmail = $user->getEMailAddress();
9296
$userAvatarUrl = $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $userId, 'size' => '64']);
9397

94-
$bio = $account->getProperty(IAccountManager::PROPERTY_BIOGRAPHY);
95-
$bio = $bio->getScope() !== IAccountManager::SCOPE_PRIVATE ? $bio->getValue() : null;
98+
$bioProperty = $account->getProperty(IAccountManager::PROPERTY_BIOGRAPHY);
99+
$bio = null;
100+
$fullBio = null;
101+
if ($this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_BIOGRAPHY, $user, $currentUser)) {
102+
$fullBio = $bioProperty->getValue();
103+
$bio = $fullBio !== ''
104+
? (mb_strlen($fullBio) > 80
105+
? (mb_substr($fullBio, 0, 80) . '...')
106+
: $fullBio)
107+
: null;
108+
}
96109
$headline = $account->getProperty(IAccountManager::PROPERTY_HEADLINE);
97110
$location = $account->getProperty(IAccountManager::PROPERTY_ADDRESS);
98111
$website = $account->getProperty(IAccountManager::PROPERTY_WEBSITE);
@@ -104,6 +117,8 @@ public function resolveReference(string $referenceText): ?IReference {
104117
$reference->setDescription($userEmail ?? $userDisplayName);
105118
$reference->setImageUrl($userAvatarUrl);
106119

120+
$isLocationVisible = $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_ADDRESS, $user, $currentUser);
121+
107122
// for the Vue reference widget
108123
$reference->setRichObject(
109124
self::RICH_OBJECT_TYPE,
@@ -112,18 +127,14 @@ public function resolveReference(string $referenceText): ?IReference {
112127
'title' => $userDisplayName,
113128
'subline' => $userEmail ?? $userDisplayName,
114129
'email' => $userEmail,
115-
'bio' => isset($bio) && $bio !== ''
116-
? (mb_strlen($bio) > 80
117-
? (mb_substr($bio, 0, 80) . '...')
118-
: $bio)
119-
: null,
120-
'full_bio' => $bio,
121-
'headline' => $headline->getScope() !== IAccountManager::SCOPE_PRIVATE ? $headline->getValue() : null,
122-
'location' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $location->getValue() : null,
123-
'location_url' => $location->getScope() !== IAccountManager::SCOPE_PRIVATE ? $this->getOpenStreetLocationUrl($location->getValue()) : null,
124-
'website' => $website->getScope() !== IAccountManager::SCOPE_PRIVATE ? $website->getValue() : null,
125-
'organisation' => $organisation->getScope() !== IAccountManager::SCOPE_PRIVATE ? $organisation->getValue() : null,
126-
'role' => $role->getScope() !== IAccountManager::SCOPE_PRIVATE ? $role->getValue() : null,
130+
'bio' => $bio,
131+
'full_bio' => $fullBio,
132+
'headline' => $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_HEADLINE, $user, $currentUser) ? $headline->getValue() : null,
133+
'location' => $isLocationVisible ? $location->getValue() : null,
134+
'location_url' => $isLocationVisible ? $this->getOpenStreetLocationUrl($location->getValue()) : null,
135+
'website' => $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_WEBSITE, $user, $currentUser) ? $website->getValue() : null,
136+
'organisation' => $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_ORGANISATION, $user, $currentUser) ? $organisation->getValue() : null,
137+
'role' => $this->profileManager->isProfileFieldVisible(IAccountManager::PROPERTY_ROLE, $user, $currentUser) ? $role->getValue() : null,
127138
'url' => $referenceText,
128139
]
129140
);

tests/unit/Reference/ProfilePickerReferenceProviderTest.php

+44-3
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
use OCP\IURLGenerator;
1818
use OCP\IUser;
1919
use OCP\IUserManager;
20+
use OCP\Profile\IProfileManager;
2021
use PHPUnit\Framework\MockObject\MockObject;
2122

2223
class ProfilePickerReferenceProviderTest extends TestCase {
2324
private string $userId = 'admin';
25+
private IUser|MockObject $adminUser;
2426
private IL10N|MockObject $l10n;
2527
private IURLGenerator|MockObject $urlGenerator;
2628
private IUserManager|MockObject $userManager;
2729
private IAccountManager|MockObject $accountManager;
30+
private IProfileManager|MockObject $profileManager;
2831
private ProfilePickerReferenceProvider $referenceProvider;
2932

3033
private array $testUsersData = [
@@ -46,60 +49,74 @@ class ProfilePickerReferenceProviderTest extends TestCase {
4649
'user1' => [
4750
IAccountManager::PROPERTY_BIOGRAPHY => [
4851
'scope' => IAccountManager::SCOPE_PRIVATE,
52+
'visible' => true,
4953
'value' => 'This is a first test user',
5054
],
5155
IAccountManager::PROPERTY_HEADLINE => [
5256
'scope' => IAccountManager::SCOPE_LOCAL,
57+
'visible' => false,
5358
'value' => 'I\'m a first test user',
5459
],
5560
IAccountManager::PROPERTY_ADDRESS => [
5661
'scope' => IAccountManager::SCOPE_LOCAL,
62+
'visible' => true,
5763
'value' => 'Odessa',
5864
],
5965
IAccountManager::PROPERTY_WEBSITE => [
6066
'scope' => IAccountManager::SCOPE_LOCAL,
67+
'visible' => true,
6168
'value' => 'https://domain.co/testuser1',
6269
],
6370
IAccountManager::PROPERTY_ORGANISATION => [
6471
'scope' => IAccountManager::SCOPE_PRIVATE,
72+
'visible' => true,
6573
'value' => 'Nextcloud GmbH',
6674
],
6775
IAccountManager::PROPERTY_ROLE => [
6876
'scope' => IAccountManager::SCOPE_LOCAL,
77+
'visible' => true,
6978
'value' => 'Non-existing user',
7079
],
7180
IAccountManager::PROPERTY_PROFILE_ENABLED => [
7281
'scope' => IAccountManager::SCOPE_LOCAL,
82+
'visible' => true,
7383
'value' => '1',
7484
],
7585
],
7686
'user2' => [
7787
IAccountManager::PROPERTY_BIOGRAPHY => [
7888
'scope' => IAccountManager::SCOPE_LOCAL,
89+
'visible' => true,
7990
'value' => 'This is a test user',
8091
],
8192
IAccountManager::PROPERTY_HEADLINE => [
8293
'scope' => IAccountManager::SCOPE_LOCAL,
94+
'visible' => true,
8395
'value' => 'Second test user',
8496
],
8597
IAccountManager::PROPERTY_ADDRESS => [
8698
'scope' => IAccountManager::SCOPE_LOCAL,
99+
'visible' => true,
87100
'value' => 'Berlin',
88101
],
89102
IAccountManager::PROPERTY_WEBSITE => [
90103
'scope' => IAccountManager::SCOPE_LOCAL,
104+
'visible' => true,
91105
'value' => 'https://domain.co/testuser2',
92106
],
93107
IAccountManager::PROPERTY_ORGANISATION => [
94108
'scope' => IAccountManager::SCOPE_PRIVATE,
109+
'visible' => true,
95110
'value' => 'Nextcloud GmbH',
96111
],
97112
IAccountManager::PROPERTY_ROLE => [
98113
'scope' => IAccountManager::SCOPE_LOCAL,
114+
'visible' => true,
99115
'value' => 'Non-existing user',
100116
],
101117
IAccountManager::PROPERTY_PROFILE_ENABLED => [
102118
'scope' => IAccountManager::SCOPE_LOCAL,
119+
'visible' => true,
103120
'value' => '1',
104121
],
105122
],
@@ -120,22 +137,40 @@ public function setUp(): void {
120137
$this->urlGenerator = $this->createMock(IURLGenerator::class);
121138
$this->userManager = $this->createMock(IUserManager::class);
122139
$this->accountManager = $this->createMock(IAccountManager::class);
140+
$this->profileManager = $this->createMock(IProfileManager::class);
123141

124142
$this->referenceProvider = new ProfilePickerReferenceProvider(
125143
$this->l10n,
126144
$this->urlGenerator,
127145
$this->userManager,
128146
$this->accountManager,
147+
$this->profileManager,
129148
$this->userId
130149
);
131150

132151
$this->urlGenerator->expects($this->any())
133152
->method('getBaseUrl')
134153
->willReturn($this->baseUrl);
154+
155+
$this->profileManager->expects($this->any())
156+
->method('isProfileFieldVisible')
157+
->willReturnCallback(function (string $profileField, IUser $targetUser, ?IUser $visitingUser) {
158+
return $this->testAccountsData[$targetUser->getUID()][$profileField]['visible']
159+
&& $this->testAccountsData[$targetUser->getUID()][$profileField]['scope'] !== IAccountManager::SCOPE_PRIVATE;
160+
});
161+
162+
$this->adminUser = $this->createMock(IUser::class);
163+
$this->adminUser->expects($this->any())
164+
->method('getUID')
165+
->willReturn('admin');
166+
$this->adminUser->expects($this->any())
167+
->method('getDisplayName')
168+
->willReturn('admin');
135169
}
136170

137171
private function getTestAccountPropertyValue(string $testUserId, string $property): mixed {
138-
if ($this->testAccountsData[$testUserId][$property]['scope'] === IAccountManager::SCOPE_PRIVATE) {
172+
if (!$this->testAccountsData[$testUserId][$property]['visible']
173+
|| $this->testAccountsData[$testUserId][$property]['scope'] === IAccountManager::SCOPE_PRIVATE) {
139174
return null;
140175
}
141176
return $this->testAccountsData[$testUserId][$property]['value'];
@@ -163,8 +198,14 @@ private function setupUserAccountReferenceExpectation(string $userId): ?IReferen
163198

164199
$this->userManager->expects($this->any())
165200
->method('get')
166-
->with($userId)
167-
->willReturn($user);
201+
->willReturnCallback(function (string $uid) use ($user, $userId) {
202+
if ($uid === $userId) {
203+
return $user;
204+
} elseif ($uid === 'admin') {
205+
return $this->adminUser;
206+
}
207+
return null;
208+
});
168209

169210
// setup account expectations
170211
$account = $this->createMock(IAccount::class);

0 commit comments

Comments
 (0)