Skip to content

Commit

Permalink
Allow to tweak default scopes for accounts
Browse files Browse the repository at this point in the history
Close #6582

Signed-off-by: Thomas Citharel <[email protected]>
  • Loading branch information
tcitworld committed Apr 26, 2020
1 parent 0eeb660 commit 6e430b3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
7 changes: 7 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => []
];
47 changes: 32 additions & 15 deletions lib/private/Accounts/AccountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 17 additions & 1 deletion lib/public/Accounts/IAccountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
8 changes: 7 additions & 1 deletion tests/lib/Accounts/AccountsManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @author Björn Schießle <[email protected]>
* @author Thomas Citharel <[email protected]>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @license AGPL-3.0
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}
Expand Down

0 comments on commit 6e430b3

Please sign in to comment.