1
0

GroupPluginManager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  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 OCA\User_LDAP;
  25. use OCP\GroupInterface;
  26. class GroupPluginManager {
  27. private int $respondToActions = 0;
  28. /** @var array<int, ?ILDAPGroupPlugin> */
  29. private array $which = [
  30. GroupInterface::CREATE_GROUP => null,
  31. GroupInterface::DELETE_GROUP => null,
  32. GroupInterface::ADD_TO_GROUP => null,
  33. GroupInterface::REMOVE_FROM_GROUP => null,
  34. GroupInterface::COUNT_USERS => null,
  35. GroupInterface::GROUP_DETAILS => null
  36. ];
  37. private bool $suppressDeletion = false;
  38. /**
  39. * @return int All implemented actions
  40. */
  41. public function getImplementedActions() {
  42. return $this->respondToActions;
  43. }
  44. /**
  45. * Registers a group plugin that may implement some actions, overriding User_LDAP's group actions.
  46. * @param ILDAPGroupPlugin $plugin
  47. */
  48. public function register(ILDAPGroupPlugin $plugin) {
  49. $respondToActions = $plugin->respondToActions();
  50. $this->respondToActions |= $respondToActions;
  51. foreach ($this->which as $action => $v) {
  52. if ((bool)($respondToActions & $action)) {
  53. $this->which[$action] = $plugin;
  54. \OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
  55. }
  56. }
  57. }
  58. /**
  59. * Signal if there is a registered plugin that implements some given actions
  60. * @param int $actions Actions defined in \OCP\GroupInterface, like GroupInterface::REMOVE_FROM_GROUP
  61. * @return bool
  62. */
  63. public function implementsActions($actions) {
  64. return ($actions & $this->respondToActions) == $actions;
  65. }
  66. /**
  67. * Create a group
  68. * @param string $gid Group Id
  69. * @return string | null The group DN if group creation was successful.
  70. * @throws \Exception
  71. */
  72. public function createGroup($gid) {
  73. $plugin = $this->which[GroupInterface::CREATE_GROUP];
  74. if ($plugin) {
  75. return $plugin->createGroup($gid);
  76. }
  77. throw new \Exception('No plugin implements createGroup in this LDAP Backend.');
  78. }
  79. public function canDeleteGroup(): bool {
  80. return !$this->suppressDeletion && $this->implementsActions(GroupInterface::DELETE_GROUP);
  81. }
  82. /**
  83. * @return bool – the value before the change
  84. */
  85. public function setSuppressDeletion(bool $value): bool {
  86. $old = $this->suppressDeletion;
  87. $this->suppressDeletion = $value;
  88. return $old;
  89. }
  90. /**
  91. * Delete a group
  92. *
  93. * @throws \Exception
  94. */
  95. public function deleteGroup(string $gid): bool {
  96. $plugin = $this->which[GroupInterface::DELETE_GROUP];
  97. if ($plugin) {
  98. if ($this->suppressDeletion) {
  99. return false;
  100. }
  101. return $plugin->deleteGroup($gid);
  102. }
  103. throw new \Exception('No plugin implements deleteGroup in this LDAP Backend.');
  104. }
  105. /**
  106. * Add a user to a group
  107. * @param string $uid ID of the user to add to group
  108. * @param string $gid ID of the group in which add the user
  109. * @return bool
  110. * @throws \Exception
  111. *
  112. * Adds a user to a group.
  113. */
  114. public function addToGroup($uid, $gid) {
  115. $plugin = $this->which[GroupInterface::ADD_TO_GROUP];
  116. if ($plugin) {
  117. return $plugin->addToGroup($uid, $gid);
  118. }
  119. throw new \Exception('No plugin implements addToGroup in this LDAP Backend.');
  120. }
  121. /**
  122. * Removes a user from a group
  123. * @param string $uid ID of the user to remove from group
  124. * @param string $gid ID of the group from which remove the user
  125. * @return bool
  126. * @throws \Exception
  127. *
  128. * removes the user from a group.
  129. */
  130. public function removeFromGroup($uid, $gid) {
  131. $plugin = $this->which[GroupInterface::REMOVE_FROM_GROUP];
  132. if ($plugin) {
  133. return $plugin->removeFromGroup($uid, $gid);
  134. }
  135. throw new \Exception('No plugin implements removeFromGroup in this LDAP Backend.');
  136. }
  137. /**
  138. * get the number of all users matching the search string in a group
  139. * @param string $gid ID of the group
  140. * @param string $search query string
  141. * @return int|false
  142. * @throws \Exception
  143. */
  144. public function countUsersInGroup($gid, $search = '') {
  145. $plugin = $this->which[GroupInterface::COUNT_USERS];
  146. if ($plugin) {
  147. return $plugin->countUsersInGroup($gid, $search);
  148. }
  149. throw new \Exception('No plugin implements countUsersInGroup in this LDAP Backend.');
  150. }
  151. /**
  152. * get an array with group details
  153. * @param string $gid
  154. * @return array|false
  155. * @throws \Exception
  156. */
  157. public function getGroupDetails($gid) {
  158. $plugin = $this->which[GroupInterface::GROUP_DETAILS];
  159. if ($plugin) {
  160. return $plugin->getGroupDetails($gid);
  161. }
  162. throw new \Exception('No plugin implements getGroupDetails in this LDAP Backend.');
  163. }
  164. }