Compare commits

...

10 Commits

Author SHA1 Message Date
Christopher Ng e4e8e36af1 [WIP] Vuetiful email section
Signed-off-by: Christopher Ng <chrng8@gmail.com>
2021-06-04 02:46:39 +00:00
Arthur Schiwon 8235d90ecc
psalm happiness
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 21:55:57 +02:00
Arthur Schiwon 8ec640d14a
adjust property type declaration
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 21:07:59 +02:00
Arthur Schiwon 2701c3e7dc
fix code style
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 20:54:25 +02:00
Arthur Schiwon 44827e37c0
allow interacting with IAccountPropertyCollections
- in fact the API could be done in a nicer way and it might be possible to
  work without IAccountPropertyCollection, but only with the
  IAccountProperties.
- To keep it simple at first and not overengineer the blunt attempt is
  followed
- If necessary helpful in the further cause of development adjustements or
  extensions can be done quickly with this base


Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 20:49:49 +02:00
Arthur Schiwon 0bade27479
add IAccountPropertyCollection with implementation
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 20:49:49 +02:00
Arthur Schiwon afea57352b
update unit tests
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 20:49:49 +02:00
Arthur Schiwon 839bff1641
deal with property collections when fetching users (with update)
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 20:49:49 +02:00
Arthur Schiwon 956bfba2e2
refactor validators and sanitizers
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 20:49:48 +02:00
Arthur Schiwon fb79350d3e
extend AccountManager API with collection const
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
2021-06-03 20:49:42 +02:00
17 changed files with 954 additions and 81 deletions

View File

@ -0,0 +1,108 @@
<!--
- @copyright 2021 Christopher Ng <chrng8@gmail.com>
-
- @author 2021 Christopher Ng <chrng8@gmail.com>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<form id="emailform" class="section">
<h3>
<label for="email">{{ t('settings', 'Emails') }}</label>
<a href="#" class="federation-menu" :aria-label="t('settings', 'Change privacy level of email')">
<span class="icon-federation-menu icon-password">
<span class="icon-triangle-s"></span>
</span>
</a>
<Actions>
<ActionButton icon="icon-add" @click.stop.prevent="addEmails(email, additionalEmails)">
{{ t('settings', 'Add email address') }}
</ActionButton>
</Actions>
</h3>
<input
type="email"
name="email"
id="email"
v-model="email"
:placeholder="t('settings', 'Your email address')" />
<input
v-for="(email, index) in additionalEmails"
type="email"
name="additionalEmail[]"
:id="`additionalEmail-${index}`"
v-model="email.value"
:placeholder="t('settings', `Additional email address ${index+1}`)"
:key="index" />
<input type="hidden" id="emailscope" value="emailScope">
</form>
</template>
<script>
import { Actions, ActionButton } from '@nextcloud/vue'
import axios from '@nextcloud/axios'
import * as auth from '@nextcloud/auth'
import * as router from '@nextcloud/router'
export default {
name: 'EmailSection',
components: {
Actions,
ActionButton,
},
props: {
initialEmails: {
type: Array,
required: true,
},
},
data() {
/* eslint-disable */
console.log(this.initialEmails)
return {
email: this.initialEmails[0],
additionalEmails: this.initialEmails.slice(1).map(email => ({ value: email })),
}
},
methods: {
async addEmails(email, additionalEmails) {
const userId = auth.getCurrentUser().uid
// TODO upgrade @nextcloud/router to v2.0 so we can remove the .slice() trailing slash hack
const url = router.generateOcsUrl(`cloud/users/${userId}`, 2).slice(0, -1)
// Set the primary email
const res = await axios.put(url, {
key: 'email',
value: email,
})
// console.log(res.data)
// Set additional emails
const resp = await axios.put(url, {
key: 'additional_mail',
value: additionalEmails.map(({ value }) => value),
})
// console.log(res.data)
additionalEmails.push({ value: '' })
}
}
}
</script>
<style scoped>
</style>

View File

@ -0,0 +1,39 @@
/**
* @copyright 2021, Christopher Ng <chrng8@gmail.com>
*
* @author Christopher Ng <chrng8@gmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import Vue from 'vue'
import { loadState } from '@nextcloud/initial-state'
import EmailSection from './components/PersonalInfo/EmailSection'
// eslint-disable-next-line camelcase
__webpack_nonce__ = btoa(OC.requestToken)
Vue.prototype.t = t
const View = Vue.extend(EmailSection)
new View({
propsData: {
initialEmails: loadState('settings', 'emails'),
// ...more initial props
},
}).$mount('#vue-emailform')

View File

@ -31,6 +31,7 @@ script('settings', [
'federationsettingsview', 'federationsettingsview',
'federationscopemenu', 'federationscopemenu',
'settings/personalInfo', 'settings/personalInfo',
'vue-settings-personal-info',
]); ]);
?> ?>
@ -125,6 +126,9 @@ script('settings', [
<input type="hidden" id="displaynamescope" value="<?php p($_['displayNameScope']) ?>"> <input type="hidden" id="displaynamescope" value="<?php p($_['displayNameScope']) ?>">
</form> </form>
</div> </div>
<div class="personal-settings-setting-box">
<div id="vue-emailform" class="section"></div>
</div>
<div class="personal-settings-setting-box"> <div class="personal-settings-setting-box">
<form id="emailform" class="section"> <form id="emailform" class="section">
<h3> <h3>

View File

@ -32,6 +32,7 @@ module.exports = {
'settings-personal-security': path.join(__dirname, 'src', 'main-personal-security'), 'settings-personal-security': path.join(__dirname, 'src', 'main-personal-security'),
'settings-personal-webauthn': path.join(__dirname, 'src', 'main-personal-webauth'), 'settings-personal-webauthn': path.join(__dirname, 'src', 'main-personal-webauth'),
'settings-nextcloud-pdf': path.join(__dirname, 'src', 'main-nextcloud-pdf'), 'settings-nextcloud-pdf': path.join(__dirname, 'src', 'main-nextcloud-pdf'),
'settings-personal-info': path.join(__dirname, 'src', 'main-personal-info'),
}, },
output: { output: {
path: path.resolve(__dirname, './js'), path: path.resolve(__dirname, './js'),

View File

@ -1769,9 +1769,6 @@
</TooManyArguments> </TooManyArguments>
</file> </file>
<file src="apps/lookup_server_connector/lib/BackgroundJobs/RetryJob.php"> <file src="apps/lookup_server_connector/lib/BackgroundJobs/RetryJob.php">
<InvalidArrayOffset occurrences="1">
<code>$publicData[IAccountManager::PROPERTY_DISPLAYNAME]['value']</code>
</InvalidArrayOffset>
<InvalidScalarArgument occurrences="1"> <InvalidScalarArgument occurrences="1">
<code>$this-&gt;retries + 1</code> <code>$this-&gt;retries + 1</code>
</InvalidScalarArgument> </InvalidScalarArgument>

View File

@ -10,6 +10,7 @@ return array(
'OCP\\Accounts\\IAccount' => $baseDir . '/lib/public/Accounts/IAccount.php', 'OCP\\Accounts\\IAccount' => $baseDir . '/lib/public/Accounts/IAccount.php',
'OCP\\Accounts\\IAccountManager' => $baseDir . '/lib/public/Accounts/IAccountManager.php', 'OCP\\Accounts\\IAccountManager' => $baseDir . '/lib/public/Accounts/IAccountManager.php',
'OCP\\Accounts\\IAccountProperty' => $baseDir . '/lib/public/Accounts/IAccountProperty.php', 'OCP\\Accounts\\IAccountProperty' => $baseDir . '/lib/public/Accounts/IAccountProperty.php',
'OCP\\Accounts\\IAccountPropertyCollection' => $baseDir . '/lib/public/Accounts/IAccountPropertyCollection.php',
'OCP\\Accounts\\PropertyDoesNotExistException' => $baseDir . '/lib/public/Accounts/PropertyDoesNotExistException.php', 'OCP\\Accounts\\PropertyDoesNotExistException' => $baseDir . '/lib/public/Accounts/PropertyDoesNotExistException.php',
'OCP\\Activity\\ActivitySettings' => $baseDir . '/lib/public/Activity/ActivitySettings.php', 'OCP\\Activity\\ActivitySettings' => $baseDir . '/lib/public/Activity/ActivitySettings.php',
'OCP\\Activity\\IConsumer' => $baseDir . '/lib/public/Activity/IConsumer.php', 'OCP\\Activity\\IConsumer' => $baseDir . '/lib/public/Activity/IConsumer.php',
@ -581,7 +582,9 @@ return array(
'OC\\Accounts\\Account' => $baseDir . '/lib/private/Accounts/Account.php', 'OC\\Accounts\\Account' => $baseDir . '/lib/private/Accounts/Account.php',
'OC\\Accounts\\AccountManager' => $baseDir . '/lib/private/Accounts/AccountManager.php', 'OC\\Accounts\\AccountManager' => $baseDir . '/lib/private/Accounts/AccountManager.php',
'OC\\Accounts\\AccountProperty' => $baseDir . '/lib/private/Accounts/AccountProperty.php', 'OC\\Accounts\\AccountProperty' => $baseDir . '/lib/private/Accounts/AccountProperty.php',
'OC\\Accounts\\AccountPropertyCollection' => $baseDir . '/lib/private/Accounts/AccountPropertyCollection.php',
'OC\\Accounts\\Hooks' => $baseDir . '/lib/private/Accounts/Hooks.php', 'OC\\Accounts\\Hooks' => $baseDir . '/lib/private/Accounts/Hooks.php',
'OC\\Accounts\\TAccountsHelper' => $baseDir . '/lib/private/Accounts/TAccountsHelper.php',
'OC\\Activity\\ActivitySettingsAdapter' => $baseDir . '/lib/private/Activity/ActivitySettingsAdapter.php', 'OC\\Activity\\ActivitySettingsAdapter' => $baseDir . '/lib/private/Activity/ActivitySettingsAdapter.php',
'OC\\Activity\\Event' => $baseDir . '/lib/private/Activity/Event.php', 'OC\\Activity\\Event' => $baseDir . '/lib/private/Activity/Event.php',
'OC\\Activity\\EventMerger' => $baseDir . '/lib/private/Activity/EventMerger.php', 'OC\\Activity\\EventMerger' => $baseDir . '/lib/private/Activity/EventMerger.php',

View File

@ -39,6 +39,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OCP\\Accounts\\IAccount' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccount.php', 'OCP\\Accounts\\IAccount' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccount.php',
'OCP\\Accounts\\IAccountManager' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountManager.php', 'OCP\\Accounts\\IAccountManager' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountManager.php',
'OCP\\Accounts\\IAccountProperty' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountProperty.php', 'OCP\\Accounts\\IAccountProperty' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountProperty.php',
'OCP\\Accounts\\IAccountPropertyCollection' => __DIR__ . '/../../..' . '/lib/public/Accounts/IAccountPropertyCollection.php',
'OCP\\Accounts\\PropertyDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Accounts/PropertyDoesNotExistException.php', 'OCP\\Accounts\\PropertyDoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/Accounts/PropertyDoesNotExistException.php',
'OCP\\Activity\\ActivitySettings' => __DIR__ . '/../../..' . '/lib/public/Activity/ActivitySettings.php', 'OCP\\Activity\\ActivitySettings' => __DIR__ . '/../../..' . '/lib/public/Activity/ActivitySettings.php',
'OCP\\Activity\\IConsumer' => __DIR__ . '/../../..' . '/lib/public/Activity/IConsumer.php', 'OCP\\Activity\\IConsumer' => __DIR__ . '/../../..' . '/lib/public/Activity/IConsumer.php',
@ -610,7 +611,9 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Accounts\\Account' => __DIR__ . '/../../..' . '/lib/private/Accounts/Account.php', 'OC\\Accounts\\Account' => __DIR__ . '/../../..' . '/lib/private/Accounts/Account.php',
'OC\\Accounts\\AccountManager' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountManager.php', 'OC\\Accounts\\AccountManager' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountManager.php',
'OC\\Accounts\\AccountProperty' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountProperty.php', 'OC\\Accounts\\AccountProperty' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountProperty.php',
'OC\\Accounts\\AccountPropertyCollection' => __DIR__ . '/../../..' . '/lib/private/Accounts/AccountPropertyCollection.php',
'OC\\Accounts\\Hooks' => __DIR__ . '/../../..' . '/lib/private/Accounts/Hooks.php', 'OC\\Accounts\\Hooks' => __DIR__ . '/../../..' . '/lib/private/Accounts/Hooks.php',
'OC\\Accounts\\TAccountsHelper' => __DIR__ . '/../../..' . '/lib/private/Accounts/TAccountsHelper.php',
'OC\\Activity\\ActivitySettingsAdapter' => __DIR__ . '/../../..' . '/lib/private/Activity/ActivitySettingsAdapter.php', 'OC\\Activity\\ActivitySettingsAdapter' => __DIR__ . '/../../..' . '/lib/private/Activity/ActivitySettingsAdapter.php',
'OC\\Activity\\Event' => __DIR__ . '/../../..' . '/lib/private/Activity/Event.php', 'OC\\Activity\\Event' => __DIR__ . '/../../..' . '/lib/private/Activity/Event.php',
'OC\\Activity\\EventMerger' => __DIR__ . '/../../..' . '/lib/private/Activity/EventMerger.php', 'OC\\Activity\\EventMerger' => __DIR__ . '/../../..' . '/lib/private/Activity/EventMerger.php',

View File

@ -27,14 +27,17 @@ declare(strict_types=1);
namespace OC\Accounts; namespace OC\Accounts;
use Generator;
use OCP\Accounts\IAccount; use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountProperty; use OCP\Accounts\IAccountProperty;
use OCP\Accounts\IAccountPropertyCollection;
use OCP\Accounts\PropertyDoesNotExistException; use OCP\Accounts\PropertyDoesNotExistException;
use OCP\IUser; use OCP\IUser;
class Account implements IAccount { class Account implements IAccount {
use TAccountsHelper;
/** @var IAccountProperty[] */ /** @var IAccountPropertyCollection[]|IAccountProperty[] */
private $properties = []; private $properties = [];
/** @var IUser */ /** @var IUser */
@ -45,11 +48,17 @@ class Account implements IAccount {
} }
public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount { public function setProperty(string $property, string $value, string $scope, string $verified, string $verificationData = ''): IAccount {
if ($this->isCollection($property)) {
throw new \InvalidArgumentException('setProperty cannot set an IAccountsPropertyCollection');
}
$this->properties[$property] = new AccountProperty($property, $value, $scope, $verified, $verificationData); $this->properties[$property] = new AccountProperty($property, $value, $scope, $verified, $verificationData);
return $this; return $this;
} }
public function getProperty(string $property): IAccountProperty { public function getProperty(string $property): IAccountProperty {
if ($this->isCollection($property)) {
throw new \InvalidArgumentException('getProperty cannot retrieve an IAccountsPropertyCollection');
}
if (!array_key_exists($property, $this->properties)) { if (!array_key_exists($property, $this->properties)) {
throw new PropertyDoesNotExistException($property); throw new PropertyDoesNotExistException($property);
} }
@ -57,19 +66,41 @@ class Account implements IAccount {
} }
public function getProperties(): array { public function getProperties(): array {
return $this->properties; return array_filter($this->properties, function ($obj) {
return $obj instanceof IAccountProperty;
});
}
public function getAllProperties(): Generator {
foreach ($this->properties as $propertyObject) {
if ($propertyObject instanceof IAccountProperty) {
yield $propertyObject;
} elseif ($propertyObject instanceof IAccountPropertyCollection) {
foreach ($propertyObject->getProperties() as $property) {
yield $property;
}
}
}
} }
public function getFilteredProperties(string $scope = null, string $verified = null): array { public function getFilteredProperties(string $scope = null, string $verified = null): array {
return \array_filter($this->properties, function (IAccountProperty $obj) use ($scope, $verified) { $result = $incrementals = [];
/** @var IAccountProperty $obj */
foreach ($this->getAllProperties() as $obj) {
if ($scope !== null && $scope !== $obj->getScope()) { if ($scope !== null && $scope !== $obj->getScope()) {
return false; continue;
} }
if ($verified !== null && $verified !== $obj->getVerified()) { if ($verified !== null && $verified !== $obj->getVerified()) {
return false; continue;
} }
return true; $index = $obj->getName();
}); if ($this->isCollection($index)) {
$incrementals[$index] = ($incrementals[$index] ?? -1) + 1;
$index .= '#' . $incrementals[$index];
}
$result[$index] = $obj;
}
return $result;
} }
public function jsonSerialize() { public function jsonSerialize() {
@ -79,4 +110,19 @@ class Account implements IAccount {
public function getUser(): IUser { public function getUser(): IUser {
return $this->user; return $this->user;
} }
public function setPropertyCollection(IAccountPropertyCollection $propertyCollection): IAccount {
$this->properties[$propertyCollection->getName()] = $propertyCollection;
return $this;
}
public function getPropertyCollection(string $propertyCollection): IAccountPropertyCollection {
if (!array_key_exists($propertyCollection, $this->properties)) {
throw new PropertyDoesNotExistException($propertyCollection);
}
if (!$this->properties[$propertyCollection] instanceof IAccountPropertyCollection) {
throw new \RuntimeException('Requested collection is not an IAccountPropertyCollection');
}
return $this->properties[$propertyCollection];
}
} }

View File

@ -45,6 +45,7 @@ use OCP\IUser;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
use function array_flip;
use function json_decode; use function json_decode;
use function json_last_error; use function json_last_error;
@ -57,6 +58,7 @@ use function json_last_error;
* @package OC\Accounts * @package OC\Accounts
*/ */
class AccountManager implements IAccountManager { class AccountManager implements IAccountManager {
use TAccountsHelper;
/** @var IDBConnection database connection */ /** @var IDBConnection database connection */
private $connection; private $connection;
@ -139,6 +141,61 @@ class AccountManager implements IAccountManager {
return $input; return $input;
} }
protected function sanitizeLength(array &$propertyData, bool $throwOnData = false): void {
if (isset($propertyData['value']) && strlen($propertyData['value']) > 2048) {
if ($throwOnData) {
throw new \InvalidArgumentException();
} else {
$propertyData['value'] = '';
}
}
}
protected function testValueLengths(array &$data, bool $throwOnData = false): void {
try {
foreach ($data as $propertyName => &$propertyData) {
if ($this->isCollection($propertyName)) {
$this->testValueLengths($propertyData, $throwOnData);
} else {
$this->sanitizeLength($propertyData, $throwOnData);
}
}
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException($propertyName);
}
}
protected function testPropertyScopes(array &$data, array $allowedScopes, bool $throwOnData = false, string $parentPropertyName = null): void {
foreach ($data as $propertyNameOrIndex => &$propertyData) {
if ($this->isCollection($propertyNameOrIndex)) {
$this->testPropertyScopes($propertyData, $allowedScopes, $throwOnData);
} elseif (isset($propertyData['scope'])) {
$effectivePropertyName = $parentPropertyName ?? $propertyNameOrIndex;
if ($throwOnData && !in_array($propertyData['scope'], $allowedScopes, true)) {
throw new \InvalidArgumentException('scope');
}
if (
$propertyData['scope'] === self::SCOPE_PRIVATE
&& ($effectivePropertyName === self::PROPERTY_DISPLAYNAME || $effectivePropertyName === self::PROPERTY_EMAIL)
) {
if ($throwOnData) {
// v2-private is not available for these fields
throw new \InvalidArgumentException('scope');
} else {
// default to local
$data[$propertyNameOrIndex]['scope'] = self::SCOPE_LOCAL;
}
} else {
// migrate scope values to the new format
// invalid scopes are mapped to a default value
$data[$propertyNameOrIndex]['scope'] = AccountProperty::mapScopeToV2($propertyData['scope']);
}
}
}
}
/** /**
* update user record * update user record
* *
@ -166,16 +223,7 @@ class AccountManager implements IAccountManager {
} }
} }
// set a max length $this->testValueLengths($data);
foreach ($data as $propertyName => $propertyData) {
if (isset($data[$propertyName]) && isset($data[$propertyName]['value']) && strlen($data[$propertyName]['value']) > 2048) {
if ($throwOnData) {
throw new \InvalidArgumentException($propertyName);
} else {
$data[$propertyName]['value'] = '';
}
}
}
if (isset($data[self::PROPERTY_WEBSITE]) && $data[self::PROPERTY_WEBSITE]['value'] !== '') { if (isset($data[self::PROPERTY_WEBSITE]) && $data[self::PROPERTY_WEBSITE]['value'] !== '') {
try { try {
@ -198,31 +246,7 @@ class AccountManager implements IAccountManager {
self::VISIBILITY_PUBLIC, self::VISIBILITY_PUBLIC,
]; ];
// validate and convert scope values $this->testPropertyScopes($data, $allowedScopes, $throwOnData);
foreach ($data as $propertyName => $propertyData) {
if (isset($propertyData['scope'])) {
if ($throwOnData && !in_array($propertyData['scope'], $allowedScopes, true)) {
throw new \InvalidArgumentException('scope');
}
if (
$propertyData['scope'] === self::SCOPE_PRIVATE
&& ($propertyName === self::PROPERTY_DISPLAYNAME || $propertyName === self::PROPERTY_EMAIL)
) {
if ($throwOnData) {
// v2-private is not available for these fields
throw new \InvalidArgumentException('scope');
} else {
// default to local
$data[$propertyName]['scope'] = self::SCOPE_LOCAL;
}
} else {
// migrate scope values to the new format
// invalid scopes are mapped to a default value
$data[$propertyName]['scope'] = AccountProperty::mapScopeToV2($propertyData['scope']);
}
}
}
if (empty($userData)) { if (empty($userData)) {
$this->insertNewUser($user, $data); $this->insertNewUser($user, $data);
@ -276,12 +300,9 @@ class AccountManager implements IAccountManager {
/** /**
* get stored data from a given user * get stored data from a given user
* *
* @param IUser $user
* @return array
*
* @deprecated use getAccount instead to make sure migrated properties work correctly * @deprecated use getAccount instead to make sure migrated properties work correctly
*/ */
public function getUser(IUser $user) { public function getUser(IUser $user, bool $insertIfNotExists = true): array {
$uid = $user->getUID(); $uid = $user->getUID();
$query = $this->connection->getQueryBuilder(); $query = $this->connection->getQueryBuilder();
$query->select('data') $query->select('data')
@ -294,7 +315,9 @@ class AccountManager implements IAccountManager {
if (empty($accountData)) { if (empty($accountData)) {
$userData = $this->buildDefaultUserRecord($user); $userData = $this->buildDefaultUserRecord($user);
$this->insertNewUser($user, $userData); if ($insertIfNotExists) {
$this->insertNewUser($user, $userData);
}
return $userData; return $userData;
} }
@ -305,9 +328,7 @@ class AccountManager implements IAccountManager {
return $this->buildDefaultUserRecord($user); return $this->buildDefaultUserRecord($user);
} }
$userDataArray = $this->addMissingDefaultValues($userDataArray); return $this->addMissingDefaultValues($userDataArray);
return $userDataArray;
} }
public function searchUsers(string $property, array $values): array { public function searchUsers(string $property, array $values): array {
@ -324,12 +345,23 @@ class AccountManager implements IAccountManager {
$result = $query->execute(); $result = $query->execute();
while ($row = $result->fetch()) { while ($row = $result->fetch()) {
$matches[$row['value']] = $row['uid']; $matches[$row['uid']] = $row['value'];
} }
$result->closeCursor(); $result->closeCursor();
} }
return $matches; $result = array_merge($matches, $this->searchUsersForRelatedCollection($property, $values));
return array_flip($result);
}
protected function searchUsersForRelatedCollection(string $property, array $values): array {
switch ($property) {
case IAccountManager::PROPERTY_EMAIL:
return array_flip($this->searchUsers(IAccountManager::COLLECTION_EMAIL, $values));
default:
return [];
}
} }
/** /**
@ -340,7 +372,7 @@ class AccountManager implements IAccountManager {
* @param IUser $user * @param IUser $user
* @return array * @return array
*/ */
protected function checkEmailVerification($oldData, $newData, IUser $user) { protected function checkEmailVerification($oldData, $newData, IUser $user): array {
if ($oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']) { if ($oldData[self::PROPERTY_EMAIL]['value'] !== $newData[self::PROPERTY_EMAIL]['value']) {
$this->jobList->add(VerifyUserData::class, $this->jobList->add(VerifyUserData::class,
[ [
@ -381,7 +413,7 @@ class AccountManager implements IAccountManager {
* @param array $newData * @param array $newData
* @return array * @return array
*/ */
protected function updateVerifyStatus($oldData, $newData) { protected function updateVerifyStatus(array $oldData, array $newData): array {
// which account was already verified successfully? // which account was already verified successfully?
$twitterVerified = isset($oldData[self::PROPERTY_TWITTER]['verified']) && $oldData[self::PROPERTY_TWITTER]['verified'] === self::VERIFIED; $twitterVerified = isset($oldData[self::PROPERTY_TWITTER]['verified']) && $oldData[self::PROPERTY_TWITTER]['verified'] === self::VERIFIED;
@ -481,12 +513,20 @@ class AccountManager implements IAccountManager {
'value' => $query->createParameter('value'), 'value' => $query->createParameter('value'),
] ]
); );
$this->writeUserDataProperties($query, $data);
}
protected function writeUserDataProperties(IQueryBuilder $query, array $data, string $parentPropertyName = null): void {
foreach ($data as $propertyName => $property) { foreach ($data as $propertyName => $property) {
if ($propertyName === self::PROPERTY_AVATAR) { if ($this->isCollection($propertyName)) {
$this->writeUserDataProperties($query, $property, $propertyName);
continue;
}
if (($parentPropertyName ?? $propertyName) === self::PROPERTY_AVATAR) {
continue; continue;
} }
$query->setParameter('name', $propertyName) $query->setParameter('name', $parentPropertyName ?? $propertyName)
->setParameter('value', $property['value'] ?? ''); ->setParameter('value', $property['value'] ?? '');
$query->execute(); $query->execute();
} }

View File

@ -0,0 +1,90 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace OC\Accounts;
use InvalidArgumentException;
use OCP\Accounts\IAccountProperty;
use OCP\Accounts\IAccountPropertyCollection;
class AccountPropertyCollection implements IAccountPropertyCollection {
/** @var string */
protected $collectionName = '';
/** @var IAccountProperty[] */
protected $properties = [];
public function __construct(string $collectionName) {
$this->collectionName = $collectionName;
}
public function setProperties(array $properties): IAccountPropertyCollection {
/** @var IAccountProperty $property */
$this->properties = [];
foreach ($properties as $property) {
$this->addProperty($property);
}
return $this;
}
public function getProperties(): array {
return $this->properties;
}
public function addProperty(IAccountProperty $property): IAccountPropertyCollection {
if ($property->getName() !== $this->collectionName) {
throw new InvalidArgumentException('Provided property does not match collection name');
}
$this->properties[] = $property;
return $this;
}
public function removeProperty(IAccountProperty $property): IAccountPropertyCollection {
$ref = array_search($property, $this->properties, true);
if ($ref !== false) {
unset($this->properties[$ref]);
}
return $this;
}
public function removePropertyByValue(string $value): IAccountPropertyCollection {
foreach ($this->properties as $i => $property) {
if ($property->getValue() === $value) {
unset($this->properties[$i]);
}
}
return $this;
}
public function jsonSerialize() {
return [$this->collectionName => $this->properties];
}
public function getName(): string {
return $this->collectionName;
}
}

View File

@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace OC\Accounts;
use OCP\Accounts\IAccountManager;
trait TAccountsHelper {
protected function isCollection(string $propertyName): bool {
return in_array($propertyName,
[
IAccountManager::COLLECTION_EMAIL,
],
true
);
}
}

View File

@ -26,6 +26,7 @@ declare(strict_types=1);
namespace OCP\Accounts; namespace OCP\Accounts;
use Generator;
use OCP\IUser; use OCP\IUser;
/** /**
@ -62,16 +63,48 @@ interface IAccount extends \JsonSerializable {
/** /**
* Get all properties of an account. Array indices are property names. * Get all properties of an account. Array indices are property names.
* Values from IAccountPropertyCollections are not included in the return
* array.
* *
* @since 15.0.0 * @since 15.0.0
* * @deprecated 22.0.0 use getAllProperties()
* @return IAccountProperty[]
*/ */
public function getProperties(): array; public function getProperties(): array;
/**
* Get all properties of an account. Array indices are numeric. To get
* the property name, call getName() against the value.
*
* IAccountPropertyCollections are being flattened into an IAccountProperty
* for each value.
*
* @since 22.0.0
*
* @return Generator<int, IAccountProperty>
*/
public function getAllProperties(): Generator;
/**
* Set a property collection (multi-value properties)
*
* @since 22.0.0
*/
public function setPropertyCollection(IAccountPropertyCollection $propertyCollection): IAccount;
/**
* Returns the requestes propery collection (multi-value properties)
*
* @since 22.0.0
*/
public function getPropertyCollection(string $propertyCollection): IAccountPropertyCollection;
/** /**
* Get all properties that match the provided filters for scope and verification status * Get all properties that match the provided filters for scope and verification status
* *
* Since 22.0.0 values from IAccountPropertyCollection are included, but also
* as IAccountProperty instances. They for properties of IAccountPropertyCollection are
* suffixed incrementally, i.e. #0, #1 ... #n the numbers have no further meaning.
*
* @since 15.0.0 * @since 15.0.0
* *
* @param string $scope Must be one of the VISIBILITY_ prefixed constants of \OCP\Accounts\IAccountManager * @param string $scope Must be one of the VISIBILITY_ prefixed constants of \OCP\Accounts\IAccountManager

View File

@ -96,6 +96,8 @@ interface IAccountManager {
public const PROPERTY_ADDRESS = 'address'; public const PROPERTY_ADDRESS = 'address';
public const PROPERTY_TWITTER = 'twitter'; public const PROPERTY_TWITTER = 'twitter';
public const COLLECTION_EMAIL = 'additional_mail';
public const NOT_VERIFIED = '0'; public const NOT_VERIFIED = '0';
public const VERIFICATION_IN_PROGRESS = '1'; public const VERIFICATION_IN_PROGRESS = '1';
public const VERIFIED = '2'; public const VERIFIED = '2';
@ -123,7 +125,10 @@ interface IAccountManager {
/** /**
* Search for users based on account data * Search for users based on account data
* *
* @param string $property * @param string $property - property or property collection name since
* NC 22 the implementation MAY add a fitting property collection into the
* search even if a property name was given e.g. email property and email
* collection)
* @param string[] $values * @param string[] $values
* @return array * @return array
* *

View File

@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace OCP\Accounts;
use InvalidArgumentException;
use JsonSerializable;
/**
* Interface IAccountPropertyCollection
*
* @package OCP\Accounts
*
* @since 22.0.0
*/
interface IAccountPropertyCollection extends JsonSerializable {
/**
* retuns the collection name
*
* @since 22.0.0
*/
public function getName(): string;
/**
* set properties of this collection
*
* @param IAccountProperty[] $properties
* @throws InvalidArgumentException
* @since 22.0.0
*/
public function setProperties(array $properties): IAccountPropertyCollection;
/**
* @return IAccountProperty[]
* @since 22.0.0
*/
public function getProperties(): array;
/**
* adds a property to this collection
*
* @throws InvalidArgumentException
* @since 22.0.0
*/
public function addProperty(IAccountProperty $property): IAccountPropertyCollection;
/**
* removes a property of this collection
*
* @since 22.0.0
*/
public function removeProperty(IAccountProperty $property): IAccountPropertyCollection;
/**
* removes a property identified by its value
*
* @since 22.0.0
*/
public function removePropertyByValue(string $value): IAccountPropertyCollection;
}

View File

@ -26,6 +26,7 @@ use OC\Accounts\AccountManager;
use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountManager;
use OCP\BackgroundJob\IJobList; use OCP\BackgroundJob\IJobList;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IUser; use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -59,13 +60,24 @@ class AccountManagerTest extends TestCase {
/** @var LoggerInterface|MockObject */ /** @var LoggerInterface|MockObject */
private $logger; private $logger;
/** @var AccountManager */
private $accountManager;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->connection = \OC::$server->getDatabaseConnection(); $this->connection = \OC::$server->get(IDBConnection::class);
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->jobList = $this->createMock(IJobList::class); $this->jobList = $this->createMock(IJobList::class);
$this->logger = $this->createMock(LoggerInterface::class); $this->logger = $this->createMock(LoggerInterface::class);
$this->accountManager = new AccountManager(
$this->connection,
$this->config,
$this->eventDispatcher,
$this->jobList,
$this->logger,
);
} }
protected function tearDown(): void { protected function tearDown(): void {
@ -74,6 +86,90 @@ class AccountManagerTest extends TestCase {
$query->delete($this->table)->execute(); $query->delete($this->table)->execute();
} }
protected function makeUser(string $uid, string $name, string $email = null): IUser {
$user = $this->createMock(IUser::class);
$user->expects($this->any())
->method('getUid')
->willReturn($uid);
$user->expects($this->any())
->method('getDisplayName')
->willReturn($name);
if ($email !== null) {
$user->expects($this->any())
->method('getEMailAddress')
->willReturn($email);
}
return $user;
}
protected function populateOrUpdate(): void {
$users = [
[
'user' => $this->makeUser('j.doe', 'Jane Doe', 'jane.doe@acme.com'),
'data' => [
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Jane Doe', 'scope' => IAccountManager::SCOPE_PUBLISHED],
IAccountManager::PROPERTY_EMAIL => ['value' => 'jane.doe@acme.com', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_TWITTER => ['value' => '@sometwitter', 'scope' => IAccountManager::SCOPE_PUBLISHED],
IAccountManager::PROPERTY_PHONE => ['value' => '+491601231212', 'scope' => IAccountManager::SCOPE_FEDERATED],
IAccountManager::PROPERTY_ADDRESS => ['value' => 'some street', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://acme.com', 'scope' => IAccountManager::SCOPE_PRIVATE],
],
],
[
'user' => $this->makeUser('a.allison', 'Alice Allison', 'a.allison@example.org'),
'data' => [
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Alice Allison', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_EMAIL => ['value' => 'a.allison@example.org', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_TWITTER => ['value' => '@a_alice', 'scope' => IAccountManager::SCOPE_FEDERATED],
IAccountManager::PROPERTY_PHONE => ['value' => '+491602312121', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_ADDRESS => ['value' => 'Dundee Road 45', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.org', 'scope' => IAccountManager::SCOPE_LOCAL],
],
],
[
'user' => $this->makeUser('b32c5a5b-1084-4380-8856-e5223b16de9f', 'Armel Oliseh', 'oliseh@example.com'),
'data' => [
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Armel Oliseh', 'scope' => IAccountManager::SCOPE_PUBLISHED],
IAccountManager::PROPERTY_EMAIL => ['value' => 'oliseh@example.com', 'scope' => IAccountManager::SCOPE_PUBLISHED],
IAccountManager::PROPERTY_TWITTER => ['value' => '', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_PHONE => ['value' => '+491603121212', 'scope' => IAccountManager::SCOPE_PUBLISHED],
IAccountManager::PROPERTY_ADDRESS => ['value' => 'Sunflower Blvd. 77', 'scope' => IAccountManager::SCOPE_PUBLISHED],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://example.com', 'scope' => IAccountManager::SCOPE_PUBLISHED],
],
],
[
'user' => $this->makeUser('31b5316a-9b57-4b17-970a-315a4cbe73eb', 'K. Cheng', 'cheng@emca.com'),
'data' => [
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'K. Cheng', 'scope' => IAccountManager::SCOPE_FEDERATED],
IAccountManager::PROPERTY_EMAIL => ['value' => 'cheng@emca.com', 'scope' => IAccountManager::SCOPE_FEDERATED],
IAccountManager::PROPERTY_TWITTER => ['value' => '', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_PHONE => ['value' => '+71601212123', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_ADDRESS => ['value' => 'Pinapple Street 22', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://emca.com', 'scope' => IAccountManager::SCOPE_FEDERATED],
IAccountManager::COLLECTION_EMAIL => [
['value' => 'k.cheng@emca.com', 'scope' => IAccountManager::SCOPE_LOCAL],
['value' => 'kai.cheng@emca.com', 'scope' => IAccountManager::SCOPE_LOCAL],
],
],
],
[
'user' => $this->makeUser('goodpal@elpmaxe.org', 'Goodpal, Kim', 'goodpal@elpmaxe.org'),
'data' => [
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'Goodpal, Kim', 'scope' => IAccountManager::SCOPE_PUBLISHED],
IAccountManager::PROPERTY_EMAIL => ['value' => 'goodpal@elpmaxe.org', 'scope' => IAccountManager::SCOPE_PUBLISHED],
IAccountManager::PROPERTY_TWITTER => ['value' => '', 'scope' => IAccountManager::SCOPE_LOCAL],
IAccountManager::PROPERTY_PHONE => ['value' => '+71602121231', 'scope' => IAccountManager::SCOPE_FEDERATED],
IAccountManager::PROPERTY_ADDRESS => ['value' => 'Octopus Ave 17', 'scope' => IAccountManager::SCOPE_FEDERATED],
IAccountManager::PROPERTY_WEBSITE => ['value' => 'https://elpmaxe.org', 'scope' => IAccountManager::SCOPE_PUBLISHED],
],
],
];
foreach ($users as $userInfo) {
$this->accountManager->updateUser($userInfo['user'], $userInfo['data'], false);
}
}
/** /**
* get a instance of the accountManager * get a instance of the accountManager
* *
@ -340,9 +436,8 @@ class AccountManagerTest extends TestCase {
$oldData = ['key' => ['value' => 'value']]; $oldData = ['key' => ['value' => 'value']];
$newData = ['newKey' => ['value' => 'newValue']]; $newData = ['newKey' => ['value' => 'newValue']];
$accountManager = $this->getInstance();
$this->addDummyValuesToTable('uid', $oldData); $this->addDummyValuesToTable('uid', $oldData);
$this->invokePrivate($accountManager, 'updateExistingUser', [$user, $newData]); $this->invokePrivate($this->accountManager, 'updateExistingUser', [$user, $newData]);
$newDataFromTable = $this->getDataFromTable('uid'); $newDataFromTable = $this->getDataFromTable('uid');
$this->assertEquals($newData, $newDataFromTable); $this->assertEquals($newData, $newDataFromTable);
} }
@ -352,18 +447,15 @@ class AccountManagerTest extends TestCase {
$uid = 'uid'; $uid = 'uid';
$data = ['key' => ['value' => 'value']]; $data = ['key' => ['value' => 'value']];
$accountManager = $this->getInstance();
$user->expects($this->atLeastOnce())->method('getUID')->willReturn($uid); $user->expects($this->atLeastOnce())->method('getUID')->willReturn($uid);
$this->assertNull($this->getDataFromTable($uid)); $this->assertNull($this->getDataFromTable($uid));
$this->invokePrivate($accountManager, 'insertNewUser', [$user, $data]); $this->invokePrivate($this->accountManager, 'insertNewUser', [$user, $data]);
$dataFromDb = $this->getDataFromTable($uid); $dataFromDb = $this->getDataFromTable($uid);
$this->assertEquals($data, $dataFromDb); $this->assertEquals($data, $dataFromDb);
} }
public function testAddMissingDefaultValues() { public function testAddMissingDefaultValues() {
$accountManager = $this->getInstance();
$input = [ $input = [
'key1' => ['value' => 'value1', 'verified' => '0'], 'key1' => ['value' => 'value1', 'verified' => '0'],
'key2' => ['value' => 'value1'], 'key2' => ['value' => 'value1'],
@ -374,7 +466,7 @@ class AccountManagerTest extends TestCase {
'key2' => ['value' => 'value1', 'verified' => '0'], 'key2' => ['value' => 'value1', 'verified' => '0'],
]; ];
$result = $this->invokePrivate($accountManager, 'addMissingDefaultValues', [$input]); $result = $this->invokePrivate($this->accountManager, 'addMissingDefaultValues', [$input]);
$this->assertSame($expected, $result); $this->assertSame($expected, $result);
} }
@ -461,13 +553,11 @@ class AccountManagerTest extends TestCase {
$this->config->method('getSystemValueString') $this->config->method('getSystemValueString')
->willReturn($defaultRegion); ->willReturn($defaultRegion);
$instance = $this->getInstance();
if ($phoneNumber === null) { if ($phoneNumber === null) {
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
self::invokePrivate($instance, 'parsePhoneNumber', [$phoneInput]); self::invokePrivate($this->accountManager, 'parsePhoneNumber', [$phoneInput]);
} else { } else {
self::assertEquals($phoneNumber, self::invokePrivate($instance, 'parsePhoneNumber', [$phoneInput])); self::assertEquals($phoneNumber, self::invokePrivate($this->accountManager, 'parsePhoneNumber', [$phoneInput]));
} }
} }
@ -487,13 +577,73 @@ class AccountManagerTest extends TestCase {
* @param string|null $websiteOutput * @param string|null $websiteOutput
*/ */
public function testParseWebsite(string $websiteInput, ?string $websiteOutput): void { public function testParseWebsite(string $websiteInput, ?string $websiteOutput): void {
$instance = $this->getInstance();
if ($websiteOutput === null) { if ($websiteOutput === null) {
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
self::invokePrivate($instance, 'parseWebsite', [$websiteInput]); self::invokePrivate($this->accountManager, 'parseWebsite', [$websiteInput]);
} else { } else {
self::assertEquals($websiteOutput, self::invokePrivate($instance, 'parseWebsite', [$websiteInput])); self::assertEquals($websiteOutput, self::invokePrivate($this->accountManager, 'parseWebsite', [$websiteInput]));
} }
} }
/**
* @dataProvider searchDataProvider
*/
public function testSearchUsers(string $property, array $values, array $expected): void {
$this->populateOrUpdate();
$matchedUsers = $this->accountManager->searchUsers($property, $values);
$this->assertSame($expected, $matchedUsers);
}
public function searchDataProvider(): array {
return [
[ #0 Search for an existing name
IAccountManager::PROPERTY_DISPLAYNAME,
['Jane Doe'],
['Jane Doe' => 'j.doe']
],
[ #1 Search for part of a name (no result)
IAccountManager::PROPERTY_DISPLAYNAME,
['Jane'],
[]
],
[ #2 Search for part of a name (no result, test wildcard)
IAccountManager::PROPERTY_DISPLAYNAME,
['Jane%'],
[]
],
[ #3 Search for phone
IAccountManager::PROPERTY_PHONE,
['+491603121212'],
['+491603121212' => 'b32c5a5b-1084-4380-8856-e5223b16de9f'],
],
[ #4 Search for twitter handles
IAccountManager::PROPERTY_TWITTER,
['@sometwitter', '@a_alice', '@unseen'],
['@sometwitter' => 'j.doe', '@a_alice' => 'a.allison'],
],
[ #5 Search for email
IAccountManager::PROPERTY_EMAIL,
['cheng@emca.com'],
['cheng@emca.com' => '31b5316a-9b57-4b17-970a-315a4cbe73eb'],
],
[ #6 Search for email by additional email
IAccountManager::PROPERTY_EMAIL,
['kai.cheng@emca.com'],
['kai.cheng@emca.com' => '31b5316a-9b57-4b17-970a-315a4cbe73eb'],
],
[ #7 Search for additional email
IAccountManager::COLLECTION_EMAIL,
['kai.cheng@emca.com', 'cheng@emca.com'],
['kai.cheng@emca.com' => '31b5316a-9b57-4b17-970a-315a4cbe73eb'],
],
[ #8 Search for email by additional email (two valid search values, but the same user)
IAccountManager::PROPERTY_EMAIL,
['kai.cheng@emca.com', 'cheng@emca.com'],
[
'kai.cheng@emca.com' => '31b5316a-9b57-4b17-970a-315a4cbe73eb',
],
],
];
}
} }

View File

@ -0,0 +1,209 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace lib\Accounts;
use InvalidArgumentException;
use OC\Accounts\AccountPropertyCollection;
use OCP\Accounts\IAccountProperty;
use OCP\Accounts\IAccountPropertyCollection;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class AccountPropertyCollectionTest extends TestCase {
/** @var IAccountPropertyCollection */
protected $collection;
protected const COLLECTION_NAME = 'my_multivalue_property';
public function setUp(): void {
parent::setUp();
$this->collection = new AccountPropertyCollection(self::COLLECTION_NAME);
}
/**
* @return IAccountProperty|MockObject
*/
protected function makePropertyMock(string $propertyName): MockObject {
$mock = $this->createMock(IAccountProperty::class);
$mock->expects($this->any())
->method('getName')
->willReturn($propertyName);
return $mock;
}
public function testSetAndGetProperties() {
$propsBefore = $this->collection->getProperties();
$this->assertIsArray($propsBefore);
$this->assertEmpty($propsBefore);
$props = [
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
];
$this->collection->setProperties($props);
$propsAfter = $this->collection->getProperties();
$this->assertIsArray($propsAfter);
$this->assertCount(count($props), $propsAfter);
}
public function testSetPropertiesMixedInvalid() {
$props = [
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock('sneaky_property'),
$this->makePropertyMock(self::COLLECTION_NAME),
];
$this->expectException(InvalidArgumentException::class);
$this->collection->setProperties($props);
}
public function testAddProperty() {
$props = [
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
];
$this->collection->setProperties($props);
$additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
$this->collection->addProperty($additionalProperty);
$propsAfter = $this->collection->getProperties();
$this->assertCount(count($props) + 1, $propsAfter);
$this->assertNotFalse(array_search($additionalProperty, $propsAfter, true));
}
public function testAddPropertyInvalid() {
$props = [
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
];
$this->collection->setProperties($props);
$additionalProperty = $this->makePropertyMock('sneaky_property');
$exceptionThrown = false;
try {
$this->collection->addProperty($additionalProperty);
} catch (\InvalidArgumentException $e) {
$exceptionThrown = true;
} finally {
$propsAfter = $this->collection->getProperties();
$this->assertCount(count($props), $propsAfter);
$this->assertFalse(array_search($additionalProperty, $propsAfter, true));
$this->assertTrue($exceptionThrown);
}
}
public function testRemoveProperty() {
$additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
$props = [
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
$additionalProperty,
$this->makePropertyMock(self::COLLECTION_NAME),
];
$this->collection->setProperties($props);
$propsBefore = $this->collection->getProperties();
$this->collection->removeProperty($additionalProperty);
$propsAfter = $this->collection->getProperties();
$this->assertTrue(count($propsBefore) > count($propsAfter));
$this->assertCount(count($propsBefore) - 1, $propsAfter);
$this->assertFalse(array_search($additionalProperty, $propsAfter, true));
}
public function testRemovePropertyNotFound() {
$additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
$props = [
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
];
$this->collection->setProperties($props);
$propsBefore = $this->collection->getProperties();
$this->collection->removeProperty($additionalProperty);
$propsAfter = $this->collection->getProperties();
// no errors, gently
$this->assertCount(count($propsBefore), $propsAfter);
}
public function testRemovePropertyByValue() {
$additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
$additionalProperty->expects($this->any())
->method('getValue')
->willReturn('Lorem ipsum');
$additionalPropertyTwo = clone $additionalProperty;
$props = [
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
$additionalProperty,
$this->makePropertyMock(self::COLLECTION_NAME),
$additionalPropertyTwo
];
$this->collection->setProperties($props);
$propsBefore = $this->collection->getProperties();
$this->collection->removePropertyByValue('Lorem ipsum');
$propsAfter = $this->collection->getProperties();
$this->assertTrue(count($propsBefore) > count($propsAfter));
$this->assertCount(count($propsBefore) - 2, $propsAfter);
$this->assertFalse(array_search($additionalProperty, $propsAfter, true));
$this->assertFalse(array_search($additionalPropertyTwo, $propsAfter, true));
}
public function testRemovePropertyByValueNotFound() {
$additionalProperty = $this->makePropertyMock(self::COLLECTION_NAME);
$additionalProperty->expects($this->any())
->method('getValue')
->willReturn('Lorem ipsum');
$props = [
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
$this->makePropertyMock(self::COLLECTION_NAME),
];
$this->collection->setProperties($props);
$propsBefore = $this->collection->getProperties();
$this->collection->removePropertyByValue('Lorem ipsum');
$propsAfter = $this->collection->getProperties();
// no errors, gently
$this->assertCount(count($propsBefore), $propsAfter);
}
}

View File

@ -25,6 +25,7 @@ namespace Test\Accounts;
use OC\Accounts\Account; use OC\Accounts\Account;
use OC\Accounts\AccountProperty; use OC\Accounts\AccountProperty;
use OC\Accounts\AccountPropertyCollection;
use OCP\Accounts\IAccountManager; use OCP\Accounts\IAccountManager;
use OCP\IUser; use OCP\IUser;
use Test\TestCase; use Test\TestCase;
@ -49,7 +50,7 @@ class AccountTest extends TestCase {
$this->assertEquals($property, $account->getProperty(IAccountManager::PROPERTY_WEBSITE)); $this->assertEquals($property, $account->getProperty(IAccountManager::PROPERTY_WEBSITE));
} }
public function testGetProperties() { public function testGetAndGetAllProperties() {
$user = $this->createMock(IUser::class); $user = $this->createMock(IUser::class);
$properties = [ $properties = [
IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''), IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''),
@ -59,7 +60,14 @@ class AccountTest extends TestCase {
$account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED); $account->setProperty(IAccountManager::PROPERTY_WEBSITE, 'https://example.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED);
$account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED); $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED);
$col = new AccountPropertyCollection(IAccountManager::COLLECTION_EMAIL);
$additionalProperty = new AccountProperty($col->getName(), 'second@example.org', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, '');
$col->addProperty($additionalProperty);
$account->setPropertyCollection($col);
$this->assertEquals($properties, $account->getProperties()); $this->assertEquals($properties, $account->getProperties());
$properties[] = $additionalProperty;
$this->assertEquals(array_values($properties), \iterator_to_array($account->getAllProperties()));
} }
public function testGetFilteredProperties() { public function testGetFilteredProperties() {
@ -74,11 +82,20 @@ class AccountTest extends TestCase {
$account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED); $account->setProperty(IAccountManager::PROPERTY_EMAIL, 'user@example.com', IAccountManager::SCOPE_LOCAL, IAccountManager::VERIFIED);
$account->setProperty(IAccountManager::PROPERTY_PHONE, '123456', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED); $account->setProperty(IAccountManager::PROPERTY_PHONE, '123456', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED);
$col = new AccountPropertyCollection(IAccountManager::COLLECTION_EMAIL);
$additionalProperty1 = new AccountProperty($col->getName(), 'second@example.org', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, '');
$additionalProperty2 = new AccountProperty($col->getName(), 'third@example.org', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED, '');
$col->addProperty($additionalProperty1);
$col->addProperty($additionalProperty2);
$account->setPropertyCollection($col);
$this->assertEquals( $this->assertEquals(
[ [
IAccountManager::PROPERTY_WEBSITE => $properties[IAccountManager::PROPERTY_WEBSITE], IAccountManager::PROPERTY_WEBSITE => $properties[IAccountManager::PROPERTY_WEBSITE],
IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE], IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE],
IAccountManager::COLLECTION_EMAIL . '#0' => $additionalProperty1,
IAccountManager::COLLECTION_EMAIL . '#1' => $additionalProperty2,
], ],
$account->getFilteredProperties(IAccountManager::SCOPE_PUBLISHED) $account->getFilteredProperties(IAccountManager::SCOPE_PUBLISHED)
); );
@ -86,12 +103,16 @@ class AccountTest extends TestCase {
[ [
IAccountManager::PROPERTY_EMAIL => $properties[IAccountManager::PROPERTY_EMAIL], IAccountManager::PROPERTY_EMAIL => $properties[IAccountManager::PROPERTY_EMAIL],
IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE], IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE],
IAccountManager::COLLECTION_EMAIL . '#0' => $additionalProperty2,
], ],
$account->getFilteredProperties(null, IAccountManager::VERIFIED) $account->getFilteredProperties(null, IAccountManager::VERIFIED)
); );
$this->assertEquals( $this->assertEquals(
[IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE]], [
$account->getFilteredProperties(IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED) IAccountManager::PROPERTY_PHONE => $properties[IAccountManager::PROPERTY_PHONE],
IAccountManager::COLLECTION_EMAIL . '#0' => $additionalProperty2,
],
$account->getFilteredProperties(IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFIED),
); );
} }