Group.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. if ($this->users) {
  171. $this->users[$user->getUID()] = $user;
  172. }
  173. $this->dispatcher->dispatchTyped(new UserAddedEvent($this, $user));
  174. if ($this->emitter) {
  175. $this->emitter->emit('\OC\Group', 'postAddUser', [$this, $user]);
  176. }
  177. return;
  178. }
  179. }
  180. }
  181. /**
  182. * remove a user from the group
  183. */
  184. public function removeUser(IUser $user): void {
  185. $result = false;
  186. $this->dispatcher->dispatchTyped(new BeforeUserRemovedEvent($this, $user));
  187. if ($this->emitter) {
  188. $this->emitter->emit('\OC\Group', 'preRemoveUser', [$this, $user]);
  189. }
  190. foreach ($this->backends as $backend) {
  191. if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) {
  192. $backend->removeFromGroup($user->getUID(), $this->gid);
  193. $result = true;
  194. }
  195. }
  196. if ($result) {
  197. $this->dispatcher->dispatchTyped(new UserRemovedEvent($this, $user));
  198. if ($this->emitter) {
  199. $this->emitter->emit('\OC\Group', 'postRemoveUser', [$this, $user]);
  200. }
  201. if ($this->users) {
  202. foreach ($this->users as $index => $groupUser) {
  203. if ($groupUser->getUID() === $user->getUID()) {
  204. unset($this->users[$index]);
  205. return;
  206. }
  207. }
  208. }
  209. }
  210. }
  211. /**
  212. * Search for users in the group by userid or display name
  213. * @return IUser[]
  214. */
  215. public function searchUsers(string $search, ?int $limit = null, ?int $offset = null): array {
  216. $users = [];
  217. foreach ($this->backends as $backend) {
  218. if ($backend instanceof ISearchableGroupBackend) {
  219. $users += $backend->searchInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0);
  220. } else {
  221. $userIds = $backend->usersInGroup($this->gid, $search, $limit ?? -1, $offset ?? 0);
  222. $userManager = \OCP\Server::get(IUserManager::class);
  223. foreach ($userIds as $userId) {
  224. if (!isset($users[$userId])) {
  225. $users[$userId] = new LazyUser($userId, $userManager);
  226. }
  227. }
  228. }
  229. if (!is_null($limit) and $limit <= 0) {
  230. return $users;
  231. }
  232. }
  233. return $users;
  234. }
  235. /**
  236. * returns the number of users matching the search string
  237. *
  238. * @param string $search
  239. * @return int|bool
  240. */
  241. public function count($search = ''): int|bool {
  242. $users = false;
  243. foreach ($this->backends as $backend) {
  244. if ($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) {
  245. if ($users === false) {
  246. //we could directly add to a bool variable, but this would
  247. //be ugly
  248. $users = 0;
  249. }
  250. $users += $backend->countUsersInGroup($this->gid, $search);
  251. }
  252. }
  253. return $users;
  254. }
  255. /**
  256. * returns the number of disabled users
  257. *
  258. * @return int|bool
  259. */
  260. public function countDisabled(): int|bool {
  261. $users = false;
  262. foreach ($this->backends as $backend) {
  263. if ($backend instanceof ICountDisabledInGroup) {
  264. if ($users === false) {
  265. //we could directly add to a bool variable, but this would
  266. //be ugly
  267. $users = 0;
  268. }
  269. $users += $backend->countDisabledInGroup($this->gid);
  270. }
  271. }
  272. return $users;
  273. }
  274. /**
  275. * search for users in the group by displayname
  276. *
  277. * @param string $search
  278. * @param int $limit
  279. * @param int $offset
  280. * @return IUser[]
  281. * @deprecated 27.0.0 Use searchUsers instead (same implementation)
  282. */
  283. public function searchDisplayName(string $search, int $limit = null, int $offset = null): array {
  284. return $this->searchUsers($search, $limit, $offset);
  285. }
  286. /**
  287. * Get the names of the backend classes the group is connected to
  288. *
  289. * @return string[]
  290. */
  291. public function getBackendNames(): array {
  292. $backends = [];
  293. foreach ($this->backends as $backend) {
  294. if ($backend instanceof INamedBackend) {
  295. $backends[] = $backend->getBackendName();
  296. } else {
  297. $backends[] = get_class($backend);
  298. }
  299. }
  300. return $backends;
  301. }
  302. /**
  303. * Delete the group
  304. *
  305. * @return bool
  306. */
  307. public function delete(): bool {
  308. // Prevent users from deleting group admin
  309. if ($this->getGID() === 'admin') {
  310. return false;
  311. }
  312. $result = false;
  313. $this->dispatcher->dispatchTyped(new BeforeGroupDeletedEvent($this));
  314. if ($this->emitter) {
  315. $this->emitter->emit('\OC\Group', 'preDelete', [$this]);
  316. }
  317. foreach ($this->backends as $backend) {
  318. if ($backend->implementsActions(\OC\Group\Backend::DELETE_GROUP)) {
  319. $result = $result || $backend->deleteGroup($this->gid);
  320. }
  321. }
  322. if ($result) {
  323. $this->dispatcher->dispatchTyped(new GroupDeletedEvent($this));
  324. if ($this->emitter) {
  325. $this->emitter->emit('\OC\Group', 'postDelete', [$this]);
  326. }
  327. }
  328. return $result;
  329. }
  330. /**
  331. * returns all the Users from an array that really exists
  332. * @param string[] $userIds an array containing user IDs
  333. * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value
  334. */
  335. private function getVerifiedUsers(array $userIds): array {
  336. $users = [];
  337. foreach ($userIds as $userId) {
  338. $user = $this->userManager->get($userId);
  339. if (!is_null($user)) {
  340. $users[$userId] = $user;
  341. }
  342. }
  343. return $users;
  344. }
  345. /**
  346. * @return bool
  347. * @since 14.0.0
  348. */
  349. public function canRemoveUser(): bool {
  350. foreach ($this->backends as $backend) {
  351. if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) {
  352. return true;
  353. }
  354. }
  355. return false;
  356. }
  357. /**
  358. * @return bool
  359. * @since 14.0.0
  360. */
  361. public function canAddUser(): bool {
  362. foreach ($this->backends as $backend) {
  363. if ($backend->implementsActions(GroupInterface::ADD_TO_GROUP)) {
  364. return true;
  365. }
  366. }
  367. return false;
  368. }
  369. /**
  370. * @return bool
  371. * @since 16.0.0
  372. */
  373. public function hideFromCollaboration(): bool {
  374. return array_reduce($this->backends, function (bool $hide, GroupInterface $backend) {
  375. return $hide | ($backend instanceof IHideFromCollaborationBackend && $backend->hideGroup($this->gid));
  376. }, false);
  377. }
  378. }