1
0

Group.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. * @author Vincent Petry <pvince81@owncloud.com>
  13. * @author John Molakvoæ <skjnldsv@protonmail.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Group;
  31. use OCP\Group\Backend\IGetDisplayNameBackend;
  32. use OCP\Group\Backend\IHideFromCollaborationBackend;
  33. use OC\Hooks\PublicEmitter;
  34. use OCP\GroupInterface;
  35. use OCP\IGroup;
  36. use OCP\IUser;
  37. use OCP\Group\Backend\ICountDisabledInGroup;
  38. use OCP\IUserManager;
  39. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  40. use Symfony\Component\EventDispatcher\GenericEvent;
  41. class Group implements IGroup {
  42. /** @var null|string */
  43. protected $displayName;
  44. /** @var string */
  45. private $gid;
  46. /** @var \OC\User\User[] */
  47. private $users = array();
  48. /** @var bool */
  49. private $usersLoaded;
  50. /** @var Backend[] */
  51. private $backends;
  52. /** @var EventDispatcherInterface */
  53. private $dispatcher;
  54. /** @var \OC\User\Manager|IUserManager */
  55. private $userManager;
  56. /** @var PublicEmitter */
  57. private $emitter;
  58. /**
  59. * @param string $gid
  60. * @param Backend[] $backends
  61. * @param EventDispatcherInterface $dispatcher
  62. * @param IUserManager $userManager
  63. * @param PublicEmitter $emitter
  64. * @param string $displayName
  65. */
  66. public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) {
  67. $this->gid = $gid;
  68. $this->backends = $backends;
  69. $this->dispatcher = $dispatcher;
  70. $this->userManager = $userManager;
  71. $this->emitter = $emitter;
  72. $this->displayName = $displayName;
  73. }
  74. public function getGID() {
  75. return $this->gid;
  76. }
  77. public function getDisplayName() {
  78. if (is_null($this->displayName)) {
  79. foreach ($this->backends as $backend) {
  80. if ($backend instanceof IGetDisplayNameBackend) {
  81. $displayName = $backend->getDisplayName($this->gid);
  82. if (trim($displayName) !== '') {
  83. $this->displayName = $displayName;
  84. return $this->displayName;
  85. }
  86. }
  87. }
  88. return $this->gid;
  89. }
  90. return $this->displayName;
  91. }
  92. /**
  93. * get all users in the group
  94. *
  95. * @return \OC\User\User[]
  96. */
  97. public function getUsers() {
  98. if ($this->usersLoaded) {
  99. return $this->users;
  100. }
  101. $userIds = array();
  102. foreach ($this->backends as $backend) {
  103. $diff = array_diff(
  104. $backend->usersInGroup($this->gid),
  105. $userIds
  106. );
  107. if ($diff) {
  108. $userIds = array_merge($userIds, $diff);
  109. }
  110. }
  111. $this->users = $this->getVerifiedUsers($userIds);
  112. $this->usersLoaded = true;
  113. return $this->users;
  114. }
  115. /**
  116. * check if a user is in the group
  117. *
  118. * @param IUser $user
  119. * @return bool
  120. */
  121. public function inGroup(IUser $user) {
  122. if (isset($this->users[$user->getUID()])) {
  123. return true;
  124. }
  125. foreach ($this->backends as $backend) {
  126. if ($backend->inGroup($user->getUID(), $this->gid)) {
  127. $this->users[$user->getUID()] = $user;
  128. return true;
  129. }
  130. }
  131. return false;
  132. }
  133. /**
  134. * add a user to the group
  135. *
  136. * @param IUser $user
  137. */
  138. public function addUser(IUser $user) {
  139. if ($this->inGroup($user)) {
  140. return;
  141. }
  142. $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [
  143. 'user' => $user,
  144. ]));
  145. if ($this->emitter) {
  146. $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
  147. }
  148. foreach ($this->backends as $backend) {
  149. if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
  150. $backend->addToGroup($user->getUID(), $this->gid);
  151. if ($this->users) {
  152. $this->users[$user->getUID()] = $user;
  153. }
  154. $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [
  155. 'user' => $user,
  156. ]));
  157. if ($this->emitter) {
  158. $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
  159. }
  160. return;
  161. }
  162. }
  163. }
  164. /**
  165. * remove a user from the group
  166. *
  167. * @param \OC\User\User $user
  168. */
  169. public function removeUser($user) {
  170. $result = false;
  171. $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [
  172. 'user' => $user,
  173. ]));
  174. if ($this->emitter) {
  175. $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
  176. }
  177. foreach ($this->backends as $backend) {
  178. if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
  179. $backend->removeFromGroup($user->getUID(), $this->gid);
  180. $result = true;
  181. }
  182. }
  183. if ($result) {
  184. $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [
  185. 'user' => $user,
  186. ]));
  187. if ($this->emitter) {
  188. $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
  189. }
  190. if ($this->users) {
  191. foreach ($this->users as $index => $groupUser) {
  192. if ($groupUser->getUID() === $user->getUID()) {
  193. unset($this->users[$index]);
  194. return;
  195. }
  196. }
  197. }
  198. }
  199. }
  200. /**
  201. * search for users in the group by userid
  202. *
  203. * @param string $search
  204. * @param int $limit
  205. * @param int $offset
  206. * @return \OC\User\User[]
  207. */
  208. public function searchUsers($search, $limit = null, $offset = null) {
  209. $users = array();
  210. foreach ($this->backends as $backend) {
  211. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  212. $users += $this->getVerifiedUsers($userIds);
  213. if (!is_null($limit) and $limit <= 0) {
  214. return $users;
  215. }
  216. }
  217. return $users;
  218. }
  219. /**
  220. * returns the number of users matching the search string
  221. *
  222. * @param string $search
  223. * @return int|bool
  224. */
  225. public function count($search = '') {
  226. $users = false;
  227. foreach ($this->backends as $backend) {
  228. if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
  229. if($users === false) {
  230. //we could directly add to a bool variable, but this would
  231. //be ugly
  232. $users = 0;
  233. }
  234. $users += $backend->countUsersInGroup($this->gid, $search);
  235. }
  236. }
  237. return $users;
  238. }
  239. /**
  240. * returns the number of disabled users
  241. *
  242. * @return int|bool
  243. */
  244. public function countDisabled() {
  245. $users = false;
  246. foreach ($this->backends as $backend) {
  247. if($backend instanceOf ICountDisabledInGroup) {
  248. if($users === false) {
  249. //we could directly add to a bool variable, but this would
  250. //be ugly
  251. $users = 0;
  252. }
  253. $users += $backend->countDisabledInGroup($this->gid);
  254. }
  255. }
  256. return $users;
  257. }
  258. /**
  259. * search for users in the group by displayname
  260. *
  261. * @param string $search
  262. * @param int $limit
  263. * @param int $offset
  264. * @return \OC\User\User[]
  265. */
  266. public function searchDisplayName($search, $limit = null, $offset = null) {
  267. $users = array();
  268. foreach ($this->backends as $backend) {
  269. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  270. $users = $this->getVerifiedUsers($userIds);
  271. if (!is_null($limit) and $limit <= 0) {
  272. return array_values($users);
  273. }
  274. }
  275. return array_values($users);
  276. }
  277. /**
  278. * delete the group
  279. *
  280. * @return bool
  281. */
  282. public function delete() {
  283. // Prevent users from deleting group admin
  284. if ($this->getGID() === 'admin') {
  285. return false;
  286. }
  287. $result = false;
  288. $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this));
  289. if ($this->emitter) {
  290. $this->emitter->emit('\OC\Group', 'preDelete', array($this));
  291. }
  292. foreach ($this->backends as $backend) {
  293. if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
  294. $result = true;
  295. $backend->deleteGroup($this->gid);
  296. }
  297. }
  298. if ($result) {
  299. $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this));
  300. if ($this->emitter) {
  301. $this->emitter->emit('\OC\Group', 'postDelete', array($this));
  302. }
  303. }
  304. return $result;
  305. }
  306. /**
  307. * returns all the Users from an array that really exists
  308. * @param string[] $userIds an array containing user IDs
  309. * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
  310. */
  311. private function getVerifiedUsers($userIds) {
  312. if (!is_array($userIds)) {
  313. return array();
  314. }
  315. $users = array();
  316. foreach ($userIds as $userId) {
  317. $user = $this->userManager->get($userId);
  318. if (!is_null($user)) {
  319. $users[$userId] = $user;
  320. }
  321. }
  322. return $users;
  323. }
  324. /**
  325. * @return bool
  326. * @since 14.0.0
  327. */
  328. public function canRemoveUser() {
  329. foreach ($this->backends as $backend) {
  330. if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) {
  331. return true;
  332. }
  333. }
  334. return false;
  335. }
  336. /**
  337. * @return bool
  338. * @since 14.0.0
  339. */
  340. public function canAddUser() {
  341. foreach ($this->backends as $backend) {
  342. if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) {
  343. return true;
  344. }
  345. }
  346. return false;
  347. }
  348. /**
  349. * @return bool
  350. * @since 16.0.0
  351. */
  352. public function hideFromCollaboration(): bool {
  353. return array_reduce($this->backends, function(bool $hide, GroupInterface $backend) {
  354. return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid));
  355. }, false);
  356. }
  357. }