group.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Joas Schilling <nickvergessen@owncloud.com>
  6. * @author Lukas Reschke <lukas@owncloud.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  10. *
  11. * @copyright Copyright (c) 2015, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Group;
  28. use OCP\IGroup;
  29. class Group implements IGroup {
  30. /**
  31. * @var string $id
  32. */
  33. private $gid;
  34. /**
  35. * @var \OC\User\User[] $users
  36. */
  37. private $users = array();
  38. /**
  39. * @var bool $usersLoaded
  40. */
  41. private $usersLoaded;
  42. /**
  43. * @var \OC_Group_Backend[]|\OC_Group_Database[] $backend
  44. */
  45. private $backends;
  46. /**
  47. * @var \OC\Hooks\PublicEmitter $emitter
  48. */
  49. private $emitter;
  50. /**
  51. * @var \OC\User\Manager $userManager
  52. */
  53. private $userManager;
  54. /**
  55. * @param string $gid
  56. * @param \OC_Group_Backend[] $backends
  57. * @param \OC\User\Manager $userManager
  58. * @param \OC\Hooks\PublicEmitter $emitter
  59. */
  60. public function __construct($gid, $backends, $userManager, $emitter = null) {
  61. $this->gid = $gid;
  62. $this->backends = $backends;
  63. $this->userManager = $userManager;
  64. $this->emitter = $emitter;
  65. }
  66. public function getGID() {
  67. return $this->gid;
  68. }
  69. /**
  70. * get all users in the group
  71. *
  72. * @return \OC\User\User[]
  73. */
  74. public function getUsers() {
  75. if ($this->usersLoaded) {
  76. return $this->users;
  77. }
  78. $userIds = array();
  79. foreach ($this->backends as $backend) {
  80. $diff = array_diff(
  81. $backend->usersInGroup($this->gid),
  82. $userIds
  83. );
  84. if ($diff) {
  85. $userIds = array_merge($userIds, $diff);
  86. }
  87. }
  88. $this->users = $this->getVerifiedUsers($userIds);
  89. $this->usersLoaded = true;
  90. return $this->users;
  91. }
  92. /**
  93. * check if a user is in the group
  94. *
  95. * @param \OC\User\User $user
  96. * @return bool
  97. */
  98. public function inGroup($user) {
  99. if (isset($this->users[$user->getUID()])) {
  100. return true;
  101. }
  102. foreach ($this->backends as $backend) {
  103. if ($backend->inGroup($user->getUID(), $this->gid)) {
  104. $this->users[$user->getUID()] = $user;
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110. /**
  111. * add a user to the group
  112. *
  113. * @param \OC\User\User $user
  114. */
  115. public function addUser($user) {
  116. if ($this->inGroup($user)) {
  117. return;
  118. }
  119. if ($this->emitter) {
  120. $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
  121. }
  122. foreach ($this->backends as $backend) {
  123. if ($backend->implementsActions(\OC_Group_Backend::ADD_TO_GROUP)) {
  124. $backend->addToGroup($user->getUID(), $this->gid);
  125. if ($this->users) {
  126. $this->users[$user->getUID()] = $user;
  127. }
  128. if ($this->emitter) {
  129. $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
  130. }
  131. return;
  132. }
  133. }
  134. }
  135. /**
  136. * remove a user from the group
  137. *
  138. * @param \OC\User\User $user
  139. */
  140. public function removeUser($user) {
  141. $result = false;
  142. if ($this->emitter) {
  143. $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
  144. }
  145. foreach ($this->backends as $backend) {
  146. if ($backend->implementsActions(\OC_Group_Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
  147. $backend->removeFromGroup($user->getUID(), $this->gid);
  148. $result = true;
  149. }
  150. }
  151. if ($result) {
  152. if ($this->emitter) {
  153. $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
  154. }
  155. if ($this->users) {
  156. foreach ($this->users as $index => $groupUser) {
  157. if ($groupUser->getUID() === $user->getUID()) {
  158. unset($this->users[$index]);
  159. return;
  160. }
  161. }
  162. }
  163. }
  164. }
  165. /**
  166. * search for users in the group by userid
  167. *
  168. * @param string $search
  169. * @param int $limit
  170. * @param int $offset
  171. * @return \OC\User\User[]
  172. */
  173. public function searchUsers($search, $limit = null, $offset = null) {
  174. $users = array();
  175. foreach ($this->backends as $backend) {
  176. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  177. $users += $this->getVerifiedUsers($userIds);
  178. if (!is_null($limit) and $limit <= 0) {
  179. return array_values($users);
  180. }
  181. }
  182. return array_values($users);
  183. }
  184. /**
  185. * returns the number of users matching the search string
  186. *
  187. * @param string $search
  188. * @return int|bool
  189. */
  190. public function count($search = '') {
  191. $users = false;
  192. foreach ($this->backends as $backend) {
  193. if($backend->implementsActions(\OC_Group_Backend::COUNT_USERS)) {
  194. if($users === false) {
  195. //we could directly add to a bool variable, but this would
  196. //be ugly
  197. $users = 0;
  198. }
  199. $users += $backend->countUsersInGroup($this->gid, $search);
  200. }
  201. }
  202. return $users;
  203. }
  204. /**
  205. * search for users in the group by displayname
  206. *
  207. * @param string $search
  208. * @param int $limit
  209. * @param int $offset
  210. * @return \OC\User\User[]
  211. */
  212. public function searchDisplayName($search, $limit = null, $offset = null) {
  213. $users = array();
  214. foreach ($this->backends as $backend) {
  215. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  216. $users = $this->getVerifiedUsers($userIds);
  217. if (!is_null($limit) and $limit <= 0) {
  218. return array_values($users);
  219. }
  220. }
  221. return array_values($users);
  222. }
  223. /**
  224. * delete the group
  225. *
  226. * @return bool
  227. */
  228. public function delete() {
  229. // Prevent users from deleting group admin
  230. if ($this->getGID() === 'admin') {
  231. return false;
  232. }
  233. $result = false;
  234. if ($this->emitter) {
  235. $this->emitter->emit('\OC\Group', 'preDelete', array($this));
  236. }
  237. foreach ($this->backends as $backend) {
  238. if ($backend->implementsActions(\OC_Group_Backend::DELETE_GROUP)) {
  239. $result = true;
  240. $backend->deleteGroup($this->gid);
  241. }
  242. }
  243. if ($result and $this->emitter) {
  244. $this->emitter->emit('\OC\Group', 'postDelete', array($this));
  245. }
  246. return $result;
  247. }
  248. /**
  249. * returns all the Users from an array that really exists
  250. * @param string[] $userIds an array containing user IDs
  251. * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
  252. */
  253. private function getVerifiedUsers($userIds) {
  254. if (!is_array($userIds)) {
  255. return array();
  256. }
  257. $users = array();
  258. foreach ($userIds as $userId) {
  259. $user = $this->userManager->get($userId);
  260. if (!is_null($user)) {
  261. $users[$userId] = $user;
  262. }
  263. }
  264. return $users;
  265. }
  266. }