1
0

Manager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. /**
  46. * Class Manager
  47. *
  48. * Hooks available in scope \OC\Group:
  49. * - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
  50. * - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
  51. * - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
  52. * - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
  53. * - preDelete(\OC\Group\Group $group)
  54. * - postDelete(\OC\Group\Group $group)
  55. * - preCreate(string $groupId)
  56. * - postCreate(\OC\Group\Group $group)
  57. *
  58. * @package OC\Group
  59. */
  60. class Manager extends PublicEmitter implements IGroupManager {
  61. /**
  62. * @var GroupInterface[] $backends
  63. */
  64. private $backends = array();
  65. /**
  66. * @var \OC\User\Manager $userManager
  67. */
  68. private $userManager;
  69. /**
  70. * @var \OC\Group\Group[]
  71. */
  72. private $cachedGroups = array();
  73. /**
  74. * @var \OC\Group\Group[]
  75. */
  76. private $cachedUserGroups = array();
  77. /** @var \OC\SubAdmin */
  78. private $subAdmin = null;
  79. /** @var ILogger */
  80. private $logger;
  81. /**
  82. * @param \OC\User\Manager $userManager
  83. * @param ILogger $logger
  84. */
  85. public function __construct(\OC\User\Manager $userManager, ILogger $logger) {
  86. $this->userManager = $userManager;
  87. $this->logger = $logger;
  88. $cachedGroups = & $this->cachedGroups;
  89. $cachedUserGroups = & $this->cachedUserGroups;
  90. $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
  91. /**
  92. * @var \OC\Group\Group $group
  93. */
  94. unset($cachedGroups[$group->getGID()]);
  95. $cachedUserGroups = array();
  96. });
  97. $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
  98. /**
  99. * @var \OC\Group\Group $group
  100. */
  101. $cachedUserGroups = array();
  102. });
  103. $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
  104. /**
  105. * @var \OC\Group\Group $group
  106. */
  107. $cachedUserGroups = array();
  108. });
  109. }
  110. /**
  111. * Checks whether a given backend is used
  112. *
  113. * @param string $backendClass Full classname including complete namespace
  114. * @return bool
  115. */
  116. public function isBackendUsed($backendClass) {
  117. $backendClass = strtolower(ltrim($backendClass, '\\'));
  118. foreach ($this->backends as $backend) {
  119. if (strtolower(get_class($backend)) === $backendClass) {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. /**
  126. * @param \OCP\GroupInterface $backend
  127. */
  128. public function addBackend($backend) {
  129. $this->backends[] = $backend;
  130. $this->clearCaches();
  131. }
  132. public function clearBackends() {
  133. $this->backends = array();
  134. $this->clearCaches();
  135. }
  136. /**
  137. * Get the active backends
  138. * @return \OCP\GroupInterface[]
  139. */
  140. public function getBackends() {
  141. return $this->backends;
  142. }
  143. protected function clearCaches() {
  144. $this->cachedGroups = array();
  145. $this->cachedUserGroups = array();
  146. }
  147. /**
  148. * @param string $gid
  149. * @return \OC\Group\Group
  150. */
  151. public function get($gid) {
  152. if (isset($this->cachedGroups[$gid])) {
  153. return $this->cachedGroups[$gid];
  154. }
  155. return $this->getGroupObject($gid);
  156. }
  157. /**
  158. * @param string $gid
  159. * @param string $displayName
  160. * @return \OCP\IGroup
  161. */
  162. protected function getGroupObject($gid, $displayName = null) {
  163. $backends = array();
  164. foreach ($this->backends as $backend) {
  165. if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) {
  166. $groupData = $backend->getGroupDetails($gid);
  167. if (is_array($groupData) && !empty($groupData)) {
  168. // take the display name from the first backend that has a non-null one
  169. if (is_null($displayName) && isset($groupData['displayName'])) {
  170. $displayName = $groupData['displayName'];
  171. }
  172. $backends[] = $backend;
  173. }
  174. } else if ($backend->groupExists($gid)) {
  175. $backends[] = $backend;
  176. }
  177. }
  178. if (count($backends) === 0) {
  179. return null;
  180. }
  181. $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this, $displayName);
  182. return $this->cachedGroups[$gid];
  183. }
  184. /**
  185. * @param string $gid
  186. * @return bool
  187. */
  188. public function groupExists($gid) {
  189. return $this->get($gid) instanceof IGroup;
  190. }
  191. /**
  192. * @param string $gid
  193. * @return \OC\Group\Group
  194. */
  195. public function createGroup($gid) {
  196. if ($gid === '' || $gid === null) {
  197. return false;
  198. } else if ($group = $this->get($gid)) {
  199. return $group;
  200. } else {
  201. $this->emit('\OC\Group', 'preCreate', array($gid));
  202. foreach ($this->backends as $backend) {
  203. if ($backend->implementsActions(\OC\Group\Backend::CREATE_GROUP)) {
  204. $backend->createGroup($gid);
  205. $group = $this->getGroupObject($gid);
  206. $this->emit('\OC\Group', 'postCreate', array($group));
  207. return $group;
  208. }
  209. }
  210. return null;
  211. }
  212. }
  213. /**
  214. * @param string $search
  215. * @param int $limit
  216. * @param int $offset
  217. * @return \OC\Group\Group[]
  218. */
  219. public function search($search, $limit = null, $offset = null) {
  220. $groups = array();
  221. foreach ($this->backends as $backend) {
  222. $groupIds = $backend->getGroups($search, $limit, $offset);
  223. foreach ($groupIds as $groupId) {
  224. $aGroup = $this->get($groupId);
  225. if ($aGroup instanceof IGroup) {
  226. $groups[$groupId] = $aGroup;
  227. } else {
  228. $this->logger->debug('Group "' . $groupId . '" was returned by search but not found through direct access', ['app' => 'core']);
  229. }
  230. }
  231. if (!is_null($limit) and $limit <= 0) {
  232. return array_values($groups);
  233. }
  234. }
  235. return array_values($groups);
  236. }
  237. /**
  238. * @param IUser|null $user
  239. * @return \OC\Group\Group[]
  240. */
  241. public function getUserGroups(IUser $user= null) {
  242. if (!$user instanceof IUser) {
  243. return [];
  244. }
  245. return $this->getUserIdGroups($user->getUID());
  246. }
  247. /**
  248. * @param string $uid the user id
  249. * @return \OC\Group\Group[]
  250. */
  251. public function getUserIdGroups($uid) {
  252. if (isset($this->cachedUserGroups[$uid])) {
  253. return $this->cachedUserGroups[$uid];
  254. }
  255. $groups = array();
  256. foreach ($this->backends as $backend) {
  257. $groupIds = $backend->getUserGroups($uid);
  258. if (is_array($groupIds)) {
  259. foreach ($groupIds as $groupId) {
  260. $aGroup = $this->get($groupId);
  261. if ($aGroup instanceof IGroup) {
  262. $groups[$groupId] = $aGroup;
  263. } else {
  264. $this->logger->debug('User "' . $uid . '" belongs to deleted group: "' . $groupId . '"', ['app' => 'core']);
  265. }
  266. }
  267. }
  268. }
  269. $this->cachedUserGroups[$uid] = $groups;
  270. return $this->cachedUserGroups[$uid];
  271. }
  272. /**
  273. * Checks if a userId is in the admin group
  274. * @param string $userId
  275. * @return bool if admin
  276. */
  277. public function isAdmin($userId) {
  278. foreach ($this->backends as $backend) {
  279. if ($backend->implementsActions(\OC\Group\Backend::IS_ADMIN) && $backend->isAdmin($userId)) {
  280. return true;
  281. }
  282. }
  283. return $this->isInGroup($userId, 'admin');
  284. }
  285. /**
  286. * Checks if a userId is in a group
  287. * @param string $userId
  288. * @param string $group
  289. * @return bool if in group
  290. */
  291. public function isInGroup($userId, $group) {
  292. return array_key_exists($group, $this->getUserIdGroups($userId));
  293. }
  294. /**
  295. * get a list of group ids for a user
  296. * @param IUser $user
  297. * @return array with group ids
  298. */
  299. public function getUserGroupIds(IUser $user) {
  300. return array_map(function($value) {
  301. return (string) $value;
  302. }, array_keys($this->getUserGroups($user)));
  303. }
  304. /**
  305. * get an array of groupid and displayName for a user
  306. * @param IUser $user
  307. * @return array ['displayName' => displayname]
  308. */
  309. public function getUserGroupNames(IUser $user) {
  310. return array_map(function($group) {
  311. return array('displayName' => $group->getDisplayName());
  312. }, $this->getUserGroups($user));
  313. }
  314. /**
  315. * get a list of all display names in a group
  316. * @param string $gid
  317. * @param string $search
  318. * @param int $limit
  319. * @param int $offset
  320. * @return array an array of display names (value) and user ids (key)
  321. */
  322. public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  323. $group = $this->get($gid);
  324. if(is_null($group)) {
  325. return array();
  326. }
  327. $search = trim($search);
  328. $groupUsers = array();
  329. if(!empty($search)) {
  330. // only user backends have the capability to do a complex search for users
  331. $searchOffset = 0;
  332. $searchLimit = $limit * 100;
  333. if($limit === -1) {
  334. $searchLimit = 500;
  335. }
  336. do {
  337. $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset);
  338. foreach($filteredUsers as $filteredUser) {
  339. if($group->inGroup($filteredUser)) {
  340. $groupUsers[]= $filteredUser;
  341. }
  342. }
  343. $searchOffset += $searchLimit;
  344. } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit);
  345. if($limit === -1) {
  346. $groupUsers = array_slice($groupUsers, $offset);
  347. } else {
  348. $groupUsers = array_slice($groupUsers, $offset, $limit);
  349. }
  350. } else {
  351. $groupUsers = $group->searchUsers('', $limit, $offset);
  352. }
  353. $matchingUsers = array();
  354. foreach($groupUsers as $groupUser) {
  355. $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName();
  356. }
  357. return $matchingUsers;
  358. }
  359. /**
  360. * @return \OC\SubAdmin
  361. */
  362. public function getSubAdmin() {
  363. if (!$this->subAdmin) {
  364. $this->subAdmin = new \OC\SubAdmin(
  365. $this->userManager,
  366. $this,
  367. \OC::$server->getDatabaseConnection()
  368. );
  369. }
  370. return $this->subAdmin;
  371. }
  372. }