Manager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 Bernhard Posselt <dev@bernhard-posselt.com>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Knut Ahlers <knut@ahlers.me>
  13. * @author Lukas Reschke <lukas@statuscode.ch>
  14. * @author macjohnny <estebanmarin@gmx.ch>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Robin McCorkell <robin@mccorkell.me.uk>
  18. * @author Roeland Jago Douma <roeland@famdouma.nl>
  19. * @author Roman Kreisel <mail@romankreisel.de>
  20. * @author Thomas Müller <thomas.mueller@tmit.eu>
  21. * @author Vincent Petry <vincent@nextcloud.com>
  22. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  23. * @author voxsim "Simon Vocella"
  24. *
  25. * @license AGPL-3.0
  26. *
  27. * This code is free software: you can redistribute it and/or modify
  28. * it under the terms of the GNU Affero General Public License, version 3,
  29. * as published by the Free Software Foundation.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU Affero General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Affero General Public License, version 3,
  37. * along with this program. If not, see <http://www.gnu.org/licenses/>
  38. *
  39. */
  40. namespace OC\Group;
  41. use OC\Hooks\PublicEmitter;
  42. use OCP\EventDispatcher\IEventDispatcher;
  43. use OCP\GroupInterface;
  44. use OCP\IGroup;
  45. use OCP\IGroupManager;
  46. use OCP\IUser;
  47. use Psr\Log\LoggerInterface;
  48. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  49. /**
  50. * Class Manager
  51. *
  52. * Hooks available in scope \OC\Group:
  53. * - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
  54. * - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
  55. * - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
  56. * - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
  57. * - preDelete(\OC\Group\Group $group)
  58. * - postDelete(\OC\Group\Group $group)
  59. * - preCreate(string $groupId)
  60. * - postCreate(\OC\Group\Group $group)
  61. *
  62. * @package OC\Group
  63. */
  64. class Manager extends PublicEmitter implements IGroupManager {
  65. /** @var GroupInterface[] */
  66. private $backends = [];
  67. /** @var \OC\User\Manager */
  68. private $userManager;
  69. /** @var EventDispatcherInterface */
  70. private $dispatcher;
  71. private LoggerInterface $logger;
  72. /** @var \OC\Group\Group[] */
  73. private $cachedGroups = [];
  74. /** @var (string[])[] */
  75. private $cachedUserGroups = [];
  76. /** @var \OC\SubAdmin */
  77. private $subAdmin = null;
  78. public function __construct(\OC\User\Manager $userManager,
  79. EventDispatcherInterface $dispatcher,
  80. LoggerInterface $logger) {
  81. $this->userManager = $userManager;
  82. $this->dispatcher = $dispatcher;
  83. $this->logger = $logger;
  84. $cachedGroups = &$this->cachedGroups;
  85. $cachedUserGroups = &$this->cachedUserGroups;
  86. $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
  87. /**
  88. * @var \OC\Group\Group $group
  89. */
  90. unset($cachedGroups[$group->getGID()]);
  91. $cachedUserGroups = [];
  92. });
  93. $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
  94. /**
  95. * @var \OC\Group\Group $group
  96. */
  97. $cachedUserGroups = [];
  98. });
  99. $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
  100. /**
  101. * @var \OC\Group\Group $group
  102. */
  103. $cachedUserGroups = [];
  104. });
  105. }
  106. /**
  107. * Checks whether a given backend is used
  108. *
  109. * @param string $backendClass Full classname including complete namespace
  110. * @return bool
  111. */
  112. public function isBackendUsed($backendClass) {
  113. $backendClass = strtolower(ltrim($backendClass, '\\'));
  114. foreach ($this->backends as $backend) {
  115. if (strtolower(get_class($backend)) === $backendClass) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. /**
  122. * @param \OCP\GroupInterface $backend
  123. */
  124. public function addBackend($backend) {
  125. $this->backends[] = $backend;
  126. $this->clearCaches();
  127. }
  128. public function clearBackends() {
  129. $this->backends = [];
  130. $this->clearCaches();
  131. }
  132. /**
  133. * Get the active backends
  134. *
  135. * @return \OCP\GroupInterface[]
  136. */
  137. public function getBackends() {
  138. return $this->backends;
  139. }
  140. protected function clearCaches() {
  141. $this->cachedGroups = [];
  142. $this->cachedUserGroups = [];
  143. }
  144. /**
  145. * @param string $gid
  146. * @return IGroup|null
  147. */
  148. public function get($gid) {
  149. if (isset($this->cachedGroups[$gid])) {
  150. return $this->cachedGroups[$gid];
  151. }
  152. return $this->getGroupObject($gid);
  153. }
  154. /**
  155. * @param string $gid
  156. * @param string $displayName
  157. * @return \OCP\IGroup|null
  158. */
  159. protected function getGroupObject($gid, $displayName = null) {
  160. $backends = [];
  161. foreach ($this->backends as $backend) {
  162. if ($backend->implementsActions(Backend::GROUP_DETAILS)) {
  163. $groupData = $backend->getGroupDetails($gid);
  164. if (is_array($groupData) && !empty($groupData)) {
  165. // take the display name from the first backend that has a non-null one
  166. if (is_null($displayName) && isset($groupData['displayName'])) {
  167. $displayName = $groupData['displayName'];
  168. }
  169. $backends[] = $backend;
  170. }
  171. } elseif ($backend->groupExists($gid)) {
  172. $backends[] = $backend;
  173. }
  174. }
  175. if (count($backends) === 0) {
  176. return null;
  177. }
  178. $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName);
  179. return $this->cachedGroups[$gid];
  180. }
  181. /**
  182. * @param string $gid
  183. * @return bool
  184. */
  185. public function groupExists($gid) {
  186. return $this->get($gid) instanceof IGroup;
  187. }
  188. /**
  189. * @param string $gid
  190. * @return IGroup|null
  191. */
  192. public function createGroup($gid) {
  193. if ($gid === '' || $gid === null) {
  194. return null;
  195. } elseif ($group = $this->get($gid)) {
  196. return $group;
  197. } else {
  198. $this->emit('\OC\Group', 'preCreate', [$gid]);
  199. foreach ($this->backends as $backend) {
  200. if ($backend->implementsActions(Backend::CREATE_GROUP)) {
  201. if ($backend->createGroup($gid)) {
  202. $group = $this->getGroupObject($gid);
  203. $this->emit('\OC\Group', 'postCreate', [$group]);
  204. return $group;
  205. }
  206. }
  207. }
  208. return null;
  209. }
  210. }
  211. /**
  212. * @param string $search
  213. * @param int $limit
  214. * @param int $offset
  215. * @return \OC\Group\Group[]
  216. */
  217. public function search($search, $limit = null, $offset = null) {
  218. $groups = [];
  219. foreach ($this->backends as $backend) {
  220. $groupIds = $backend->getGroups($search, $limit, $offset);
  221. foreach ($groupIds as $groupId) {
  222. $aGroup = $this->get($groupId);
  223. if ($aGroup instanceof IGroup) {
  224. $groups[$groupId] = $aGroup;
  225. } else {
  226. $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']);
  227. }
  228. }
  229. if (!is_null($limit) and $limit <= 0) {
  230. return array_values($groups);
  231. }
  232. }
  233. return array_values($groups);
  234. }
  235. /**
  236. * @param IUser|null $user
  237. * @return \OC\Group\Group[]
  238. */
  239. public function getUserGroups(IUser $user = null) {
  240. if (!$user instanceof IUser) {
  241. return [];
  242. }
  243. return $this->getUserIdGroups($user->getUID());
  244. }
  245. /**
  246. * @param string $uid the user id
  247. * @return \OC\Group\Group[]
  248. */
  249. public function getUserIdGroups(string $uid): array {
  250. $groups = [];
  251. foreach ($this->getUserIdGroupIds($uid) as $groupId) {
  252. $aGroup = $this->get($groupId);
  253. if ($aGroup instanceof IGroup) {
  254. $groups[$groupId] = $aGroup;
  255. } else {
  256. $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']);
  257. }
  258. }
  259. return $groups;
  260. }
  261. /**
  262. * Checks if a userId is in the admin group
  263. *
  264. * @param string $userId
  265. * @return bool if admin
  266. */
  267. public function isAdmin($userId) {
  268. foreach ($this->backends as $backend) {
  269. if ($backend->implementsActions(Backend::IS_ADMIN) && $backend->isAdmin($userId)) {
  270. return true;
  271. }
  272. }
  273. return $this->isInGroup($userId, 'admin');
  274. }
  275. /**
  276. * Checks if a userId is in a group
  277. *
  278. * @param string $userId
  279. * @param string $group
  280. * @return bool if in group
  281. */
  282. public function isInGroup($userId, $group) {
  283. return array_search($group, $this->getUserIdGroupIds($userId)) !== false;
  284. }
  285. /**
  286. * get a list of group ids for a user
  287. *
  288. * @param IUser $user
  289. * @return string[] with group ids
  290. */
  291. public function getUserGroupIds(IUser $user): array {
  292. return $this->getUserIdGroupIds($user->getUID());
  293. }
  294. /**
  295. * @param string $uid the user id
  296. * @return string[]
  297. */
  298. private function getUserIdGroupIds(string $uid): array {
  299. if (!isset($this->cachedUserGroups[$uid])) {
  300. $groups = [];
  301. foreach ($this->backends as $backend) {
  302. if ($groupIds = $backend->getUserGroups($uid)) {
  303. $groups = array_merge($groups, $groupIds);
  304. }
  305. }
  306. $this->cachedUserGroups[$uid] = $groups;
  307. }
  308. return $this->cachedUserGroups[$uid];
  309. }
  310. /**
  311. * get an array of groupid and displayName for a user
  312. *
  313. * @param IUser $user
  314. * @return array ['displayName' => displayname]
  315. */
  316. public function getUserGroupNames(IUser $user) {
  317. return array_map(function ($group) {
  318. return ['displayName' => $group->getDisplayName()];
  319. }, $this->getUserGroups($user));
  320. }
  321. /**
  322. * get a list of all display names in a group
  323. *
  324. * @param string $gid
  325. * @param string $search
  326. * @param int $limit
  327. * @param int $offset
  328. * @return array an array of display names (value) and user ids (key)
  329. */
  330. public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  331. $group = $this->get($gid);
  332. if (is_null($group)) {
  333. return [];
  334. }
  335. $search = trim($search);
  336. $groupUsers = [];
  337. if (!empty($search)) {
  338. // only user backends have the capability to do a complex search for users
  339. $searchOffset = 0;
  340. $searchLimit = $limit * 100;
  341. if ($limit === -1) {
  342. $searchLimit = 500;
  343. }
  344. do {
  345. $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset);
  346. foreach ($filteredUsers as $filteredUser) {
  347. if ($group->inGroup($filteredUser)) {
  348. $groupUsers[] = $filteredUser;
  349. }
  350. }
  351. $searchOffset += $searchLimit;
  352. } while (count($groupUsers) < $searchLimit + $offset && count($filteredUsers) >= $searchLimit);
  353. if ($limit === -1) {
  354. $groupUsers = array_slice($groupUsers, $offset);
  355. } else {
  356. $groupUsers = array_slice($groupUsers, $offset, $limit);
  357. }
  358. } else {
  359. $groupUsers = $group->searchUsers('', $limit, $offset);
  360. }
  361. $matchingUsers = [];
  362. foreach ($groupUsers as $groupUser) {
  363. $matchingUsers[(string) $groupUser->getUID()] = $groupUser->getDisplayName();
  364. }
  365. return $matchingUsers;
  366. }
  367. /**
  368. * @return \OC\SubAdmin
  369. */
  370. public function getSubAdmin() {
  371. if (!$this->subAdmin) {
  372. $this->subAdmin = new \OC\SubAdmin(
  373. $this->userManager,
  374. $this,
  375. \OC::$server->getDatabaseConnection(),
  376. \OC::$server->get(IEventDispatcher::class)
  377. );
  378. }
  379. return $this->subAdmin;
  380. }
  381. }