From f4fe762859f383d6163a90918d8873367f4e07cf 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 | 43 ++++++++++++++-------- lib/public/Accounts/IAccountManager.php | 19 +++++++++- tests/lib/Accounts/AccountsManagerTest.php | 8 +++- 4 files changed, 60 insertions(+), 17 deletions(-) diff --git a/config/config.sample.php b/config/config.sample.php index 268838a1768cb..09d6909171a68 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 overridable properties and valid values for visibility 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..c48d2996fc1e3 100644 --- a/lib/private/Accounts/AccountManager.php +++ b/lib/private/Accounts/AccountManager.php @@ -11,6 +11,7 @@ * @author Julius Härtl * @author Morris Jobke * @author Roeland Jago Douma + * @author Thomas Citharel * * @license AGPL-3.0 * @@ -34,6 +35,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 +69,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,45 +308,48 @@ protected function updateExistingUser(IUser $user, $data) { * @return array */ protected function buildDefaultUserRecord(IUser $user) { + $scopes = array_merge(self::DEFAULT_SCOPE_VALUES, array_filter($this->config->getSystemValue('account_manager_default_property_scope', []), function (string $scope) { + return in_array($scope, self::VISIBILITIES_LIST, true); + })); return [ self::PROPERTY_DISPLAYNAME => [ 'value' => $user->getDisplayName(), - 'scope' => self::VISIBILITY_CONTACTS_ONLY, + 'scope' => $scopes[self::PROPERTY_DISPLAYNAME], 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_ADDRESS => [ 'value' => '', - 'scope' => self::VISIBILITY_PRIVATE, + 'scope' => $scopes[self::PROPERTY_ADDRESS], 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_WEBSITE => [ 'value' => '', - 'scope' => self::VISIBILITY_PRIVATE, + 'scope' => $scopes[self::PROPERTY_WEBSITE], 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_EMAIL => [ 'value' => $user->getEMailAddress(), - 'scope' => self::VISIBILITY_CONTACTS_ONLY, + 'scope' => $scopes[self::PROPERTY_EMAIL], 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_AVATAR => [ - 'scope' => self::VISIBILITY_CONTACTS_ONLY + 'scope' => $scopes[self::PROPERTY_AVATAR], ], self::PROPERTY_PHONE => [ 'value' => '', - 'scope' => self::VISIBILITY_PRIVATE, + 'scope' => $scopes[self::PROPERTY_PHONE], 'verified' => self::NOT_VERIFIED, ], self::PROPERTY_TWITTER => [ 'value' => '', - 'scope' => self::VISIBILITY_PRIVATE, + 'scope' => $scopes[self::PROPERTY_TWITTER], 'verified' => self::NOT_VERIFIED, ], ]; diff --git a/lib/public/Accounts/IAccountManager.php b/lib/public/Accounts/IAccountManager.php index 23af0f8b0b18b..fbc0eddafa797 100644 --- a/lib/public/Accounts/IAccountManager.php +++ b/lib/public/Accounts/IAccountManager.php @@ -6,6 +6,7 @@ * @copyright Copyright (c) 2018 Julius Härtl * * @author Julius Härtl + * @author Thomas Citharel * * @license GNU AGPL version 3 or any later version * @@ -41,9 +42,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 +59,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(); }