GroupPluginManager.php 5.3 KB

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