VisibilityTrait.php 3.4 KB

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