]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add method to set all account properties from json
authorChristopher Ng <chrng8@gmail.com>
Tue, 8 Mar 2022 00:49:00 +0000 (00:49 +0000)
committerChristopher Ng <chrng8@gmail.com>
Thu, 17 Mar 2022 16:33:19 +0000 (16:33 +0000)
Signed-off-by: Christopher Ng <chrng8@gmail.com>
lib/private/Accounts/Account.php
lib/public/Accounts/IAccount.php
tests/lib/Accounts/AccountTest.php

index 7d36af561ce81e4be99fdebe47fe82e0f27bbd0d..d3287b219d0aab589f3b0cf6fef7adefa100762a 100644 (file)
@@ -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) {
index 8d4d8b5c0234f61553c43036e6901528ac811098..dcec17f9363d0b8692f8a6519ab333abe8f838d9 100644 (file)
@@ -71,6 +71,15 @@ 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
+        *
+        * @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.
index 064a9bf2732a69c0c427cd29509c872bc1b730aa..a86b1d0255b51e8e6480125e5c2522720b5a4d6e 100644 (file)
@@ -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 = [