Group.php 11 KB

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