AccountProperty.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. private string $scope;
  32. private string $locallyVerified = IAccountManager::NOT_VERIFIED;
  33. public function __construct(
  34. private string $name,
  35. private string $value,
  36. string $scope,
  37. private string $verified,
  38. private string $verificationData,
  39. ) {
  40. $this->setScope($scope);
  41. }
  42. public function jsonSerialize(): array {
  43. return [
  44. 'name' => $this->getName(),
  45. 'value' => $this->getValue(),
  46. 'scope' => $this->getScope(),
  47. 'verified' => $this->getVerified(),
  48. 'verificationData' => $this->getVerificationData(),
  49. ];
  50. }
  51. /**
  52. * Set the value of a property
  53. *
  54. * @since 15.0.0
  55. */
  56. public function setValue(string $value): IAccountProperty {
  57. $this->value = $value;
  58. return $this;
  59. }
  60. /**
  61. * Set the scope of a property
  62. *
  63. * @since 15.0.0
  64. */
  65. public function setScope(string $scope): IAccountProperty {
  66. $newScope = $this->mapScopeToV2($scope);
  67. if (!in_array($newScope, [
  68. IAccountManager::SCOPE_LOCAL,
  69. IAccountManager::SCOPE_FEDERATED,
  70. IAccountManager::SCOPE_PRIVATE,
  71. IAccountManager::SCOPE_PUBLISHED
  72. ])) {
  73. throw new InvalidArgumentException('Invalid scope');
  74. }
  75. $this->scope = $newScope;
  76. return $this;
  77. }
  78. /**
  79. * Set the verification status of a property
  80. *
  81. * @since 15.0.0
  82. */
  83. public function setVerified(string $verified): IAccountProperty {
  84. $this->verified = $verified;
  85. return $this;
  86. }
  87. /**
  88. * Get the name of a property
  89. *
  90. * @since 15.0.0
  91. */
  92. public function getName(): string {
  93. return $this->name;
  94. }
  95. /**
  96. * Get the value of a property
  97. *
  98. * @since 15.0.0
  99. */
  100. public function getValue(): string {
  101. return $this->value;
  102. }
  103. /**
  104. * Get the scope of a property
  105. *
  106. * @since 15.0.0
  107. */
  108. public function getScope(): string {
  109. return $this->scope;
  110. }
  111. public static function mapScopeToV2(string $scope): string {
  112. if (str_starts_with($scope, 'v2-')) {
  113. return $scope;
  114. }
  115. return match ($scope) {
  116. IAccountManager::VISIBILITY_PRIVATE, '' => IAccountManager::SCOPE_LOCAL,
  117. IAccountManager::VISIBILITY_CONTACTS_ONLY => IAccountManager::SCOPE_FEDERATED,
  118. IAccountManager::VISIBILITY_PUBLIC => IAccountManager::SCOPE_PUBLISHED,
  119. default => $scope,
  120. };
  121. }
  122. /**
  123. * Get the verification status of a property
  124. *
  125. * @since 15.0.0
  126. */
  127. public function getVerified(): string {
  128. return $this->verified;
  129. }
  130. public function setVerificationData(string $verificationData): IAccountProperty {
  131. $this->verificationData = $verificationData;
  132. return $this;
  133. }
  134. public function getVerificationData(): string {
  135. return $this->verificationData;
  136. }
  137. public function setLocallyVerified(string $verified): IAccountProperty {
  138. if (!in_array($verified, [
  139. IAccountManager::NOT_VERIFIED,
  140. IAccountManager::VERIFICATION_IN_PROGRESS,
  141. IAccountManager::VERIFIED,
  142. ])) {
  143. throw new InvalidArgumentException('Provided verification value is invalid');
  144. }
  145. $this->locallyVerified = $verified;
  146. return $this;
  147. }
  148. public function getLocallyVerified(): string {
  149. return $this->locallyVerified;
  150. }
  151. }