Skip to content

Commit e12ffb3

Browse files
committed
feat(profile): Add public interface for profile manager so apps can check config
Signed-off-by: Joas Schilling <[email protected]>
1 parent 6114364 commit e12ffb3

File tree

7 files changed

+138
-31
lines changed

7 files changed

+138
-31
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: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
namespace OC\Core\Db;
2828

29+
use OCP\Profile\IProfileManager;
2930
use function Safe\json_decode;
3031
use function Safe\json_encode;
3132
use \JsonSerializable;
32-
use OCP\Accounts\IAccountManager;
3333
use OCP\AppFramework\Db\Entity;
3434
use OCP\Profile\ParameterDoesNotExistException;
3535

@@ -44,48 +44,41 @@ class ProfileConfig extends Entity implements JsonSerializable {
4444
* Visible to users, guests, and public access
4545
*
4646
* @since 23.0.0
47+
* @deprecated 28.0.0 Use {@see IProfileManager::VISIBILITY_SHOW} instead
4748
*/
48-
public const VISIBILITY_SHOW = 'show';
49+
public const VISIBILITY_SHOW = IProfileManager::VISIBILITY_SHOW;
4950

5051
/**
5152
* Visible to users and guests
5253
*
5354
* @since 23.0.0
55+
* @deprecated 28.0.0 Use {@see IProfileManager::VISIBILITY_SHOW_USERS_ONLY} instead
5456
*/
55-
public const VISIBILITY_SHOW_USERS_ONLY = 'show_users_only';
57+
public const VISIBILITY_SHOW_USERS_ONLY = IProfileManager::VISIBILITY_SHOW_USERS_ONLY;
5658

5759
/**
5860
* Visible to nobody
5961
*
6062
* @since 23.0.0
63+
* @deprecated 28.0.0 Use {@see IProfileManager::VISIBILITY_HIDE} instead
6164
*/
62-
public const VISIBILITY_HIDE = 'hide';
65+
public const VISIBILITY_HIDE = IProfileManager::VISIBILITY_HIDE;
6366

6467
/**
6568
* Default account property visibility
6669
*
6770
* @since 23.0.0
71+
* @deprecated 28.0.0 Use {@see IProfileManager::DEFAULT_PROPERTY_VISIBILITY} instead
6872
*/
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-
];
73+
public const DEFAULT_PROPERTY_VISIBILITY = IProfileManager::DEFAULT_PROPERTY_VISIBILITY;
8274

8375
/**
8476
* Default visibility
8577
*
8678
* @since 23.0.0
79+
* @deprecated 28.0.0 Use {@see IProfileManager::DEFAULT_VISIBILITY} instead
8780
*/
88-
public const DEFAULT_VISIBILITY = self::VISIBILITY_SHOW_USERS_ONLY;
81+
public const DEFAULT_VISIBILITY = IProfileManager::DEFAULT_VISIBILITY;
8982

9083
/** @var string */
9184
protected $userId;

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: 14 additions & 12 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,7 +102,7 @@ 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) {
@@ -193,15 +194,15 @@ 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

207208
if ($visibility === ProfileConfig::VISIBILITY_SHOW_USERS_ONLY) {
@@ -221,7 +222,7 @@ private function isParameterVisible(string $paramId, IUser $targetUser, ?IUser $
221222
if ($visibility === ProfileConfig::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<string, 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
)

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<string, 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)