Group.php 11 KB

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