From 6e430b37320df80eb6f4c4f18eede3521f69c7c0 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sun, 26 Apr 2020 13:03:32 +0200 Subject: [PATCH] Allow to tweak default scopes for accounts Close #6582 Signed-off-by: Thomas Citharel --- config/config.sample.php | 7 ++++ lib/private/Accounts/AccountManager.php | 47 +++++++++++++++------- lib/public/Accounts/IAccountManager.php | 18 ++++++++- tests/lib/Accounts/AccountsManagerTest.php | 8 +++- 4 files changed, 63 insertions(+), 17 deletions(-) diff --git a/config/config.sample.php b/config/config.sample.php index 268838a1768cb..8176312522015 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -1805,4 +1805,11 @@ */ 'login_form_autocomplete' => true, + +/** + * Allows to override the default visibility scopes for Account data. + * The list of properties overridable and the visibilities possible values are in + * OCP\Accounts\IAccountManager. Default values are in OC\Accounts\AccountManager + */ +'account_manager_default_property_scope' => [] ]; diff --git a/lib/private/Accounts/AccountManager.php b/lib/private/Accounts/AccountManager.php index 8b0cb972c5989..eac8bcd2c9b5e 100644 --- a/lib/private/Accounts/AccountManager.php +++ b/lib/private/Accounts/AccountManager.php @@ -34,6 +34,7 @@ use OCP\Accounts\IAccount; use OCP\Accounts\IAccountManager; use OCP\BackgroundJob\IJobList; +use OCP\IConfig; use OCP\IDBConnection; use OCP\ILogger; use OCP\IUser; @@ -67,21 +68,29 @@ class AccountManager implements IAccountManager { /** @var ILogger */ private $logger; - /** - * AccountManager constructor. - * - * @param IDBConnection $connection - * @param EventDispatcherInterface $eventDispatcher - * @param IJobList $jobList - */ + /** @var IConfig */ + private $config; + + public const DEFAULT_SCOPE_VALUES = [ + self::PROPERTY_DISPLAYNAME => self::VISIBILITY_CONTACTS_ONLY, + self::PROPERTY_ADDRESS => self::VISIBILITY_PRIVATE, + self::PROPERTY_WEBSITE => self::VISIBILITY_PRIVATE, + self::PROPERTY_EMAIL => self::VISIBILITY_CONTACTS_ONLY, + self::PROPERTY_AVATAR => self::VISIBILITY_CONTACTS_ONLY, + self::PROPERTY_PHONE => self::VISIBILITY_PRIVATE, + self::PROPERTY_TWITTER => self::VISIBILITY_PRIVATE + ]; + public function __construct(IDBConnection $connection, EventDispatcherInterface $eventDispatcher, IJobList $jobList, - ILogger $logger) { + ILogger $logger, + IConfig $config) { $this->connection = $connection; $this->eventDispatcher = $eventDispatcher; $this->jobList = $jobList; $this->logger = $logger; + $this->config = $config; } /** @@ -298,50 +307,58 @@ protected function updateExistingUser(IUser $user, $data) { * @return array */ protected function buildDefaultUserRecord(IUser $user) { + $override = $this->config->getSystemValue('account_manager_default_property_scope', []); return [ self::PROPERTY_DISPLAYNAME => [ 'value' => $user->getDisplayName(), - 'scope' => self::VISIBILITY_CONTACTS_ONLY, + 'scope' => $this->getOverrideValueOrDefault($override,self::PROPERTY_DISPLAYNAME), 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_ADDRESS => [ 'value' => '', - 'scope' => self::VISIBILITY_PRIVATE, + 'scope' => $this->getOverrideValueOrDefault($override, self::PROPERTY_ADDRESS), 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_WEBSITE => [ 'value' => '', - 'scope' => self::VISIBILITY_PRIVATE, + 'scope' => $this->getOverrideValueOrDefault($override, self::PROPERTY_WEBSITE), 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_EMAIL => [ 'value' => $user->getEMailAddress(), - 'scope' => self::VISIBILITY_CONTACTS_ONLY, + 'scope' => $this->getOverrideValueOrDefault($override, self::PROPERTY_EMAIL), 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_AVATAR => [ - 'scope' => self::VISIBILITY_CONTACTS_ONLY + 'scope' => $this->getOverrideValueOrDefault($override, self::PROPERTY_AVATAR) ], self::PROPERTY_PHONE => [ 'value' => '', - 'scope' => self::VISIBILITY_PRIVATE, + 'scope' => $this->getOverrideValueOrDefault($override, self::PROPERTY_PHONE), 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_TWITTER => [ 'value' => '', - 'scope' => self::VISIBILITY_PRIVATE, + 'scope' => $this->getOverrideValueOrDefault($override, self::PROPERTY_TWITTER), 'verified' => self::NOT_VERIFIED, ], ]; } + private function getOverrideValueOrDefault(array $override, string $property): string { + if (in_array($property, self::PROPERTIES_LIST, true) && isset($override[$property]) && in_array($override[$property], self::VISIBILITIES_LIST, true)) { + return $override[$property]; + } + return self::DEFAULT_SCOPE_VALUES[$property]; + } + private function parseAccountData(IUser $user, $data): Account { $account = new Account($user); foreach ($data as $property => $accountData) { diff --git a/lib/public/Accounts/IAccountManager.php b/lib/public/Accounts/IAccountManager.php index 23af0f8b0b18b..fa595cde6f10f 100644 --- a/lib/public/Accounts/IAccountManager.php +++ b/lib/public/Accounts/IAccountManager.php @@ -41,9 +41,15 @@ interface IAccountManager { public const VISIBILITY_PRIVATE = 'private'; /** only contacts, especially trusted servers can see my contact details */ public const VISIBILITY_CONTACTS_ONLY = 'contacts'; - /** every body ca see my contact detail, will be published to the lookup server */ + /** everybody can see my contact details, will be published to the lookup server */ public const VISIBILITY_PUBLIC = 'public'; + public const VISIBILITIES_LIST = [ + self::VISIBILITY_PRIVATE, + self::VISIBILITY_CONTACTS_ONLY, + self::VISIBILITY_PUBLIC + ]; + public const PROPERTY_AVATAR = 'avatar'; public const PROPERTY_DISPLAYNAME = 'displayname'; public const PROPERTY_PHONE = 'phone'; @@ -52,6 +58,16 @@ interface IAccountManager { public const PROPERTY_ADDRESS = 'address'; public const PROPERTY_TWITTER = 'twitter'; + public const PROPERTIES_LIST = [ + self::PROPERTY_AVATAR, + self::PROPERTY_DISPLAYNAME, + self::PROPERTY_PHONE, + self::PROPERTY_EMAIL, + self::PROPERTY_WEBSITE, + self::PROPERTY_ADDRESS, + self::PROPERTY_TWITTER + ]; + public const NOT_VERIFIED = '0'; public const VERIFICATION_IN_PROGRESS = '1'; public const VERIFIED = '2'; diff --git a/tests/lib/Accounts/AccountsManagerTest.php b/tests/lib/Accounts/AccountsManagerTest.php index 76abeb5b49576..eb826cbce2866 100644 --- a/tests/lib/Accounts/AccountsManagerTest.php +++ b/tests/lib/Accounts/AccountsManagerTest.php @@ -1,6 +1,7 @@ + * @author Thomas Citharel * * @copyright Copyright (c) 2016, ownCloud, Inc. * @license AGPL-3.0 @@ -25,6 +26,7 @@ use OC\Accounts\AccountManager; use OCP\Accounts\IAccountManager; use OCP\BackgroundJob\IJobList; +use OCP\IConfig; use OCP\ILogger; use OCP\IUser; use PHPUnit\Framework\MockObject\MockObject; @@ -55,12 +57,16 @@ class AccountsManagerTest extends TestCase { /** @var ILogger|MockObject */ private $logger; + /** @var IConfig|MockObject */ + private $config; + protected function setUp(): void { parent::setUp(); $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); $this->connection = \OC::$server->getDatabaseConnection(); $this->jobList = $this->createMock(IJobList::class); $this->logger = $this->createMock(ILogger::class); + $this->config = $this->createMock(IConfig::class); } protected function tearDown(): void { @@ -77,7 +83,7 @@ protected function tearDown(): void { */ public function getInstance($mockedMethods = null) { return $this->getMockBuilder(AccountManager::class) - ->setConstructorArgs([$this->connection, $this->eventDispatcher, $this->jobList, $this->logger]) + ->setConstructorArgs([$this->connection, $this->eventDispatcher, $this->jobList, $this->logger, $this->config]) ->setMethods($mockedMethods) ->getMock(); }