1
0

Group.php 9.7 KB

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