manager.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Group;
  9. use OC\Hooks\PublicEmitter;
  10. /**
  11. * Class Manager
  12. *
  13. * Hooks available in scope \OC\Group:
  14. * - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
  15. * - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
  16. * - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
  17. * - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
  18. * - preDelete(\OC\Group\Group $group)
  19. * - postDelete(\OC\Group\Group $group)
  20. * - preCreate(string $groupId)
  21. * - postCreate(\OC\Group\Group $group)
  22. *
  23. * @package OC\Group
  24. */
  25. class Manager extends PublicEmitter {
  26. /**
  27. * @var \OC_Group_Backend[] | \OC_Group_Database[] $backends
  28. */
  29. private $backends = array();
  30. /**
  31. * @var \OC\User\Manager $userManager
  32. */
  33. private $userManager;
  34. /**
  35. * @var \OC\Group\Group[]
  36. */
  37. private $cachedGroups;
  38. /**
  39. * @param \OC\User\Manager $userManager
  40. */
  41. public function __construct($userManager) {
  42. $this->userManager = $userManager;
  43. $cache = & $this->cachedGroups;
  44. $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cache) {
  45. /**
  46. * @var \OC\Group\Group $group
  47. */
  48. unset($cache[$group->getGID()]);
  49. });
  50. }
  51. /**
  52. * @param \OC_Group_Backend $backend
  53. */
  54. public function addBackend($backend) {
  55. $this->backends[] = $backend;
  56. }
  57. public function clearBackends() {
  58. $this->backends = array();
  59. $this->cachedGroups = array();
  60. }
  61. /**
  62. * @param string $gid
  63. * @return \OC\Group\Group
  64. */
  65. public function get($gid) {
  66. if (isset($this->cachedGroups[$gid])) {
  67. return $this->cachedGroups[$gid];
  68. }
  69. foreach ($this->backends as $backend) {
  70. if ($backend->groupExists($gid)) {
  71. return $this->getGroupObject($gid);
  72. }
  73. }
  74. return null;
  75. }
  76. protected function getGroupObject($gid) {
  77. $backends = array();
  78. foreach ($this->backends as $backend) {
  79. if ($backend->groupExists($gid)) {
  80. $backends[] = $backend;
  81. }
  82. }
  83. $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this);
  84. return $this->cachedGroups[$gid];
  85. }
  86. /**
  87. * @param string $gid
  88. * @return bool
  89. */
  90. public function groupExists($gid) {
  91. return !is_null($this->get($gid));
  92. }
  93. /**
  94. * @param string $gid
  95. * @return \OC\Group\Group
  96. */
  97. public function createGroup($gid) {
  98. if (!$gid) {
  99. return false;
  100. } else if ($this->groupExists($gid)) {
  101. return $this->get($gid);
  102. } else {
  103. $this->emit('\OC\Group', 'preCreate', array($gid));
  104. foreach ($this->backends as $backend) {
  105. if ($backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) {
  106. $backend->createGroup($gid);
  107. $group = $this->getGroupObject($gid);
  108. $this->emit('\OC\Group', 'postCreate', array($group));
  109. return $group;
  110. }
  111. }
  112. return null;
  113. }
  114. }
  115. /**
  116. * @param string $search
  117. * @param int $limit
  118. * @param int $offset
  119. * @return \OC\Group\Group[]
  120. */
  121. public function search($search, $limit = null, $offset = null) {
  122. $groups = array();
  123. foreach ($this->backends as $backend) {
  124. $groupIds = $backend->getGroups($search, $limit, $offset);
  125. if (!is_null($limit)) {
  126. $limit -= count($groupIds);
  127. }
  128. if (!is_null($offset)) {
  129. $offset -= count($groupIds);
  130. }
  131. foreach ($groupIds as $groupId) {
  132. $groups[$groupId] = $this->getGroupObject($groupId);
  133. }
  134. if (!is_null($limit) and $limit <= 0) {
  135. return array_values($groups);
  136. }
  137. }
  138. return array_values($groups);
  139. }
  140. /**
  141. * @param \OC\User\User $user
  142. * @return \OC\Group\Group[]
  143. */
  144. public function getUserGroups($user) {
  145. $groups = array();
  146. foreach ($this->backends as $backend) {
  147. $groupIds = $backend->getUserGroups($user->getUID());
  148. foreach ($groupIds as $groupId) {
  149. $groups[$groupId] = $this->getGroupObject($groupId);
  150. }
  151. }
  152. return array_values($groups);
  153. }
  154. }