VisibilityTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin McCorkell <robin@mccorkell.me.uk>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files_External\Lib;
  23. use \OCA\Files_External\Service\BackendService;
  24. /**
  25. * Trait to implement visibility mechanics for a configuration class
  26. *
  27. * The standard visibility defines which users/groups can use or see the
  28. * object. The allowed visibility defines the maximum visibility allowed to be
  29. * set on the object. The standard visibility is often set dynamically by
  30. * stored configuration parameters that can be modified by the administrator,
  31. * while the allowed visibility is set directly by the object and cannot be
  32. * modified by the administrator.
  33. */
  34. trait VisibilityTrait {
  35. /** @var int visibility */
  36. protected $visibility = BackendService::VISIBILITY_DEFAULT;
  37. /** @var int allowed visibilities */
  38. protected $allowedVisibility = BackendService::VISIBILITY_DEFAULT;
  39. /**
  40. * @return int
  41. */
  42. public function getVisibility() {
  43. return $this->visibility;
  44. }
  45. /**
  46. * Check if the backend is visible for a user type
  47. *
  48. * @param int $visibility
  49. * @return bool
  50. */
  51. public function isVisibleFor($visibility) {
  52. if ($this->visibility & $visibility) {
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * @param int $visibility
  59. * @return self
  60. */
  61. public function setVisibility($visibility) {
  62. $this->visibility = $visibility;
  63. $this->allowedVisibility |= $visibility;
  64. return $this;
  65. }
  66. /**
  67. * @param int $visibility
  68. * @return self
  69. */
  70. public function addVisibility($visibility) {
  71. return $this->setVisibility($this->visibility | $visibility);
  72. }
  73. /**
  74. * @param int $visibility
  75. * @return self
  76. */
  77. public function removeVisibility($visibility) {
  78. return $this->setVisibility($this->visibility & ~$visibility);
  79. }
  80. /**
  81. * @return int
  82. */
  83. public function getAllowedVisibility() {
  84. return $this->allowedVisibility;
  85. }
  86. /**
  87. * Check if the backend is allowed to be visible for a user type
  88. *
  89. * @param int $allowedVisibility
  90. * @return bool
  91. */
  92. public function isAllowedVisibleFor($allowedVisibility) {
  93. if ($this->allowedVisibility & $allowedVisibility) {
  94. return true;
  95. }
  96. return false;
  97. }
  98. /**
  99. * @param int $allowedVisibility
  100. * @return self
  101. */
  102. public function setAllowedVisibility($allowedVisibility) {
  103. $this->allowedVisibility = $allowedVisibility;
  104. $this->visibility &= $allowedVisibility;
  105. return $this;
  106. }
  107. /**
  108. * @param int $allowedVisibility
  109. * @return self
  110. */
  111. public function addAllowedVisibility($allowedVisibility) {
  112. return $this->setAllowedVisibility($this->allowedVisibility | $allowedVisibility);
  113. }
  114. /**
  115. * @param int $allowedVisibility
  116. * @return self
  117. */
  118. public function removeAllowedVisibility($allowedVisibility) {
  119. return $this->setAllowedVisibility($this->allowedVisibility & ~$allowedVisibility);
  120. }
  121. }