Group.php 11 KB

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