ProfileConfig.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christopher Ng <chrng8@gmail.com>
  5. *
  6. * @author Christopher Ng <chrng8@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\Db;
  25. use function Safe\json_decode;
  26. use function Safe\json_encode;
  27. use \JsonSerializable;
  28. use OCP\Accounts\IAccountManager;
  29. use OCP\AppFramework\Db\Entity;
  30. use OCP\Profile\ParameterDoesNotExistException;
  31. /**
  32. * @method string getUserId()
  33. * @method void setUserId(string $userId)
  34. * @method string getConfig()
  35. * @method void setConfig(string $config)
  36. */
  37. class ProfileConfig extends Entity implements JsonSerializable {
  38. /**
  39. * Visible to users, guests, and public access
  40. *
  41. * @since 23.0.0
  42. */
  43. public const VISIBILITY_SHOW = 'show';
  44. /**
  45. * Visible to users and guests
  46. *
  47. * @since 23.0.0
  48. */
  49. public const VISIBILITY_SHOW_USERS_ONLY = 'show_users_only';
  50. /**
  51. * Visible to nobody
  52. *
  53. * @since 23.0.0
  54. */
  55. public const VISIBILITY_HIDE = 'hide';
  56. /**
  57. * Default account property visibility
  58. *
  59. * @since 23.0.0
  60. */
  61. public const DEFAULT_PROPERTY_VISIBILITY = [
  62. IAccountManager::PROPERTY_ADDRESS => self::VISIBILITY_SHOW_USERS_ONLY,
  63. IAccountManager::PROPERTY_AVATAR => self::VISIBILITY_SHOW,
  64. IAccountManager::PROPERTY_BIOGRAPHY => self::VISIBILITY_SHOW,
  65. IAccountManager::PROPERTY_DISPLAYNAME => self::VISIBILITY_SHOW,
  66. IAccountManager::PROPERTY_HEADLINE => self::VISIBILITY_SHOW,
  67. IAccountManager::PROPERTY_ORGANISATION => self::VISIBILITY_SHOW,
  68. IAccountManager::PROPERTY_ROLE => self::VISIBILITY_SHOW,
  69. IAccountManager::PROPERTY_EMAIL => self::VISIBILITY_SHOW_USERS_ONLY,
  70. IAccountManager::PROPERTY_PHONE => self::VISIBILITY_SHOW_USERS_ONLY,
  71. IAccountManager::PROPERTY_TWITTER => self::VISIBILITY_SHOW,
  72. IAccountManager::PROPERTY_WEBSITE => self::VISIBILITY_SHOW,
  73. ];
  74. /**
  75. * Default visibility
  76. *
  77. * @since 23.0.0
  78. */
  79. public const DEFAULT_VISIBILITY = self::VISIBILITY_SHOW_USERS_ONLY;
  80. /** @var string */
  81. protected $userId;
  82. /** @var string */
  83. protected $config;
  84. public function __construct() {
  85. $this->addType('userId', 'string');
  86. $this->addType('config', 'string');
  87. }
  88. /**
  89. * Returns the config in an associative array
  90. */
  91. public function getConfigArray(): array {
  92. return json_decode($this->config, true);
  93. }
  94. /**
  95. * Set the config
  96. */
  97. public function setConfigArray(array $config): void {
  98. $this->setConfig(json_encode($config));
  99. }
  100. /**
  101. * Returns the visibility map in an associative array
  102. */
  103. public function getVisibilityMap(): array {
  104. $config = $this->getConfigArray();
  105. $visibilityMap = [];
  106. foreach ($config as $paramId => $paramConfig) {
  107. $visibilityMap[$paramId] = $paramConfig['visibility'];
  108. }
  109. return $visibilityMap;
  110. }
  111. /**
  112. * Set the visibility map
  113. */
  114. public function setVisibilityMap(array $visibilityMap): void {
  115. $config = $this->getConfigArray();
  116. foreach ($visibilityMap as $paramId => $visibility) {
  117. $config[$paramId] = array_merge(
  118. $config[$paramId] ?: [],
  119. ['visibility' => $visibility],
  120. );
  121. }
  122. $this->setConfigArray($config);
  123. }
  124. /**
  125. * Returns the visibility of the parameter
  126. *
  127. * @throws ParameterDoesNotExistException
  128. */
  129. public function getVisibility(string $paramId): string {
  130. $visibilityMap = $this->getVisibilityMap();
  131. if (isset($visibilityMap[$paramId])) {
  132. return $visibilityMap[$paramId];
  133. }
  134. throw new ParameterDoesNotExistException($paramId);
  135. }
  136. /**
  137. * Set the visibility of the parameter
  138. */
  139. public function setVisibility(string $paramId, string $visibility): void {
  140. $visibilityMap = $this->getVisibilityMap();
  141. $visibilityMap[$paramId] = $visibility;
  142. $this->setVisibilityMap($visibilityMap);
  143. }
  144. public function jsonSerialize(): array {
  145. return [
  146. 'userId' => $this->userId,
  147. 'config' => $this->getConfigArray(),
  148. ];
  149. }
  150. }