PageRenderTime 71ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/private/group/manager.php

https://gitlab.com/Red54/core
PHP | 296 lines | 150 code | 26 blank | 120 comment | 20 complexity | 525280d8b913847d5b5489e366fd88ea MD5 | raw file
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author macjohnny <estebanmarin@gmx.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  11. * @author voxsim <Simon Vocella>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Group;
  30. use OC\Hooks\PublicEmitter;
  31. use OCP\IGroupManager;
  32. /**
  33. * Class Manager
  34. *
  35. * Hooks available in scope \OC\Group:
  36. * - preAddUser(\OC\Group\Group $group, \OC\User\User $user)
  37. * - postAddUser(\OC\Group\Group $group, \OC\User\User $user)
  38. * - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
  39. * - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user)
  40. * - preDelete(\OC\Group\Group $group)
  41. * - postDelete(\OC\Group\Group $group)
  42. * - preCreate(string $groupId)
  43. * - postCreate(\OC\Group\Group $group)
  44. *
  45. * @package OC\Group
  46. */
  47. class Manager extends PublicEmitter implements IGroupManager {
  48. /**
  49. * @var \OC_Group_Backend[]|\OC_Group_Database[] $backends
  50. */
  51. private $backends = array();
  52. /**
  53. * @var \OC\User\Manager $userManager
  54. */
  55. private $userManager;
  56. /**
  57. * @var \OC\Group\Group[]
  58. */
  59. private $cachedGroups = array();
  60. /**
  61. * @var \OC\Group\Group[]
  62. */
  63. private $cachedUserGroups = array();
  64. /**
  65. * @param \OC\User\Manager $userManager
  66. */
  67. public function __construct($userManager) {
  68. $this->userManager = $userManager;
  69. $cachedGroups = & $this->cachedGroups;
  70. $cachedUserGroups = & $this->cachedUserGroups;
  71. $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
  72. /**
  73. * @var \OC\Group\Group $group
  74. */
  75. unset($cachedGroups[$group->getGID()]);
  76. $cachedUserGroups = array();
  77. });
  78. $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
  79. /**
  80. * @var \OC\Group\Group $group
  81. */
  82. $cachedUserGroups = array();
  83. });
  84. $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
  85. /**
  86. * @var \OC\Group\Group $group
  87. */
  88. $cachedUserGroups = array();
  89. });
  90. }
  91. /**
  92. * @param \OC_Group_Backend $backend
  93. */
  94. public function addBackend($backend) {
  95. $this->backends[] = $backend;
  96. }
  97. public function clearBackends() {
  98. $this->backends = array();
  99. $this->cachedGroups = array();
  100. $this->cachedUserGroups = array();
  101. }
  102. /**
  103. * @param string $gid
  104. * @return \OC\Group\Group
  105. */
  106. public function get($gid) {
  107. if (isset($this->cachedGroups[$gid])) {
  108. return $this->cachedGroups[$gid];
  109. }
  110. return $this->getGroupObject($gid);
  111. }
  112. protected function getGroupObject($gid) {
  113. $backends = array();
  114. foreach ($this->backends as $backend) {
  115. if ($backend->groupExists($gid)) {
  116. $backends[] = $backend;
  117. }
  118. }
  119. if (count($backends) === 0) {
  120. return null;
  121. }
  122. $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this);
  123. return $this->cachedGroups[$gid];
  124. }
  125. /**
  126. * @param string $gid
  127. * @return bool
  128. */
  129. public function groupExists($gid) {
  130. return !is_null($this->get($gid));
  131. }
  132. /**
  133. * @param string $gid
  134. * @return \OC\Group\Group
  135. */
  136. public function createGroup($gid) {
  137. if ($gid === '' || is_null($gid)) {
  138. return false;
  139. } else if ($group = $this->get($gid)) {
  140. return $group;
  141. } else {
  142. $this->emit('\OC\Group', 'preCreate', array($gid));
  143. foreach ($this->backends as $backend) {
  144. if ($backend->implementsActions(\OC_Group_Backend::CREATE_GROUP)) {
  145. $backend->createGroup($gid);
  146. $group = $this->getGroupObject($gid);
  147. $this->emit('\OC\Group', 'postCreate', array($group));
  148. return $group;
  149. }
  150. }
  151. return null;
  152. }
  153. }
  154. /**
  155. * @param string $search
  156. * @param int $limit
  157. * @param int $offset
  158. * @return \OC\Group\Group[]
  159. */
  160. public function search($search, $limit = null, $offset = null) {
  161. $groups = array();
  162. foreach ($this->backends as $backend) {
  163. $groupIds = $backend->getGroups($search, $limit, $offset);
  164. foreach ($groupIds as $groupId) {
  165. $groups[$groupId] = $this->get($groupId);
  166. }
  167. if (!is_null($limit) and $limit <= 0) {
  168. return array_values($groups);
  169. }
  170. }
  171. return array_values($groups);
  172. }
  173. /**
  174. * @param \OC\User\User $user
  175. * @return \OC\Group\Group[]
  176. */
  177. public function getUserGroups($user) {
  178. return $this->getUserIdGroups($user->getUID());
  179. }
  180. /**
  181. * @param string $uid the user id
  182. * @return \OC\Group\Group[]
  183. */
  184. public function getUserIdGroups($uid) {
  185. if (isset($this->cachedUserGroups[$uid])) {
  186. return $this->cachedUserGroups[$uid];
  187. }
  188. $groups = array();
  189. foreach ($this->backends as $backend) {
  190. $groupIds = $backend->getUserGroups($uid);
  191. if (is_array($groupIds)) {
  192. foreach ($groupIds as $groupId) {
  193. $groups[$groupId] = $this->get($groupId);
  194. }
  195. }
  196. }
  197. $this->cachedUserGroups[$uid] = $groups;
  198. return $this->cachedUserGroups[$uid];
  199. }
  200. /**
  201. * Checks if a userId is in the admin group
  202. * @param string $userId
  203. * @return bool if admin
  204. */
  205. public function isAdmin($userId) {
  206. return $this->isInGroup($userId, 'admin');
  207. }
  208. /**
  209. * Checks if a userId is in a group
  210. * @param string $userId
  211. * @param group $group
  212. * @return bool if in group
  213. */
  214. public function isInGroup($userId, $group) {
  215. return array_key_exists($group, $this->getUserIdGroups($userId));
  216. }
  217. /**
  218. * get a list of group ids for a user
  219. * @param \OC\User\User $user
  220. * @return array with group ids
  221. */
  222. public function getUserGroupIds($user) {
  223. return array_keys($this->getUserGroups($user));
  224. }
  225. /**
  226. * get a list of all display names in a group
  227. * @param string $gid
  228. * @param string $search
  229. * @param int $limit
  230. * @param int $offset
  231. * @return array an array of display names (value) and user ids (key)
  232. */
  233. public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  234. $group = $this->get($gid);
  235. if(is_null($group)) {
  236. return array();
  237. }
  238. $search = trim($search);
  239. $groupUsers = array();
  240. if(!empty($search)) {
  241. // only user backends have the capability to do a complex search for users
  242. $searchOffset = 0;
  243. $searchLimit = $limit * 100;
  244. if($limit === -1) {
  245. $searchLimit = 500;
  246. }
  247. do {
  248. $filteredUsers = $this->userManager->searchDisplayName($search, $searchLimit, $searchOffset);
  249. foreach($filteredUsers as $filteredUser) {
  250. if($group->inGroup($filteredUser)) {
  251. $groupUsers[]= $filteredUser;
  252. }
  253. }
  254. $searchOffset += $searchLimit;
  255. } while(count($groupUsers) < $searchLimit+$offset && count($filteredUsers) >= $searchLimit);
  256. if($limit === -1) {
  257. $groupUsers = array_slice($groupUsers, $offset);
  258. } else {
  259. $groupUsers = array_slice($groupUsers, $offset, $limit);
  260. }
  261. } else {
  262. $groupUsers = $group->searchUsers('', $limit, $offset);
  263. }
  264. $matchingUsers = array();
  265. foreach($groupUsers as $groupUser) {
  266. $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName();
  267. }
  268. return $matchingUsers;
  269. }
  270. }