1
0

AccountProperty.php 4.1 KB

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