group.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. $userIds = array();
  55. foreach ($this->backends as $backend) {
  56. $diff = array_diff(
  57. $backend->usersInGroup($this->gid),
  58. $userIds
  59. );
  60. if ($diff) {
  61. $userIds = array_merge($userIds, $diff);
  62. }
  63. }
  64. $this->users = $this->getVerifiedUsers($userIds);
  65. return $this->users;
  66. }
  67. /**
  68. * check if a user is in the group
  69. *
  70. * @param \OC\User\User $user
  71. * @return bool
  72. */
  73. public function inGroup($user) {
  74. foreach ($this->backends as $backend) {
  75. if ($backend->inGroup($user->getUID(), $this->gid)) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81. /**
  82. * add a user to the group
  83. *
  84. * @param \OC\User\User $user
  85. */
  86. public function addUser($user) {
  87. if ($this->inGroup($user)) {
  88. return;
  89. }
  90. if ($this->emitter) {
  91. $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
  92. }
  93. foreach ($this->backends as $backend) {
  94. if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) {
  95. $backend->addToGroup($user->getUID(), $this->gid);
  96. if ($this->users) {
  97. $this->users[$user->getUID()] = $user;
  98. }
  99. if ($this->emitter) {
  100. $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
  101. }
  102. return;
  103. }
  104. }
  105. }
  106. /**
  107. * remove a user from the group
  108. *
  109. * @param \OC\User\User $user
  110. */
  111. public function removeUser($user) {
  112. $result = false;
  113. if ($this->emitter) {
  114. $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
  115. }
  116. foreach ($this->backends as $backend) {
  117. if ($backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
  118. $backend->removeFromGroup($user->getUID(), $this->gid);
  119. $result = true;
  120. }
  121. }
  122. if ($result) {
  123. if ($this->emitter) {
  124. $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
  125. }
  126. if ($this->users) {
  127. foreach ($this->users as $index => $groupUser) {
  128. if ($groupUser->getUID() === $user->getUID()) {
  129. unset($this->users[$index]);
  130. return;
  131. }
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * search for users in the group by userid
  138. *
  139. * @param string $search
  140. * @param int $limit
  141. * @param int $offset
  142. * @return \OC\User\User[]
  143. */
  144. public function searchUsers($search, $limit = null, $offset = null) {
  145. $users = array();
  146. foreach ($this->backends as $backend) {
  147. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  148. if (!is_null($limit)) {
  149. $limit -= count($userIds);
  150. }
  151. if (!is_null($offset)) {
  152. $offset -= count($userIds);
  153. }
  154. $users += $this->getVerifiedUsers($userIds);
  155. if (!is_null($limit) and $limit <= 0) {
  156. return array_values($users);
  157. }
  158. }
  159. return array_values($users);
  160. }
  161. /**
  162. * search for users in the group by displayname
  163. *
  164. * @param string $search
  165. * @param int $limit
  166. * @param int $offset
  167. * @return \OC\User\User[]
  168. */
  169. public function searchDisplayName($search, $limit = null, $offset = null) {
  170. foreach ($this->backends as $backend) {
  171. if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) {
  172. $userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset));
  173. } else {
  174. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  175. }
  176. if (!is_null($limit)) {
  177. $limit -= count($userIds);
  178. }
  179. if (!is_null($offset)) {
  180. $offset -= count($userIds);
  181. }
  182. $users = $this->getVerifiedUsers($userIds);
  183. if (!is_null($limit) and $limit <= 0) {
  184. return array_values($users);
  185. }
  186. }
  187. return array_values($users);
  188. }
  189. /**
  190. * delete the group
  191. *
  192. * @return bool
  193. */
  194. public function delete() {
  195. $result = false;
  196. if ($this->emitter) {
  197. $this->emitter->emit('\OC\Group', 'preDelete', array($this));
  198. }
  199. foreach ($this->backends as $backend) {
  200. if ($backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP)) {
  201. $result = true;
  202. $backend->deleteGroup($this->gid);
  203. }
  204. }
  205. if ($result and $this->emitter) {
  206. $this->emitter->emit('\OC\Group', 'postDelete', array($this));
  207. }
  208. return $result;
  209. }
  210. /**
  211. * @brief returns all the Users from an array that really exists
  212. * @param $userIds an array containing user IDs
  213. * @return an Array with the userId as Key and \OC\User\User as value
  214. */
  215. private function getVerifiedUsers($userIds) {
  216. if(!is_array($userIds)) {
  217. return array();
  218. }
  219. $users = array();
  220. foreach ($userIds as $userId) {
  221. $user = $this->userManager->get($userId);
  222. if(!is_null($user)) {
  223. $users[$userId] = $user;
  224. }
  225. }
  226. return $users;
  227. }
  228. }