Group.php 11 KB

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