Skip to content

Commit a8bdf1e

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

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
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
}
@@ -74,6 +76,8 @@ public function resolveReference(string $referenceText): ?IReference {
7476
return null;
7577
}
7678

79+
$currentUser = $this->userManager->get($this->userId);
80+
7781
$userId = $this->getObjectId($referenceText);
7882
$user = $this->userManager->get($userId);
7983
if ($user === null) {
@@ -91,8 +95,17 @@ public function resolveReference(string $referenceText): ?IReference {
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

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
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 {
@@ -25,6 +26,7 @@ class ProfilePickerReferenceProviderTest extends TestCase {
2526
private IURLGenerator|MockObject $urlGenerator;
2627
private IUserManager|MockObject $userManager;
2728
private IAccountManager|MockObject $accountManager;
29+
private IProfileManager|MockObject $profileManager;
2830
private ProfilePickerReferenceProvider $referenceProvider;
2931

3032
private array $testUsersData = [
@@ -120,12 +122,14 @@ public function setUp(): void {
120122
$this->urlGenerator = $this->createMock(IURLGenerator::class);
121123
$this->userManager = $this->createMock(IUserManager::class);
122124
$this->accountManager = $this->createMock(IAccountManager::class);
125+
$this->profileManager = $this->createMock(IProfileManager::class);
123126

124127
$this->referenceProvider = new ProfilePickerReferenceProvider(
125128
$this->l10n,
126129
$this->urlGenerator,
127130
$this->userManager,
128131
$this->accountManager,
132+
$this->profileManager,
129133
$this->userId
130134
);
131135

0 commit comments

Comments
 (0)