visibilitytrait.php 3.4 KB

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