Group_Proxy.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Johannes Leuker <j.leuker@hosting.de>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin McCorkell <robin@mccorkell.me.uk>
  12. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  13. *
  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 OCA\User_LDAP;
  30. use OC\ServerNotAvailableException;
  31. use OCP\Group\Backend\IBatchMethodsBackend;
  32. use OCP\Group\Backend\IDeleteGroupBackend;
  33. use OCP\Group\Backend\IGetDisplayNameBackend;
  34. use OCP\Group\Backend\IGroupDetailsBackend;
  35. use OCP\Group\Backend\INamedBackend;
  36. use OCP\GroupInterface;
  37. use OCP\IConfig;
  38. use OCP\IUserManager;
  39. class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend, IBatchMethodsBackend {
  40. private $backends = [];
  41. private ?Group_LDAP $refBackend = null;
  42. private Helper $helper;
  43. private GroupPluginManager $groupPluginManager;
  44. private bool $isSetUp = false;
  45. private IConfig $config;
  46. private IUserManager $ncUserManager;
  47. public function __construct(
  48. Helper $helper,
  49. ILDAPWrapper $ldap,
  50. AccessFactory $accessFactory,
  51. GroupPluginManager $groupPluginManager,
  52. IConfig $config,
  53. IUserManager $ncUserManager,
  54. ) {
  55. parent::__construct($ldap, $accessFactory);
  56. $this->helper = $helper;
  57. $this->groupPluginManager = $groupPluginManager;
  58. $this->config = $config;
  59. $this->ncUserManager = $ncUserManager;
  60. }
  61. protected function setup(): void {
  62. if ($this->isSetUp) {
  63. return;
  64. }
  65. $serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
  66. foreach ($serverConfigPrefixes as $configPrefix) {
  67. $this->backends[$configPrefix] =
  68. new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager, $this->config, $this->ncUserManager);
  69. if (is_null($this->refBackend)) {
  70. $this->refBackend = &$this->backends[$configPrefix];
  71. }
  72. }
  73. $this->isSetUp = true;
  74. }
  75. /**
  76. * Tries the backends one after the other until a positive result is returned from the specified method
  77. *
  78. * @param string $id the gid connected to the request
  79. * @param string $method the method of the group backend that shall be called
  80. * @param array $parameters an array of parameters to be passed
  81. * @return mixed the result of the method or false
  82. */
  83. protected function walkBackends($id, $method, $parameters) {
  84. $this->setup();
  85. $gid = $id;
  86. $cacheKey = $this->getGroupCacheKey($gid);
  87. foreach ($this->backends as $configPrefix => $backend) {
  88. if ($result = call_user_func_array([$backend, $method], $parameters)) {
  89. if (!$this->isSingleBackend()) {
  90. $this->writeToCache($cacheKey, $configPrefix);
  91. }
  92. return $result;
  93. }
  94. }
  95. return false;
  96. }
  97. /**
  98. * Asks the backend connected to the server that supposely takes care of the gid from the request.
  99. *
  100. * @param string $id the gid connected to the request
  101. * @param string $method the method of the group backend that shall be called
  102. * @param array $parameters an array of parameters to be passed
  103. * @param mixed $passOnWhen the result matches this variable
  104. * @return mixed the result of the method or false
  105. */
  106. protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen) {
  107. $this->setup();
  108. $gid = $id;
  109. $cacheKey = $this->getGroupCacheKey($gid);
  110. $prefix = $this->getFromCache($cacheKey);
  111. //in case the uid has been found in the past, try this stored connection first
  112. if (!is_null($prefix)) {
  113. if (isset($this->backends[$prefix])) {
  114. $result = call_user_func_array([$this->backends[$prefix], $method], $parameters);
  115. if ($result === $passOnWhen) {
  116. //not found here, reset cache to null if group vanished
  117. //because sometimes methods return false with a reason
  118. $groupExists = call_user_func_array(
  119. [$this->backends[$prefix], 'groupExists'],
  120. [$gid]
  121. );
  122. if (!$groupExists) {
  123. $this->writeToCache($cacheKey, null);
  124. }
  125. }
  126. return $result;
  127. }
  128. }
  129. return false;
  130. }
  131. protected function activeBackends(): int {
  132. $this->setup();
  133. return count($this->backends);
  134. }
  135. /**
  136. * is user in group?
  137. *
  138. * @param string $uid uid of the user
  139. * @param string $gid gid of the group
  140. * @return bool
  141. *
  142. * Checks whether the user is member of a group or not.
  143. */
  144. public function inGroup($uid, $gid) {
  145. return $this->handleRequest($gid, 'inGroup', [$uid, $gid]);
  146. }
  147. /**
  148. * Get all groups a user belongs to
  149. *
  150. * @param string $uid Name of the user
  151. * @return string[] with group names
  152. *
  153. * This function fetches all groups a user belongs to. It does not check
  154. * if the user exists at all.
  155. */
  156. public function getUserGroups($uid) {
  157. $this->setup();
  158. $groups = [];
  159. foreach ($this->backends as $backend) {
  160. $backendGroups = $backend->getUserGroups($uid);
  161. if (is_array($backendGroups)) {
  162. $groups = array_merge($groups, $backendGroups);
  163. }
  164. }
  165. return $groups;
  166. }
  167. /**
  168. * get a list of all users in a group
  169. *
  170. * @return array<int,string> user ids
  171. */
  172. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  173. $this->setup();
  174. $users = [];
  175. foreach ($this->backends as $backend) {
  176. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  177. if (is_array($backendUsers)) {
  178. $users = array_merge($users, $backendUsers);
  179. }
  180. }
  181. return $users;
  182. }
  183. /**
  184. * @param string $gid
  185. * @return bool
  186. */
  187. public function createGroup($gid) {
  188. return $this->handleRequest(
  189. $gid, 'createGroup', [$gid]);
  190. }
  191. /**
  192. * delete a group
  193. */
  194. public function deleteGroup(string $gid): bool {
  195. return $this->handleRequest(
  196. $gid, 'deleteGroup', [$gid]);
  197. }
  198. /**
  199. * Add a user to a group
  200. *
  201. * @param string $uid Name of the user to add to group
  202. * @param string $gid Name of the group in which add the user
  203. * @return bool
  204. *
  205. * Adds a user to a group.
  206. */
  207. public function addToGroup($uid, $gid) {
  208. return $this->handleRequest(
  209. $gid, 'addToGroup', [$uid, $gid]);
  210. }
  211. /**
  212. * Removes a user from a group
  213. *
  214. * @param string $uid Name of the user to remove from group
  215. * @param string $gid Name of the group from which remove the user
  216. * @return bool
  217. *
  218. * removes the user from a group.
  219. */
  220. public function removeFromGroup($uid, $gid) {
  221. return $this->handleRequest(
  222. $gid, 'removeFromGroup', [$uid, $gid]);
  223. }
  224. /**
  225. * returns the number of users in a group, who match the search term
  226. *
  227. * @param string $gid the internal group name
  228. * @param string $search optional, a search string
  229. * @return int|bool
  230. */
  231. public function countUsersInGroup($gid, $search = '') {
  232. return $this->handleRequest(
  233. $gid, 'countUsersInGroup', [$gid, $search]);
  234. }
  235. /**
  236. * get an array with group details
  237. *
  238. * @param string $gid
  239. * @return array|false
  240. */
  241. public function getGroupDetails($gid) {
  242. return $this->handleRequest(
  243. $gid, 'getGroupDetails', [$gid]);
  244. }
  245. /**
  246. * {@inheritdoc}
  247. */
  248. public function getGroupsDetails(array $gids): array {
  249. if (!($this instanceof IGroupDetailsBackend || $this->implementsActions(GroupInterface::GROUP_DETAILS))) {
  250. throw new \Exception("Should not have been called");
  251. }
  252. $groupData = [];
  253. foreach ($gids as $gid) {
  254. $groupData[$gid] = $this->handleRequest($gid, 'getGroupDetails', [$gid]);
  255. }
  256. return $groupData;
  257. }
  258. /**
  259. * get a list of all groups
  260. *
  261. * @return string[] with group names
  262. *
  263. * Returns a list with all groups
  264. */
  265. public function getGroups($search = '', $limit = -1, $offset = 0) {
  266. $this->setup();
  267. $groups = [];
  268. foreach ($this->backends as $backend) {
  269. $backendGroups = $backend->getGroups($search, $limit, $offset);
  270. if (is_array($backendGroups)) {
  271. $groups = array_merge($groups, $backendGroups);
  272. }
  273. }
  274. return $groups;
  275. }
  276. /**
  277. * check if a group exists
  278. *
  279. * @param string $gid
  280. * @return bool
  281. */
  282. public function groupExists($gid) {
  283. return $this->handleRequest($gid, 'groupExists', [$gid]);
  284. }
  285. /**
  286. * Check if a group exists
  287. *
  288. * @throws ServerNotAvailableException
  289. */
  290. public function groupExistsOnLDAP(string $gid, bool $ignoreCache = false): bool {
  291. return $this->handleRequest($gid, 'groupExistsOnLDAP', [$gid, $ignoreCache]);
  292. }
  293. /**
  294. * returns the groupname for the given LDAP DN, if available
  295. */
  296. public function dn2GroupName(string $dn): string|false {
  297. $id = 'DN,' . $dn;
  298. return $this->handleRequest($id, 'dn2GroupName', [$dn]);
  299. }
  300. /**
  301. * {@inheritdoc}
  302. */
  303. public function groupsExists(array $gids): array {
  304. return array_values(array_filter(
  305. $gids,
  306. fn (string $gid): bool => $this->handleRequest($gid, 'groupExists', [$gid]),
  307. ));
  308. }
  309. /**
  310. * Check if backend implements actions
  311. *
  312. * @param int $actions bitwise-or'ed actions
  313. * @return boolean
  314. *
  315. * Returns the supported actions as int to be
  316. * compared with \OCP\GroupInterface::CREATE_GROUP etc.
  317. */
  318. public function implementsActions($actions) {
  319. $this->setup();
  320. //it's the same across all our user backends obviously
  321. return $this->refBackend->implementsActions($actions);
  322. }
  323. /**
  324. * Return access for LDAP interaction.
  325. *
  326. * @param string $gid
  327. * @return Access instance of Access for LDAP interaction
  328. */
  329. public function getLDAPAccess($gid) {
  330. return $this->handleRequest($gid, 'getLDAPAccess', [$gid]);
  331. }
  332. /**
  333. * Return a new LDAP connection for the specified group.
  334. * The connection needs to be closed manually.
  335. *
  336. * @param string $gid
  337. * @return resource|\LDAP\Connection The LDAP connection
  338. */
  339. public function getNewLDAPConnection($gid) {
  340. return $this->handleRequest($gid, 'getNewLDAPConnection', [$gid]);
  341. }
  342. public function getDisplayName(string $gid): string {
  343. return $this->handleRequest($gid, 'getDisplayName', [$gid]);
  344. }
  345. /**
  346. * Backend name to be shown in group management
  347. * @return string the name of the backend to be shown
  348. * @since 22.0.0
  349. */
  350. public function getBackendName(): string {
  351. return 'LDAP';
  352. }
  353. public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
  354. return $this->handleRequest($gid, 'searchInGroup', [$gid, $search, $limit, $offset]);
  355. }
  356. public function addRelationshipToCaches(string $uid, ?string $dnUser, string $gid): void {
  357. $this->handleRequest($gid, 'addRelationshipToCaches', [$uid, $dnUser, $gid]);
  358. }
  359. }