1
0

Manager.php 11 KB

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