1
0

GroupPluginManager.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
  4. *
  5. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\User_LDAP;
  24. use OCP\GroupInterface;
  25. class GroupPluginManager {
  26. private $respondToActions = 0;
  27. private $which = array(
  28. GroupInterface::CREATE_GROUP => null,
  29. GroupInterface::DELETE_GROUP => null,
  30. GroupInterface::ADD_TO_GROUP => null,
  31. GroupInterface::REMOVE_FROM_GROUP => null,
  32. GroupInterface::COUNT_USERS => null,
  33. GroupInterface::GROUP_DETAILS => null
  34. );
  35. /**
  36. * @return int All implemented actions
  37. */
  38. public function getImplementedActions() {
  39. return $this->respondToActions;
  40. }
  41. /**
  42. * Registers a group plugin that may implement some actions, overriding User_LDAP's group actions.
  43. * @param ILDAPGroupPlugin $plugin
  44. */
  45. public function register(ILDAPGroupPlugin $plugin) {
  46. $respondToActions = $plugin->respondToActions();
  47. $this->respondToActions |= $respondToActions;
  48. foreach($this->which as $action => $v) {
  49. if ((bool)($respondToActions & $action)) {
  50. $this->which[$action] = $plugin;
  51. \OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
  52. }
  53. }
  54. }
  55. /**
  56. * Signal if there is a registered plugin that implements some given actions
  57. * @param int $actions Actions defined in \OCP\GroupInterface, like GroupInterface::REMOVE_FROM_GROUP
  58. * @return bool
  59. */
  60. public function implementsActions($actions) {
  61. return ($actions & $this->respondToActions) == $actions;
  62. }
  63. /**
  64. * Create a group
  65. * @param string $gid Group Id
  66. * @return string | null The group DN if group creation was successful.
  67. * @throws \Exception
  68. */
  69. public function createGroup($gid) {
  70. $plugin = $this->which[GroupInterface::CREATE_GROUP];
  71. if ($plugin) {
  72. return $plugin->createGroup($gid);
  73. }
  74. throw new \Exception('No plugin implements createGroup in this LDAP Backend.');
  75. }
  76. /**
  77. * Delete a group
  78. * @param string $gid Group Id of the group to delete
  79. * @return bool
  80. * @throws \Exception
  81. */
  82. public function deleteGroup($gid) {
  83. $plugin = $this->which[GroupInterface::DELETE_GROUP];
  84. if ($plugin) {
  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. }