You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Group.php 6.8KB

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