Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -3100,6 +3100,16 @@
*/
'account_manager.default_property_scope' => [],

/*
* Overrides default visibility for profile data. Valid properties and visibilities
* are defined in ``OCP\Accounts\IAccountManager`` and ``OCP\Profile\IProfileManager``.
* Values are merged with defaults from there.
*
* Example: Set phone property to hidden visibility:
* ``[\OCP\Accounts\IAccountManager::PROPERTY_PHONE => \OCP\Profile\IProfileManager::VISIBILITY_HIDE]``
*/
'profile_manager.default_property_visibility' => [],

/**
* Enable the deprecated Projects feature, superseded by Related Resources since
* Nextcloud 25.
Expand Down
18 changes: 17 additions & 1 deletion lib/private/Profile/ProfileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ class ProfileManager implements IProfileManager {
IAccountManager::PROPERTY_PRONOUNS,
];

/**
* Array of allowed visibility values
*/
private const array ALLOWED_VISIBILITY = [
self::VISIBILITY_HIDE,
self::VISIBILITY_SHOW_USERS_ONLY,
self::VISIBILITY_SHOW,
];

public function __construct(
private IAccountManager $accountManager,
private IAppManager $appManager,
Expand Down Expand Up @@ -319,9 +328,16 @@ private function getDefaultProfileConfig(IUser $targetUser, ?IUser $visitingUser
}

// Construct the default config for account properties
$configDefaultVisibility = $this->config->getSystemValue('profile_manager.default_property_visibility', []);
$propertiesConfig = [];
foreach (self::DEFAULT_PROPERTY_VISIBILITY as $property => $visibility) {
$this->applyDefaultProfilePreset($property, $visibility);
if (isset($configDefaultVisibility[$property])
&& in_array($configDefaultVisibility[$property], self::ALLOWED_VISIBILITY, true)) {
$visibility = $configDefaultVisibility[$property];
} else {
$this->applyDefaultProfilePreset($property, $visibility);
}

$propertiesConfig[$property] = ['visibility' => $visibility];
}

Expand Down
156 changes: 156 additions & 0 deletions tests/lib/Profile/ProfileManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Test\Profile;

use OC\AppFramework\Bootstrap\Coordinator;
use OC\Config\PresetManager;
use OC\Core\Db\ProfileConfigMapper;
use OC\KnownUser\KnownUserService;
use OC\Profile\ProfileManager;
use OCP\Accounts\IAccountManager;
use OCP\App\IAppManager;
use OCP\Config\Lexicon\Preset;
use OCP\IConfig;
use OCP\IUser;
use OCP\L10N\IFactory;
use OCP\Profile\IProfileManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Test\TestCase;

/**
* Class ProfileManagerTest
*
* @package Test\Profile
*/
class ProfileManagerTest extends TestCase {

private IAccountManager&MockObject $accountManager;
private IAppManager&MockObject $appManager;
private IConfig&MockObject $config;
private ProfileConfigMapper&MockObject $configMapper;
private ContainerInterface&MockObject $container;
private KnownUserService&MockObject $knownUserService;
private IFactory&MockObject $l10nFactory;
private LoggerInterface&MockObject $logger;
private Coordinator&MockObject $coordinator;
private PresetManager&MockObject $presetManager;

private ProfileManager $profileManager;

#[\Override]
protected function setUp(): void {
parent::setUp();

$this->accountManager = $this->createMock(IAccountManager::class);
$this->appManager = $this->createMock(IAppManager::class);
$this->config = $this->createMock(IConfig::class);
$this->configMapper = $this->createMock(ProfileConfigMapper::class);
$this->container = $this->createMock(ContainerInterface::class);
$this->knownUserService = $this->createMock(KnownUserService::class);
$this->l10nFactory = $this->createMock(IFactory::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->coordinator = $this->createMock(Coordinator::class);
$this->presetManager = $this->createMock(PresetManager::class);

$this->profileManager = new ProfileManager(
$this->accountManager,
$this->appManager,
$this->config,
$this->configMapper,
$this->container,
$this->knownUserService,
$this->l10nFactory,
$this->logger,
$this->coordinator,
$this->presetManager
);
}

public static function dataSetDefaultPropertyScopes(): array {
return [
[
[], # returns just IProfileManager::DEFAULT_PROPERTY_VISIBILITY
[
IAccountManager::PROPERTY_ADDRESS => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_AVATAR => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_BIOGRAPHY => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_DISPLAYNAME => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_HEADLINE => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_ORGANISATION => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_ROLE => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_EMAIL => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_PHONE => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_TWITTER => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_BLUESKY => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_WEBSITE => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_PRONOUNS => IProfileManager::VISIBILITY_SHOW,
]
],
[
[ # should overwrite their respective defaults
IAccountManager::PROPERTY_AVATAR => IProfileManager::VISIBILITY_HIDE,
IAccountManager::PROPERTY_ROLE => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
],
[
IAccountManager::PROPERTY_ADDRESS => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_AVATAR => IProfileManager::VISIBILITY_HIDE,
IAccountManager::PROPERTY_BIOGRAPHY => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_DISPLAYNAME => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_HEADLINE => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_ORGANISATION => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_ROLE => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_EMAIL => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_PHONE => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_TWITTER => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_BLUESKY => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_WEBSITE => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_PRONOUNS => IProfileManager::VISIBILITY_SHOW,
]
],
[
[
IAccountManager::PROPERTY_BLUESKY => 'invalid', # should fall back to default
],
[
IAccountManager::PROPERTY_ADDRESS => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_AVATAR => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_BIOGRAPHY => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_DISPLAYNAME => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_HEADLINE => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_ORGANISATION => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_ROLE => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_EMAIL => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_PHONE => IProfileManager::VISIBILITY_SHOW_USERS_ONLY,
IAccountManager::PROPERTY_TWITTER => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_BLUESKY => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_WEBSITE => IProfileManager::VISIBILITY_SHOW,
IAccountManager::PROPERTY_PRONOUNS => IProfileManager::VISIBILITY_SHOW,
]
],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('dataSetDefaultPropertyScopes')]
public function testSetDefaultPropertyVisibility(array $propertyVisibilities, array $expected): void {
$targetUser = $this->createMock(IUser::class);
$this->invokePrivate($this->profileManager, 'sortedActions', [[]]);
$this->presetManager->method('getLexiconPreset')->willReturn(Preset::NONE);
$this->config->expects($this->once())->method('getSystemValue')->with('profile_manager.default_property_visibility', [])->willReturn($propertyVisibilities);

$result = $this->invokePrivate($this->profileManager, 'getDefaultProfileConfig', [$targetUser, null]);

foreach ($expected as $key => $expectedValue) {
$resultVisibility = $result[$key]['visibility'];
$this->assertEquals($expectedValue, $resultVisibility, "The result visibility doesn't follow the value set into the config or defaults correctly.");
}
}
}