Skip to content

Commit 834c9a2

Browse files
Merge pull request #41055 from nextcloud/feat/noid/profile-manager-ocp
feat(profile): Add public interface for profile manager so apps can check config
2 parents 06b3580 + fd9f7a7 commit 834c9a2

File tree

7 files changed

+132
-66
lines changed

7 files changed

+132
-66
lines changed

core/Controller/ProfilePageController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function index(string $targetUserId): TemplateResponse {
101101

102102
$this->initialStateService->provideInitialState(
103103
'profileParameters',
104-
$this->profileManager->getProfileParams($targetUser, $visitingUser),
104+
$this->profileManager->getProfileFields($targetUser, $visitingUser),
105105
);
106106

107107
$this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($targetUserId));

core/Db/ProfileConfig.php

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use function Safe\json_decode;
3030
use function Safe\json_encode;
3131
use \JsonSerializable;
32-
use OCP\Accounts\IAccountManager;
3332
use OCP\AppFramework\Db\Entity;
3433
use OCP\Profile\ParameterDoesNotExistException;
3534

@@ -40,53 +39,6 @@
4039
* @method void setConfig(string $config)
4140
*/
4241
class ProfileConfig extends Entity implements JsonSerializable {
43-
/**
44-
* Visible to users, guests, and public access
45-
*
46-
* @since 23.0.0
47-
*/
48-
public const VISIBILITY_SHOW = 'show';
49-
50-
/**
51-
* Visible to users and guests
52-
*
53-
* @since 23.0.0
54-
*/
55-
public const VISIBILITY_SHOW_USERS_ONLY = 'show_users_only';
56-
57-
/**
58-
* Visible to nobody
59-
*
60-
* @since 23.0.0
61-
*/
62-
public const VISIBILITY_HIDE = 'hide';
63-
64-
/**
65-
* Default account property visibility
66-
*
67-
* @since 23.0.0
68-
*/
69-
public const DEFAULT_PROPERTY_VISIBILITY = [
70-
IAccountManager::PROPERTY_ADDRESS => self::VISIBILITY_SHOW_USERS_ONLY,
71-
IAccountManager::PROPERTY_AVATAR => self::VISIBILITY_SHOW,
72-
IAccountManager::PROPERTY_BIOGRAPHY => self::VISIBILITY_SHOW,
73-
IAccountManager::PROPERTY_DISPLAYNAME => self::VISIBILITY_SHOW,
74-
IAccountManager::PROPERTY_HEADLINE => self::VISIBILITY_SHOW,
75-
IAccountManager::PROPERTY_ORGANISATION => self::VISIBILITY_SHOW,
76-
IAccountManager::PROPERTY_ROLE => self::VISIBILITY_SHOW,
77-
IAccountManager::PROPERTY_EMAIL => self::VISIBILITY_SHOW_USERS_ONLY,
78-
IAccountManager::PROPERTY_PHONE => self::VISIBILITY_SHOW_USERS_ONLY,
79-
IAccountManager::PROPERTY_TWITTER => self::VISIBILITY_SHOW,
80-
IAccountManager::PROPERTY_WEBSITE => self::VISIBILITY_SHOW,
81-
];
82-
83-
/**
84-
* Default visibility
85-
*
86-
* @since 23.0.0
87-
*/
88-
public const DEFAULT_VISIBILITY = self::VISIBILITY_SHOW_USERS_ONLY;
89-
9042
/** @var string */
9143
protected $userId;
9244

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,7 @@
552552
'OCP\\Preview\\IVersionedPreviewFile' => $baseDir . '/lib/public/Preview/IVersionedPreviewFile.php',
553553
'OCP\\Profile\\BeforeTemplateRenderedEvent' => $baseDir . '/lib/public/Profile/BeforeTemplateRenderedEvent.php',
554554
'OCP\\Profile\\ILinkAction' => $baseDir . '/lib/public/Profile/ILinkAction.php',
555+
'OCP\\Profile\\IProfileManager' => $baseDir . '/lib/public/Profile/IProfileManager.php',
555556
'OCP\\Profile\\ParameterDoesNotExistException' => $baseDir . '/lib/public/Profile/ParameterDoesNotExistException.php',
556557
'OCP\\Profiler\\IProfile' => $baseDir . '/lib/public/Profiler/IProfile.php',
557558
'OCP\\Profiler\\IProfiler' => $baseDir . '/lib/public/Profiler/IProfiler.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
585585
'OCP\\Preview\\IVersionedPreviewFile' => __DIR__ . '/../../..' . '/lib/public/Preview/IVersionedPreviewFile.php',
586586
'OCP\\Profile\\BeforeTemplateRenderedEvent' => __DIR__ . '/../../..' . '/lib/public/Profile/BeforeTemplateRenderedEvent.php',
587587
'OCP\\Profile\\ILinkAction' => __DIR__ . '/../../..' . '/lib/public/Profile/ILinkAction.php',
588+
'OCP\\Profile\\IProfileManager' => __DIR__ . '/../../..' . '/lib/public/Profile/IProfileManager.php',
588589
'OCP\\Profile\\ParameterDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Profile/ParameterDoesNotExistException.php',
589590
'OCP\\Profiler\\IProfile' => __DIR__ . '/../../..' . '/lib/public/Profiler/IProfile.php',
590591
'OCP\\Profiler\\IProfiler' => __DIR__ . '/../../..' . '/lib/public/Profiler/IProfiler.php',

lib/private/Profile/ProfileManager.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626

2727
namespace OC\Profile;
2828

29-
use function Safe\array_flip;
30-
use function Safe\usort;
29+
use OCP\Profile\IProfileManager;
30+
use function array_flip;
31+
use function usort;
3132
use OC\AppFramework\Bootstrap\Coordinator;
3233
use OC\Core\Db\ProfileConfig;
3334
use OC\Core\Db\ProfileConfigMapper;
@@ -49,7 +50,7 @@
4950
use Psr\Container\ContainerInterface;
5051
use Psr\Log\LoggerInterface;
5152

52-
class ProfileManager {
53+
class ProfileManager implements IProfileManager {
5354
/** @var ILinkAction[] */
5455
private array $actions = [];
5556

@@ -101,15 +102,15 @@ public function __construct(
101102
/**
102103
* If no user is passed as an argument return whether profile is enabled globally in `config.php`
103104
*/
104-
public function isProfileEnabled(?IUser $user = null): ?bool {
105+
public function isProfileEnabled(?IUser $user = null): bool {
105106
$profileEnabledGlobally = $this->config->getSystemValueBool('profile.enabled', true);
106107

107108
if (empty($user) || !$profileEnabledGlobally) {
108109
return $profileEnabledGlobally;
109110
}
110111

111112
$account = $this->accountManager->getAccount($user);
112-
return filter_var(
113+
return (bool) filter_var(
113114
$account->getProperty(IAccountManager::PROPERTY_PROFILE_ENABLED)->getValue(),
114115
FILTER_VALIDATE_BOOLEAN,
115116
FILTER_NULL_ON_FAILURE,
@@ -193,18 +194,18 @@ private function getActions(IUser $targetUser, ?IUser $visitingUser): array {
193194
* Return whether the profile parameter of the target user
194195
* is visible to the visiting user
195196
*/
196-
private function isParameterVisible(string $paramId, IUser $targetUser, ?IUser $visitingUser): bool {
197+
public function isProfileFieldVisible(string $profileField, IUser $targetUser, ?IUser $visitingUser): bool {
197198
try {
198199
$account = $this->accountManager->getAccount($targetUser);
199-
$scope = $account->getProperty($paramId)->getScope();
200+
$scope = $account->getProperty($profileField)->getScope();
200201
} catch (PropertyDoesNotExistException $e) {
201202
// Allow the exception as not all profile parameters are account properties
202203
}
203204

204-
$visibility = $this->getProfileConfig($targetUser, $visitingUser)[$paramId]['visibility'];
205+
$visibility = $this->getProfileConfig($targetUser, $visitingUser)[$profileField]['visibility'];
205206
// Handle profile visibility and account property scope
206207

207-
if ($visibility === ProfileConfig::VISIBILITY_SHOW_USERS_ONLY) {
208+
if ($visibility === self::VISIBILITY_SHOW_USERS_ONLY) {
208209
if (empty($scope)) {
209210
return $visitingUser !== null;
210211
}
@@ -218,10 +219,10 @@ private function isParameterVisible(string $paramId, IUser $targetUser, ?IUser $
218219
};
219220
}
220221

221-
if ($visibility === ProfileConfig::VISIBILITY_SHOW) {
222+
if ($visibility === self::VISIBILITY_SHOW) {
222223
if (empty($scope)) {
223224
return true;
224-
};
225+
}
225226

226227
return match ($scope) {
227228
IAccountManager::SCOPE_PRIVATE => $visitingUser !== null && $this->knownUserService->isKnownToUser($targetUser->getUID(), $visitingUser->getUID()),
@@ -238,8 +239,9 @@ private function isParameterVisible(string $paramId, IUser $targetUser, ?IUser $
238239
/**
239240
* Return the profile parameters of the target user that are visible to the visiting user
240241
* in an associative array
242+
* @return array{userId: string, address?: string|null, biography?: string|null, displayname?: string|null, headline?: string|null, isUserAvatarVisible?: bool, organisation?: string|null, role?: string|null, actions: list<array{id: string, icon: string, title: string, target: ?string}>}
241243
*/
242-
public function getProfileParams(IUser $targetUser, ?IUser $visitingUser): array {
244+
public function getProfileFields(IUser $targetUser, ?IUser $visitingUser): array {
243245
$account = $this->accountManager->getAccount($targetUser);
244246

245247
// Initialize associative array of profile parameters
@@ -257,14 +259,14 @@ public function getProfileParams(IUser $targetUser, ?IUser $visitingUser): array
257259
case IAccountManager::PROPERTY_ORGANISATION:
258260
case IAccountManager::PROPERTY_ROLE:
259261
$profileParameters[$property] =
260-
$this->isParameterVisible($property, $targetUser, $visitingUser)
262+
$this->isProfileFieldVisible($property, $targetUser, $visitingUser)
261263
// Explicitly set to null when value is empty string
262264
? ($account->getProperty($property)->getValue() ?: null)
263265
: null;
264266
break;
265267
case IAccountManager::PROPERTY_AVATAR:
266268
// Add avatar visibility
267-
$profileParameters['isUserAvatarVisible'] = $this->isParameterVisible($property, $targetUser, $visitingUser);
269+
$profileParameters['isUserAvatarVisible'] = $this->isProfileFieldVisible($property, $targetUser, $visitingUser);
268270
break;
269271
}
270272
}
@@ -284,7 +286,7 @@ function (ILinkAction $action) {
284286
array_filter(
285287
$this->getActions($targetUser, $visitingUser),
286288
function (ILinkAction $action) use ($targetUser, $visitingUser) {
287-
return $this->isParameterVisible($action->getId(), $targetUser, $visitingUser);
289+
return $this->isProfileFieldVisible($action->getId(), $targetUser, $visitingUser);
288290
}
289291
),
290292
)
@@ -316,12 +318,12 @@ private function getDefaultProfileConfig(IUser $targetUser, ?IUser $visitingUser
316318
// Construct the default config for actions
317319
$actionsConfig = [];
318320
foreach ($this->getActions($targetUser, $visitingUser) as $action) {
319-
$actionsConfig[$action->getId()] = ['visibility' => ProfileConfig::DEFAULT_VISIBILITY];
321+
$actionsConfig[$action->getId()] = ['visibility' => self::DEFAULT_VISIBILITY];
320322
}
321323

322324
// Construct the default config for account properties
323325
$propertiesConfig = [];
324-
foreach (ProfileConfig::DEFAULT_PROPERTY_VISIBILITY as $property => $visibility) {
326+
foreach (self::DEFAULT_PROPERTY_VISIBILITY as $property => $visibility) {
325327
$propertiesConfig[$property] = ['visibility' => $visibility];
326328
}
327329

lib/private/Server.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@
130130
use OC\Preview\GeneratorHelper;
131131
use OC\Preview\IMagickSupport;
132132
use OC\Preview\MimeIconProvider;
133+
use OC\Profile\ProfileManager;
133134
use OC\Remote\Api\ApiFactory;
134135
use OC\Remote\InstanceFactory;
135136
use OC\RichObjectStrings\Validator;
@@ -235,6 +236,7 @@
235236
use OCP\Mail\IMailer;
236237
use OCP\OCM\IOCMDiscoveryService;
237238
use OCP\OCM\IOCMProvider;
239+
use OCP\Profile\IProfileManager;
238240
use OCP\Remote\Api\IApiFactory;
239241
use OCP\Remote\IInstanceFactory;
240242
use OCP\RichObjectStrings\IValidator;
@@ -1434,6 +1436,8 @@ public function __construct($webRoot, \OC\Config $config) {
14341436

14351437
$this->registerAlias(ISetupCheckManager::class, SetupCheckManager::class);
14361438

1439+
$this->registerAlias(IProfileManager::class, ProfileManager::class);
1440+
14371441
$this->connectDispatcher();
14381442
}
14391443

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Joas Schilling <[email protected]>
7+
*
8+
* @author Joas Schilling <[email protected]>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCP\Profile;
28+
29+
use OCP\Accounts\IAccountManager;
30+
use OCP\IUser;
31+
32+
/**
33+
* @since 28.0.0
34+
*/
35+
interface IProfileManager {
36+
/**
37+
* Visible to users, guests, and public access
38+
*
39+
* @since 28.0.0
40+
*/
41+
public const VISIBILITY_SHOW = 'show';
42+
43+
/**
44+
* Visible to users and guests
45+
*
46+
* @since 28.0.0
47+
*/
48+
public const VISIBILITY_SHOW_USERS_ONLY = 'show_users_only';
49+
50+
/**
51+
* Visible to nobody
52+
*
53+
* @since 28.0.0
54+
*/
55+
public const VISIBILITY_HIDE = 'hide';
56+
57+
/**
58+
* Default account property visibility
59+
*
60+
* @since 28.0.0
61+
*/
62+
public const DEFAULT_PROPERTY_VISIBILITY = [
63+
IAccountManager::PROPERTY_ADDRESS => self::VISIBILITY_SHOW_USERS_ONLY,
64+
IAccountManager::PROPERTY_AVATAR => self::VISIBILITY_SHOW,
65+
IAccountManager::PROPERTY_BIOGRAPHY => self::VISIBILITY_SHOW,
66+
IAccountManager::PROPERTY_DISPLAYNAME => self::VISIBILITY_SHOW,
67+
IAccountManager::PROPERTY_HEADLINE => self::VISIBILITY_SHOW,
68+
IAccountManager::PROPERTY_ORGANISATION => self::VISIBILITY_SHOW,
69+
IAccountManager::PROPERTY_ROLE => self::VISIBILITY_SHOW,
70+
IAccountManager::PROPERTY_EMAIL => self::VISIBILITY_SHOW_USERS_ONLY,
71+
IAccountManager::PROPERTY_PHONE => self::VISIBILITY_SHOW_USERS_ONLY,
72+
IAccountManager::PROPERTY_TWITTER => self::VISIBILITY_SHOW,
73+
IAccountManager::PROPERTY_WEBSITE => self::VISIBILITY_SHOW,
74+
];
75+
76+
/**
77+
* Default visibility
78+
*
79+
* @since 28.0.0
80+
*/
81+
public const DEFAULT_VISIBILITY = self::VISIBILITY_SHOW_USERS_ONLY;
82+
83+
/**
84+
* If no user is passed as an argument return whether profile is enabled globally in `config.php`
85+
*
86+
* @since 28.0.0
87+
*/
88+
public function isProfileEnabled(?IUser $user = null): bool;
89+
90+
/**
91+
* Return whether the profile parameter of the target user
92+
* is visible to the visiting user
93+
*
94+
* @since 28.0.0
95+
*/
96+
public function isProfileFieldVisible(string $profileField, IUser $targetUser, ?IUser $visitingUser): bool;
97+
98+
/**
99+
* Return the profile parameters of the target user that are visible to the visiting user
100+
* in an associative array
101+
*
102+
* @return array{userId: string, address?: ?string, biography?: ?string, displayname?: ?string, headline?: ?string, isUserAvatarVisible?: bool, organisation?: ?string, role?: ?string, actions: list<array{id: string, icon: string, title: string, target: ?string}>}
103+
* @since 28.0.0
104+
*/
105+
public function getProfileFields(IUser $targetUser, ?IUser $visitingUser): array;
106+
}

0 commit comments

Comments
 (0)