group.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. class Group {
  10. /**
  11. * @var string $id
  12. */
  13. private $gid;
  14. /**
  15. * @var \OC\User\User[] $users
  16. */
  17. private $users;
  18. /**
  19. * @var \OC_Group_Backend[] | \OC_Group_Database[] $backend
  20. */
  21. private $backends;
  22. /**
  23. * @var \OC\Hooks\PublicEmitter $emitter;
  24. */
  25. private $emitter;
  26. /**
  27. * @var \OC\User\Manager $userManager
  28. */
  29. private $userManager;
  30. /**
  31. * @param string $gid
  32. * @param \OC_Group_Backend[] $backends
  33. * @param \OC\User\Manager $userManager
  34. * @param \OC\Hooks\PublicEmitter $emitter
  35. */
  36. public function __construct($gid, $backends, $userManager, $emitter = null) {
  37. $this->gid = $gid;
  38. $this->backends = $backends;
  39. $this->userManager = $userManager;
  40. $this->emitter = $emitter;
  41. }
  42. public function getGID() {
  43. return $this->gid;
  44. }
  45. /**
  46. * get all users in the group
  47. *
  48. * @return \OC\User\User[]
  49. */
  50. public function getUsers() {
  51. if ($this->users) {
  52. return $this->users;
  53. }
  54. $users = array();
  55. $userIds = array();
  56. foreach ($this->backends as $backend) {
  57. $diff = array_diff(
  58. $backend->usersInGroup($this->gid),
  59. $userIds
  60. );
  61. if ($diff) {
  62. $userIds = array_merge($userIds, $diff);
  63. }
  64. }
  65. foreach ($userIds as $userId) {
  66. $users[] = $this->userManager->get($userId);
  67. }
  68. $this->users = $users;
  69. return $users;
  70. }
  71. /**
  72. * check if a user is in the group
  73. *
  74. * @param \OC\User\User $user
  75. * @return bool
  76. */
  77. public function inGroup($user) {
  78. foreach ($this->backends as $backend) {
  79. if ($backend->inGroup($user->getUID(), $this->gid)) {
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. /**
  86. * add a user to the group
  87. *
  88. * @param \OC\User\User $user
  89. */
  90. public function addUser($user) {
  91. if ($this->inGroup($user)) {
  92. return;
  93. }
  94. if ($this->emitter) {
  95. $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
  96. }
  97. foreach ($this->backends as $backend) {
  98. if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) {
  99. $backend->addToGroup($user->getUID(), $this->gid);
  100. if ($this->users) {
  101. $this->users[] = $user;
  102. }
  103. if ($this->emitter) {
  104. $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
  105. }
  106. return;
  107. }
  108. }
  109. }
  110. /**
  111. * remove a user from the group
  112. *
  113. * @param \OC\User\User $user
  114. */
  115. public function removeUser($user) {
  116. $result = false;
  117. if ($this->emitter) {
  118. $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
  119. }
  120. foreach ($this->backends as $backend) {
  121. if ($backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
  122. $backend->removeFromGroup($user->getUID(), $this->gid);
  123. $result = true;
  124. }
  125. }
  126. if ($result) {
  127. if ($this->emitter) {
  128. $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
  129. }
  130. if ($this->users) {
  131. foreach ($this->users as $index => $groupUser) {
  132. if ($groupUser->getUID() === $user->getUID()) {
  133. unset($this->users[$index]);
  134. return;
  135. }
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * search for users in the group by userid
  142. *
  143. * @param string $search
  144. * @param int $limit
  145. * @param int $offset
  146. * @return \OC\User\User[]
  147. */
  148. public function searchUsers($search, $limit = null, $offset = null) {
  149. $users = array();
  150. foreach ($this->backends as $backend) {
  151. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  152. if (!is_null($limit)) {
  153. $limit -= count($userIds);
  154. }
  155. if (!is_null($offset)) {
  156. $offset -= count($userIds);
  157. }
  158. foreach ($userIds as $userId) {
  159. $users[$userId] = $this->userManager->get($userId);
  160. }
  161. if (!is_null($limit) and $limit <= 0) {
  162. return array_values($users);
  163. }
  164. }
  165. return array_values($users);
  166. }
  167. /**
  168. * search for users in the group by displayname
  169. *
  170. * @param string $search
  171. * @param int $limit
  172. * @param int $offset
  173. * @return \OC\User\User[]
  174. */
  175. public function searchDisplayName($search, $limit = null, $offset = null) {
  176. $users = array();
  177. foreach ($this->backends as $backend) {
  178. if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) {
  179. $userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset));
  180. } else {
  181. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  182. }
  183. if (!is_null($limit)) {
  184. $limit -= count($userIds);
  185. }
  186. if (!is_null($offset)) {
  187. $offset -= count($userIds);
  188. }
  189. foreach ($userIds as $userId) {
  190. $users[$userId] = $this->userManager->get($userId);
  191. }
  192. if (!is_null($limit) and $limit <= 0) {
  193. return array_values($users);
  194. }
  195. }
  196. return array_values($users);
  197. }
  198. /**
  199. * delete the group
  200. *
  201. * @return bool
  202. */
  203. public function delete() {
  204. $result = false;
  205. if ($this->emitter) {
  206. $this->emitter->emit('\OC\Group', 'preDelete', array($this));
  207. }
  208. foreach ($this->backends as $backend) {
  209. if ($backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP)) {
  210. $result = true;
  211. $backend->deleteGroup($this->gid);
  212. }
  213. }
  214. if ($result and $this->emitter) {
  215. $this->emitter->emit('\OC\Group', 'postDelete', array($this));
  216. }
  217. return $result;
  218. }
  219. }