GroupPluginManager.php 4.5 KB

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