Manager.php 9.8 KB

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