diff options
author | Pytal <24800714+Pytal@users.noreply.github.com> | 2022-03-18 01:22:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-18 01:22:41 +0300 |
commit | 3f190d9fe6355a837ebc9b56c3211e9e7ac6d396 (patch) | |
tree | 31db15c27346212872001a1a7bc9857fb656ccc3 | |
parent | 6fae5983904b3f8c0435923db297e6feccf9a6e9 (diff) | |
parent | c505bb144954821a86a57a496d2a31d9f6f6cb1f (diff) |
Merge pull request #31488 from nextcloud/enh/account-set-all-properties
-rw-r--r-- | lib/private/Accounts/Account.php | 22 | ||||
-rw-r--r-- | lib/public/Accounts/IAccount.php | 43 | ||||
-rw-r--r-- | tests/lib/Accounts/AccountTest.php | 25 |
3 files changed, 90 insertions, 0 deletions
diff --git a/lib/private/Accounts/Account.php b/lib/private/Accounts/Account.php index 7d36af561ce..d3287b219d0 100644 --- a/lib/private/Accounts/Account.php +++ b/lib/private/Accounts/Account.php @@ -72,6 +72,28 @@ class Account implements IAccount { }); } + public function setAllPropertiesFromJson(array $properties): IAccount { + foreach ($properties as $propertyName => $propertyObject) { + if ($this->isCollection($propertyName)) { + $collection = new AccountPropertyCollection($propertyName); + /** @var array<int, IAccountProperty> $collectionProperties */ + $collectionProperties = []; + /** @var array<int, array<string, string>> $propertyObject */ + foreach ($propertyObject as ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData]) { + $collectionProperties[] = new AccountProperty($collection->getName(), $value, $scope, $verified, $verificationData); + } + $collection->setProperties($collectionProperties); + $this->setPropertyCollection($collection); + } else { + /** @var array<string, string> $propertyObject */ + ['value' => $value, 'scope' => $scope, 'verified' => $verified, 'verificationData' => $verificationData] = $propertyObject; + $this->setProperty($propertyName, $value, $scope, $verified, $verificationData); + } + } + + return $this; + } + public function getAllProperties(): Generator { foreach ($this->properties as $propertyObject) { if ($propertyObject instanceof IAccountProperty) { diff --git a/lib/public/Accounts/IAccount.php b/lib/public/Accounts/IAccount.php index 8d4d8b5c023..383f21068a9 100644 --- a/lib/public/Accounts/IAccount.php +++ b/lib/public/Accounts/IAccount.php @@ -72,6 +72,49 @@ interface IAccount extends \JsonSerializable { public function getProperties(): array; /** + * Set all properties of an account + * + * @param array<string, array<string, string>>|array<string, array<int, array<string, string>>> $properties + * + * e.g. `[ + * 'displayname' => [ + * 'name' => 'displayname', + * 'value' => 'Jonathan Smith', + * 'scope' => 'v2-federated', + * 'verified' => '0', + * 'verificationData' => '', + * ], + * 'email' => [ + * 'name' => 'email', + * 'value' => 'jonathan@example.org', + * 'scope' => 'v2-federated', + * 'verified' => '0', + * 'verificationData' => '', + * ], + * // ... + * 'additional_mail' => [ + * [ + * 'name' => 'additional_mail', + * 'value' => 'jon@example.org', + * 'scope' => 'v2-local', + * 'verified' => '0', + * 'verificationData' => '', + * ], + * [ + * 'name' => 'additional_mail', + * 'value' => 'jon@earth.org', + * 'scope' => 'v2-local', + * 'verified' => '0', + * 'verificationData' => '', + * ], + * ], + * ]` + * + * @since 24.0.0 + */ + public function setAllPropertiesFromJson(array $properties): IAccount; + + /** * Get all properties of an account. Array indices are numeric. To get * the property name, call getName() against the value. * diff --git a/tests/lib/Accounts/AccountTest.php b/tests/lib/Accounts/AccountTest.php index 064a9bf2732..a86b1d0255b 100644 --- a/tests/lib/Accounts/AccountTest.php +++ b/tests/lib/Accounts/AccountTest.php @@ -70,6 +70,31 @@ class AccountTest extends TestCase { $this->assertEquals(array_values($properties), \iterator_to_array($account->getAllProperties())); } + public function testSetAllPropertiesFromJson() { + $user = $this->createMock(IUser::class); + $properties = [ + IAccountManager::PROPERTY_DISPLAYNAME => new AccountProperty(IAccountManager::PROPERTY_DISPLAYNAME, 'Steve', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_ADDRESS => new AccountProperty(IAccountManager::PROPERTY_ADDRESS, '123 Acorn Avenue', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_WEBSITE => new AccountProperty(IAccountManager::PROPERTY_WEBSITE, 'https://www.example.org', IAccountManager::SCOPE_FEDERATED, IAccountManager::VERIFIED, ''), + IAccountManager::PROPERTY_EMAIL => new AccountProperty(IAccountManager::PROPERTY_EMAIL, 'steve@earth.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::VERIFICATION_IN_PROGRESS, ''), + IAccountManager::PROPERTY_AVATAR => new AccountProperty(IAccountManager::PROPERTY_AVATAR, '', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_PHONE => new AccountProperty(IAccountManager::PROPERTY_PHONE, '+358407991028', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_TWITTER => new AccountProperty(IAccountManager::PROPERTY_TWITTER, 'therealsteve', IAccountManager::SCOPE_PRIVATE, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_ORGANISATION => new AccountProperty(IAccountManager::PROPERTY_ORGANISATION, 'Steve Incorporated', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_ROLE => new AccountProperty(IAccountManager::PROPERTY_ROLE, 'Founder', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_HEADLINE => new AccountProperty(IAccountManager::PROPERTY_HEADLINE, 'I am Steve', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_BIOGRAPHY => new AccountProperty(IAccountManager::PROPERTY_BIOGRAPHY, 'Steve is the best', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::PROPERTY_PROFILE_ENABLED => new AccountProperty(IAccountManager::PROPERTY_PROFILE_ENABLED, '1', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''), + IAccountManager::COLLECTION_EMAIL => [ + new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'steve@mars.com', IAccountManager::SCOPE_PUBLISHED, IAccountManager::NOT_VERIFIED, ''), + new AccountProperty(IAccountManager::COLLECTION_EMAIL, 'steve@neptune.com', IAccountManager::SCOPE_FEDERATED, IAccountManager::NOT_VERIFIED, ''), + ], + ]; + $account = new Account($user); + $account->setAllPropertiesFromJson(json_decode(json_encode($properties), true)); + $this->assertEquals($properties, $account->jsonSerialize()); + } + public function testGetFilteredProperties() { $user = $this->createMock(IUser::class); $properties = [ |