Group.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. /** @var null|string */
  32. protected $displayName;
  33. /**
  34. * @var string $id
  35. */
  36. private $gid;
  37. /**
  38. * @var \OC\User\User[] $users
  39. */
  40. private $users = array();
  41. /**
  42. * @var bool $usersLoaded
  43. */
  44. private $usersLoaded;
  45. /**
  46. * @var \OC\Group\Backend[]|\OC\Group\Database[] $backend
  47. */
  48. private $backends;
  49. /**
  50. * @var \OC\Hooks\PublicEmitter $emitter
  51. */
  52. private $emitter;
  53. /**
  54. * @var \OC\User\Manager $userManager
  55. */
  56. private $userManager;
  57. /**
  58. * @param string $gid
  59. * @param \OC\Group\Backend[] $backends
  60. * @param \OC\User\Manager $userManager
  61. * @param \OC\Hooks\PublicEmitter $emitter
  62. * @param string $displayName
  63. */
  64. public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
  65. $this->gid = $gid;
  66. $this->backends = $backends;
  67. $this->userManager = $userManager;
  68. $this->emitter = $emitter;
  69. $this->displayName = $displayName;
  70. }
  71. public function getGID() {
  72. return $this->gid;
  73. }
  74. public function getDisplayName() {
  75. if (is_null($this->displayName)) {
  76. return $this->gid;
  77. }
  78. return $this->displayName;
  79. }
  80. /**
  81. * get all users in the group
  82. *
  83. * @return \OC\User\User[]
  84. */
  85. public function getUsers() {
  86. if ($this->usersLoaded) {
  87. return $this->users;
  88. }
  89. $userIds = array();
  90. foreach ($this->backends as $backend) {
  91. $diff = array_diff(
  92. $backend->usersInGroup($this->gid),
  93. $userIds
  94. );
  95. if ($diff) {
  96. $userIds = array_merge($userIds, $diff);
  97. }
  98. }
  99. $this->users = $this->getVerifiedUsers($userIds);
  100. $this->usersLoaded = true;
  101. return $this->users;
  102. }
  103. /**
  104. * check if a user is in the group
  105. *
  106. * @param \OC\User\User $user
  107. * @return bool
  108. */
  109. public function inGroup($user) {
  110. if (isset($this->users[$user->getUID()])) {
  111. return true;
  112. }
  113. foreach ($this->backends as $backend) {
  114. if ($backend->inGroup($user->getUID(), $this->gid)) {
  115. $this->users[$user->getUID()] = $user;
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. /**
  122. * add a user to the group
  123. *
  124. * @param \OC\User\User $user
  125. */
  126. public function addUser($user) {
  127. if ($this->inGroup($user)) {
  128. return;
  129. }
  130. if ($this->emitter) {
  131. $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
  132. }
  133. foreach ($this->backends as $backend) {
  134. if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
  135. $backend->addToGroup($user->getUID(), $this->gid);
  136. if ($this->users) {
  137. $this->users[$user->getUID()] = $user;
  138. }
  139. if ($this->emitter) {
  140. $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
  141. }
  142. return;
  143. }
  144. }
  145. }
  146. /**
  147. * remove a user from the group
  148. *
  149. * @param \OC\User\User $user
  150. */
  151. public function removeUser($user) {
  152. $result = false;
  153. if ($this->emitter) {
  154. $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
  155. }
  156. foreach ($this->backends as $backend) {
  157. if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
  158. $backend->removeFromGroup($user->getUID(), $this->gid);
  159. $result = true;
  160. }
  161. }
  162. if ($result) {
  163. if ($this->emitter) {
  164. $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
  165. }
  166. if ($this->users) {
  167. foreach ($this->users as $index => $groupUser) {
  168. if ($groupUser->getUID() === $user->getUID()) {
  169. unset($this->users[$index]);
  170. return;
  171. }
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * search for users in the group by userid
  178. *
  179. * @param string $search
  180. * @param int $limit
  181. * @param int $offset
  182. * @return \OC\User\User[]
  183. */
  184. public function searchUsers($search, $limit = null, $offset = null) {
  185. $users = array();
  186. foreach ($this->backends as $backend) {
  187. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  188. $users += $this->getVerifiedUsers($userIds);
  189. if (!is_null($limit) and $limit <= 0) {
  190. return array_values($users);
  191. }
  192. }
  193. return array_values($users);
  194. }
  195. /**
  196. * returns the number of users matching the search string
  197. *
  198. * @param string $search
  199. * @return int|bool
  200. */
  201. public function count($search = '') {
  202. $users = false;
  203. foreach ($this->backends as $backend) {
  204. if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
  205. if($users === false) {
  206. //we could directly add to a bool variable, but this would
  207. //be ugly
  208. $users = 0;
  209. }
  210. $users += $backend->countUsersInGroup($this->gid, $search);
  211. }
  212. }
  213. return $users;
  214. }
  215. /**
  216. * search for users in the group by displayname
  217. *
  218. * @param string $search
  219. * @param int $limit
  220. * @param int $offset
  221. * @return \OC\User\User[]
  222. */
  223. public function searchDisplayName($search, $limit = null, $offset = null) {
  224. $users = array();
  225. foreach ($this->backends as $backend) {
  226. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  227. $users = $this->getVerifiedUsers($userIds);
  228. if (!is_null($limit) and $limit <= 0) {
  229. return array_values($users);
  230. }
  231. }
  232. return array_values($users);
  233. }
  234. /**
  235. * delete the group
  236. *
  237. * @return bool
  238. */
  239. public function delete() {
  240. // Prevent users from deleting group admin
  241. if ($this->getGID() === 'admin') {
  242. return false;
  243. }
  244. $result = false;
  245. if ($this->emitter) {
  246. $this->emitter->emit('\OC\Group', 'preDelete', array($this));
  247. }
  248. foreach ($this->backends as $backend) {
  249. if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
  250. $result = true;
  251. $backend->deleteGroup($this->gid);
  252. }
  253. }
  254. if ($result and $this->emitter) {
  255. $this->emitter->emit('\OC\Group', 'postDelete', array($this));
  256. }
  257. return $result;
  258. }
  259. /**
  260. * returns all the Users from an array that really exists
  261. * @param string[] $userIds an array containing user IDs
  262. * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
  263. */
  264. private function getVerifiedUsers($userIds) {
  265. if (!is_array($userIds)) {
  266. return array();
  267. }
  268. $users = array();
  269. foreach ($userIds as $userId) {
  270. $user = $this->userManager->get($userId);
  271. if (!is_null($user)) {
  272. $users[$userId] = $user;
  273. }
  274. }
  275. return $users;
  276. }
  277. }