GroupPluginManager.php 4.5 KB

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