VisibilityTrait.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_External\Lib;
  8. use OCA\Files_External\Service\BackendService;
  9. /**
  10. * Trait to implement visibility mechanics for a configuration class
  11. *
  12. * The standard visibility defines which users/groups can use or see the
  13. * object. The allowed visibility defines the maximum visibility allowed to be
  14. * set on the object. The standard visibility is often set dynamically by
  15. * stored configuration parameters that can be modified by the administrator,
  16. * while the allowed visibility is set directly by the object and cannot be
  17. * modified by the administrator.
  18. */
  19. trait VisibilityTrait {
  20. /** @var int visibility */
  21. protected $visibility = BackendService::VISIBILITY_DEFAULT;
  22. /** @var int allowed visibilities */
  23. protected $allowedVisibility = BackendService::VISIBILITY_DEFAULT;
  24. /**
  25. * @return int
  26. */
  27. public function getVisibility() {
  28. return $this->visibility;
  29. }
  30. /**
  31. * Check if the backend is visible for a user type
  32. *
  33. * @param int $visibility
  34. * @return bool
  35. */
  36. public function isVisibleFor($visibility) {
  37. if ($this->visibility & $visibility) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. /**
  43. * @param int $visibility
  44. * @return self
  45. */
  46. public function setVisibility($visibility) {
  47. $this->visibility = $visibility;
  48. $this->allowedVisibility |= $visibility;
  49. return $this;
  50. }
  51. /**
  52. * @param int $visibility
  53. * @return self
  54. */
  55. public function addVisibility($visibility) {
  56. return $this->setVisibility($this->visibility | $visibility);
  57. }
  58. /**
  59. * @param int $visibility
  60. * @return self
  61. */
  62. public function removeVisibility($visibility) {
  63. return $this->setVisibility($this->visibility & ~$visibility);
  64. }
  65. /**
  66. * @return int
  67. */
  68. public function getAllowedVisibility() {
  69. return $this->allowedVisibility;
  70. }
  71. /**
  72. * Check if the backend is allowed to be visible for a user type
  73. *
  74. * @param int $allowedVisibility
  75. * @return bool
  76. */
  77. public function isAllowedVisibleFor($allowedVisibility) {
  78. if ($this->allowedVisibility & $allowedVisibility) {
  79. return true;
  80. }
  81. return false;
  82. }
  83. /**
  84. * @param int $allowedVisibility
  85. * @return self
  86. */
  87. public function setAllowedVisibility($allowedVisibility) {
  88. $this->allowedVisibility = $allowedVisibility;
  89. $this->visibility &= $allowedVisibility;
  90. return $this;
  91. }
  92. /**
  93. * @param int $allowedVisibility
  94. * @return self
  95. */
  96. public function addAllowedVisibility($allowedVisibility) {
  97. return $this->setAllowedVisibility($this->allowedVisibility | $allowedVisibility);
  98. }
  99. /**
  100. * @param int $allowedVisibility
  101. * @return self
  102. */
  103. public function removeAllowedVisibility($allowedVisibility) {
  104. return $this->setAllowedVisibility($this->allowedVisibility & ~$allowedVisibility);
  105. }
  106. }