1
0

AccountProperty.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Vincent Petry <vincent@nextcloud.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Accounts;
  27. use InvalidArgumentException;
  28. use OCP\Accounts\IAccountManager;
  29. use OCP\Accounts\IAccountProperty;
  30. class AccountProperty implements IAccountProperty {
  31. /** @var string */
  32. private $name;
  33. /** @var string */
  34. private $value;
  35. /** @var string */
  36. private $scope;
  37. /** @var string */
  38. private $verified;
  39. /** @var string */
  40. private $verificationData;
  41. /** @var string */
  42. private $locallyVerified = IAccountManager::NOT_VERIFIED;
  43. public function __construct(string $name, string $value, string $scope, string $verified, string $verificationData) {
  44. $this->name = $name;
  45. $this->value = $value;
  46. $this->setScope($scope);
  47. $this->verified = $verified;
  48. $this->verificationData = $verificationData;
  49. }
  50. public function jsonSerialize(): array {
  51. return [
  52. 'name' => $this->getName(),
  53. 'value' => $this->getValue(),
  54. 'scope' => $this->getScope(),
  55. 'verified' => $this->getVerified(),
  56. 'verificationData' => $this->getVerificationData(),
  57. ];
  58. }
  59. /**
  60. * Set the value of a property
  61. *
  62. * @since 15.0.0
  63. *
  64. * @param string $value
  65. * @return IAccountProperty
  66. */
  67. public function setValue(string $value): IAccountProperty {
  68. $this->value = $value;
  69. return $this;
  70. }
  71. /**
  72. * Set the scope of a property
  73. *
  74. * @since 15.0.0
  75. *
  76. * @param string $scope
  77. * @return IAccountProperty
  78. */
  79. public function setScope(string $scope): IAccountProperty {
  80. $newScope = $this->mapScopeToV2($scope);
  81. if (!in_array($newScope, [
  82. IAccountManager::SCOPE_LOCAL,
  83. IAccountManager::SCOPE_FEDERATED,
  84. IAccountManager::SCOPE_PRIVATE,
  85. IAccountManager::SCOPE_PUBLISHED
  86. ])) {
  87. throw new InvalidArgumentException('Invalid scope');
  88. }
  89. $this->scope = $newScope;
  90. return $this;
  91. }
  92. /**
  93. * Set the verification status of a property
  94. *
  95. * @since 15.0.0
  96. *
  97. * @param string $verified
  98. * @return IAccountProperty
  99. */
  100. public function setVerified(string $verified): IAccountProperty {
  101. $this->verified = $verified;
  102. return $this;
  103. }
  104. /**
  105. * Get the name of a property
  106. *
  107. * @since 15.0.0
  108. *
  109. * @return string
  110. */
  111. public function getName(): string {
  112. return $this->name;
  113. }
  114. /**
  115. * Get the value of a property
  116. *
  117. * @since 15.0.0
  118. *
  119. * @return string
  120. */
  121. public function getValue(): string {
  122. return $this->value;
  123. }
  124. /**
  125. * Get the scope of a property
  126. *
  127. * @since 15.0.0
  128. *
  129. * @return string
  130. */
  131. public function getScope(): string {
  132. return $this->scope;
  133. }
  134. public static function mapScopeToV2(string $scope): string {
  135. if (strpos($scope, 'v2-') === 0) {
  136. return $scope;
  137. }
  138. switch ($scope) {
  139. case IAccountManager::VISIBILITY_PRIVATE:
  140. case '':
  141. return IAccountManager::SCOPE_LOCAL;
  142. case IAccountManager::VISIBILITY_CONTACTS_ONLY:
  143. return IAccountManager::SCOPE_FEDERATED;
  144. case IAccountManager::VISIBILITY_PUBLIC:
  145. return IAccountManager::SCOPE_PUBLISHED;
  146. default:
  147. return $scope;
  148. }
  149. }
  150. /**
  151. * Get the verification status of a property
  152. *
  153. * @since 15.0.0
  154. *
  155. * @return string
  156. */
  157. public function getVerified(): string {
  158. return $this->verified;
  159. }
  160. public function setVerificationData(string $verificationData): IAccountProperty {
  161. $this->verificationData = $verificationData;
  162. return $this;
  163. }
  164. public function getVerificationData(): string {
  165. return $this->verificationData;
  166. }
  167. public function setLocallyVerified(string $verified): IAccountProperty {
  168. if (!in_array($verified, [
  169. IAccountManager::NOT_VERIFIED,
  170. IAccountManager::VERIFICATION_IN_PROGRESS,
  171. IAccountManager::VERIFIED,
  172. ])) {
  173. throw new InvalidArgumentException('Provided verification value is invalid');
  174. }
  175. $this->locallyVerified = $verified;
  176. return $this;
  177. }
  178. public function getLocallyVerified(): string {
  179. return $this->locallyVerified;
  180. }
  181. }