Group.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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\Group\Events\GroupChangedEvent;
  41. use OCP\GroupInterface;
  42. use OCP\IGroup;
  43. use OCP\IUser;
  44. use OCP\IUserManager;
  45. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  46. use Symfony\Component\EventDispatcher\GenericEvent;
  47. class Group implements IGroup {
  48. /** @var null|string */
  49. protected $displayName;
  50. /** @var string */
  51. private $gid;
  52. /** @var \OC\User\User[] */
  53. private $users = [];
  54. /** @var bool */
  55. private $usersLoaded;
  56. /** @var Backend[] */
  57. private $backends;
  58. /** @var EventDispatcherInterface */
  59. private $dispatcher;
  60. /** @var \OC\User\Manager|IUserManager */
  61. private $userManager;
  62. /** @var PublicEmitter */
  63. private $emitter;
  64. /**
  65. * @param string $gid
  66. * @param Backend[] $backends
  67. * @param EventDispatcherInterface $dispatcher
  68. * @param IUserManager $userManager
  69. * @param PublicEmitter $emitter
  70. * @param string $displayName
  71. */
  72. public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) {
  73. $this->gid = $gid;
  74. $this->backends = $backends;
  75. $this->dispatcher = $dispatcher;
  76. $this->userManager = $userManager;
  77. $this->emitter = $emitter;
  78. $this->displayName = $displayName;
  79. }
  80. public function getGID() {
  81. return $this->gid;
  82. }
  83. public function getDisplayName() {
  84. if (is_null($this->displayName)) {
  85. foreach ($this->backends as $backend) {
  86. if ($backend instanceof IGetDisplayNameBackend) {
  87. $displayName = $backend->getDisplayName($this->gid);
  88. if (trim($displayName) !== '') {
  89. $this->displayName = $displayName;
  90. return $this->displayName;
  91. }
  92. }
  93. }
  94. return $this->gid;
  95. }
  96. return $this->displayName;
  97. }
  98. public function setDisplayName(string $displayName): bool {
  99. $displayName = trim($displayName);
  100. if ($displayName !== '') {
  101. foreach ($this->backends as $backend) {
  102. if (($backend instanceof ISetDisplayNameBackend)
  103. && $backend->setDisplayName($this->gid, $displayName)) {
  104. $this->displayName = $displayName;
  105. $this->dispatcher->dispatch(new GroupChangedEvent($this, 'displayName', $displayName, ''));
  106. return true;
  107. }
  108. }
  109. }
  110. return false;
  111. }
  112. /**
  113. * get all users in the group
  114. *
  115. * @return \OC\User\User[]
  116. */
  117. public function getUsers() {
  118. if ($this->usersLoaded) {
  119. return $this->users;
  120. }
  121. $userIds = [];
  122. foreach ($this->backends as $backend) {
  123. $diff = array_diff(
  124. $backend->usersInGroup($this->gid),
  125. $userIds
  126. );
  127. if ($diff) {
  128. $userIds = array_merge($userIds, $diff);
  129. }
  130. }
  131. $this->users = $this->getVerifiedUsers($userIds);
  132. $this->usersLoaded = true;
  133. return $this->users;
  134. }
  135. /**
  136. * check if a user is in the group
  137. *
  138. * @param IUser $user
  139. * @return bool
  140. */
  141. public function inGroup(IUser $user) {
  142. if (isset($this->users[$user->getUID()])) {
  143. return true;
  144. }
  145. foreach ($this->backends as $backend) {
  146. if ($backend->inGroup($user->getUID(), $this->gid)) {
  147. $this->users[$user->getUID()] = $user;
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. /**
  154. * add a user to the group
  155. *
  156. * @param IUser $user
  157. */
  158. public function addUser(IUser $user) {
  159. if ($this->inGroup($user)) {
  160. return;
  161. }
  162. $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [
  163. 'user' => $user,
  164. ]));
  165. if ($this->emitter) {
  166. $this->emitter->emit('\OC\Group', 'preAddUser', [$this, $user]);
  167. }
  168. foreach ($this->backends as $backend) {
  169. if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) {
  170. $backend->addToGroup($user->getUID(), $this->gid);
  171. if ($this->users) {
  172. $this->users[$user->getUID()] = $user;
  173. }
  174. $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [
  175. 'user' => $user,
  176. ]));
  177. if ($this->emitter) {
  178. $this->emitter->emit('\OC\Group', 'postAddUser', [$this, $user]);
  179. }
  180. return;
  181. }
  182. }
  183. }
  184. /**
  185. * remove a user from the group
  186. *
  187. * @param \OC\User\User $user
  188. */
  189. public function removeUser($user) {
  190. $result = false;
  191. $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [
  192. 'user' => $user,
  193. ]));
  194. if ($this->emitter) {
  195. $this->emitter->emit('\OC\Group', 'preRemoveUser', [$this, $user]);
  196. }
  197. foreach ($this->backends as $backend) {
  198. if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
  199. $backend->removeFromGroup($user->getUID(), $this->gid);
  200. $result = true;
  201. }
  202. }
  203. if ($result) {
  204. $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [
  205. 'user' => $user,
  206. ]));
  207. if ($this->emitter) {
  208. $this->emitter->emit('\OC\Group', 'postRemoveUser', [$this, $user]);
  209. }
  210. if ($this->users) {
  211. foreach ($this->users as $index => $groupUser) {
  212. if ($groupUser->getUID() === $user->getUID()) {
  213. unset($this->users[$index]);
  214. return;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * search for users in the group by userid
  222. *
  223. * @param string $search
  224. * @param int $limit
  225. * @param int $offset
  226. * @return \OC\User\User[]
  227. */
  228. public function searchUsers($search, $limit = null, $offset = null) {
  229. $users = [];
  230. foreach ($this->backends as $backend) {
  231. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  232. $users += $this->getVerifiedUsers($userIds);
  233. if (!is_null($limit) and $limit <= 0) {
  234. return $users;
  235. }
  236. }
  237. return $users;
  238. }
  239. /**
  240. * returns the number of users matching the search string
  241. *
  242. * @param string $search
  243. * @return int|bool
  244. */
  245. public function count($search = '') {
  246. $users = false;
  247. foreach ($this->backends as $backend) {
  248. if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
  249. if ($users === false) {
  250. //we could directly add to a bool variable, but this would
  251. //be ugly
  252. $users = 0;
  253. }
  254. $users += $backend->countUsersInGroup($this->gid, $search);
  255. }
  256. }
  257. return $users;
  258. }
  259. /**
  260. * returns the number of disabled users
  261. *
  262. * @return int|bool
  263. */
  264. public function countDisabled() {
  265. $users = false;
  266. foreach ($this->backends as $backend) {
  267. if ($backend instanceof ICountDisabledInGroup) {
  268. if ($users === false) {
  269. //we could directly add to a bool variable, but this would
  270. //be ugly
  271. $users = 0;
  272. }
  273. $users += $backend->countDisabledInGroup($this->gid);
  274. }
  275. }
  276. return $users;
  277. }
  278. /**
  279. * search for users in the group by displayname
  280. *
  281. * @param string $search
  282. * @param int $limit
  283. * @param int $offset
  284. * @return \OC\User\User[]
  285. */
  286. public function searchDisplayName($search, $limit = null, $offset = null) {
  287. $users = [];
  288. foreach ($this->backends as $backend) {
  289. $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset);
  290. $users = $this->getVerifiedUsers($userIds);
  291. if (!is_null($limit) and $limit <= 0) {
  292. return array_values($users);
  293. }
  294. }
  295. return array_values($users);
  296. }
  297. /**
  298. * Get the names of the backend classes the group is connected to
  299. *
  300. * @return string[]
  301. */
  302. public function getBackendNames() {
  303. $backends = [];
  304. foreach ($this->backends as $backend) {
  305. if ($backend instanceof INamedBackend) {
  306. $backends[] = $backend->getBackendName();
  307. } else {
  308. $backends[] = get_class($backend);
  309. }
  310. }
  311. return $backends;
  312. }
  313. /**
  314. * delete the group
  315. *
  316. * @return bool
  317. */
  318. public function delete() {
  319. // Prevent users from deleting group admin
  320. if ($this->getGID() === 'admin') {
  321. return false;
  322. }
  323. $result = false;
  324. $this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this));
  325. if ($this->emitter) {
  326. $this->emitter->emit('\OC\Group', 'preDelete', [$this]);
  327. }
  328. foreach ($this->backends as $backend) {
  329. if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
  330. $result = $result || $backend->deleteGroup($this->gid);
  331. }
  332. }
  333. if ($result) {
  334. $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this));
  335. if ($this->emitter) {
  336. $this->emitter->emit('\OC\Group', 'postDelete', [$this]);
  337. }
  338. }
  339. return $result;
  340. }
  341. /**
  342. * returns all the Users from an array that really exists
  343. * @param string[] $userIds an array containing user IDs
  344. * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
  345. */
  346. private function getVerifiedUsers($userIds) {
  347. if (!is_array($userIds)) {
  348. return [];
  349. }
  350. $users = [];
  351. foreach ($userIds as $userId) {
  352. $user = $this->userManager->get($userId);
  353. if (!is_null($user)) {
  354. $users[$userId] = $user;
  355. }
  356. }
  357. return $users;
  358. }
  359. /**
  360. * @return bool
  361. * @since 14.0.0
  362. */
  363. public function canRemoveUser() {
  364. foreach ($this->backends as $backend) {
  365. if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) {
  366. return true;
  367. }
  368. }
  369. return false;
  370. }
  371. /**
  372. * @return bool
  373. * @since 14.0.0
  374. */
  375. public function canAddUser() {
  376. foreach ($this->backends as $backend) {
  377. if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) {
  378. return true;
  379. }
  380. }
  381. return false;
  382. }
  383. /**
  384. * @return bool
  385. * @since 16.0.0
  386. */
  387. public function hideFromCollaboration(): bool {
  388. return array_reduce($this->backends, function (bool $hide, GroupInterface $backend) {
  389. return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid));
  390. }, false);
  391. }
  392. }