GroupPluginManager.php 4.8 KB

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