Group.php 11 KB

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