Group_Proxy.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 OCP\Group\Backend\IDeleteGroupBackend;
  31. use OCP\Group\Backend\IGetDisplayNameBackend;
  32. use OCP\Group\Backend\INamedBackend;
  33. class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend {
  34. private $backends = [];
  35. private ?Group_LDAP $refBackend = null;
  36. private Helper $helper;
  37. private GroupPluginManager $groupPluginManager;
  38. private bool $isSetUp = false;
  39. public function __construct(
  40. Helper $helper,
  41. ILDAPWrapper $ldap,
  42. AccessFactory $accessFactory,
  43. GroupPluginManager $groupPluginManager
  44. ) {
  45. parent::__construct($ldap, $accessFactory);
  46. $this->helper = $helper;
  47. $this->groupPluginManager = $groupPluginManager;
  48. }
  49. protected function setup(): void {
  50. if ($this->isSetUp) {
  51. return;
  52. }
  53. $serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
  54. foreach ($serverConfigPrefixes as $configPrefix) {
  55. $this->backends[$configPrefix] =
  56. new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager);
  57. if (is_null($this->refBackend)) {
  58. $this->refBackend = &$this->backends[$configPrefix];
  59. }
  60. }
  61. $this->isSetUp = true;
  62. }
  63. /**
  64. * Tries the backends one after the other until a positive result is returned from the specified method
  65. *
  66. * @param string $id the gid connected to the request
  67. * @param string $method the method of the group backend that shall be called
  68. * @param array $parameters an array of parameters to be passed
  69. * @return mixed the result of the method or false
  70. */
  71. protected function walkBackends($id, $method, $parameters) {
  72. $this->setup();
  73. $gid = $id;
  74. $cacheKey = $this->getGroupCacheKey($gid);
  75. foreach ($this->backends as $configPrefix => $backend) {
  76. if ($result = call_user_func_array([$backend, $method], $parameters)) {
  77. if (!$this->isSingleBackend()) {
  78. $this->writeToCache($cacheKey, $configPrefix);
  79. }
  80. return $result;
  81. }
  82. }
  83. return false;
  84. }
  85. /**
  86. * Asks the backend connected to the server that supposely takes care of the gid from the request.
  87. *
  88. * @param string $id the gid connected to the request
  89. * @param string $method the method of the group backend that shall be called
  90. * @param array $parameters an array of parameters to be passed
  91. * @param mixed $passOnWhen the result matches this variable
  92. * @return mixed the result of the method or false
  93. */
  94. protected function callOnLastSeenOn($id, $method, $parameters, $passOnWhen) {
  95. $this->setup();
  96. $gid = $id;
  97. $cacheKey = $this->getGroupCacheKey($gid);
  98. $prefix = $this->getFromCache($cacheKey);
  99. //in case the uid has been found in the past, try this stored connection first
  100. if (!is_null($prefix)) {
  101. if (isset($this->backends[$prefix])) {
  102. $result = call_user_func_array([$this->backends[$prefix], $method], $parameters);
  103. if ($result === $passOnWhen) {
  104. //not found here, reset cache to null if group vanished
  105. //because sometimes methods return false with a reason
  106. $groupExists = call_user_func_array(
  107. [$this->backends[$prefix], 'groupExists'],
  108. [$gid]
  109. );
  110. if (!$groupExists) {
  111. $this->writeToCache($cacheKey, null);
  112. }
  113. }
  114. return $result;
  115. }
  116. }
  117. return false;
  118. }
  119. protected function activeBackends(): int {
  120. $this->setup();
  121. return count($this->backends);
  122. }
  123. /**
  124. * is user in group?
  125. *
  126. * @param string $uid uid of the user
  127. * @param string $gid gid of the group
  128. * @return bool
  129. *
  130. * Checks whether the user is member of a group or not.
  131. */
  132. public function inGroup($uid, $gid) {
  133. return $this->handleRequest($gid, 'inGroup', [$uid, $gid]);
  134. }
  135. /**
  136. * Get all groups a user belongs to
  137. *
  138. * @param string $uid Name of the user
  139. * @return string[] with group names
  140. *
  141. * This function fetches all groups a user belongs to. It does not check
  142. * if the user exists at all.
  143. */
  144. public function getUserGroups($uid) {
  145. $this->setup();
  146. $groups = [];
  147. foreach ($this->backends as $backend) {
  148. $backendGroups = $backend->getUserGroups($uid);
  149. if (is_array($backendGroups)) {
  150. $groups = array_merge($groups, $backendGroups);
  151. }
  152. }
  153. return $groups;
  154. }
  155. /**
  156. * get a list of all users in a group
  157. *
  158. * @return array<int,string> user ids
  159. */
  160. public function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) {
  161. $this->setup();
  162. $users = [];
  163. foreach ($this->backends as $backend) {
  164. $backendUsers = $backend->usersInGroup($gid, $search, $limit, $offset);
  165. if (is_array($backendUsers)) {
  166. $users = array_merge($users, $backendUsers);
  167. }
  168. }
  169. return $users;
  170. }
  171. /**
  172. * @param string $gid
  173. * @return bool
  174. */
  175. public function createGroup($gid) {
  176. return $this->handleRequest(
  177. $gid, 'createGroup', [$gid]);
  178. }
  179. /**
  180. * delete a group
  181. */
  182. public function deleteGroup(string $gid): bool {
  183. return $this->handleRequest(
  184. $gid, 'deleteGroup', [$gid]);
  185. }
  186. /**
  187. * Add a user to a group
  188. *
  189. * @param string $uid Name of the user to add to group
  190. * @param string $gid Name of the group in which add the user
  191. * @return bool
  192. *
  193. * Adds a user to a group.
  194. */
  195. public function addToGroup($uid, $gid) {
  196. return $this->handleRequest(
  197. $gid, 'addToGroup', [$uid, $gid]);
  198. }
  199. /**
  200. * Removes a user from a group
  201. *
  202. * @param string $uid Name of the user to remove from group
  203. * @param string $gid Name of the group from which remove the user
  204. * @return bool
  205. *
  206. * removes the user from a group.
  207. */
  208. public function removeFromGroup($uid, $gid) {
  209. return $this->handleRequest(
  210. $gid, 'removeFromGroup', [$uid, $gid]);
  211. }
  212. /**
  213. * returns the number of users in a group, who match the search term
  214. *
  215. * @param string $gid the internal group name
  216. * @param string $search optional, a search string
  217. * @return int|bool
  218. */
  219. public function countUsersInGroup($gid, $search = '') {
  220. return $this->handleRequest(
  221. $gid, 'countUsersInGroup', [$gid, $search]);
  222. }
  223. /**
  224. * get an array with group details
  225. *
  226. * @param string $gid
  227. * @return array|false
  228. */
  229. public function getGroupDetails($gid) {
  230. return $this->handleRequest(
  231. $gid, 'getGroupDetails', [$gid]);
  232. }
  233. /**
  234. * get a list of all groups
  235. *
  236. * @return string[] with group names
  237. *
  238. * Returns a list with all groups
  239. */
  240. public function getGroups($search = '', $limit = -1, $offset = 0) {
  241. $this->setup();
  242. $groups = [];
  243. foreach ($this->backends as $backend) {
  244. $backendGroups = $backend->getGroups($search, $limit, $offset);
  245. if (is_array($backendGroups)) {
  246. $groups = array_merge($groups, $backendGroups);
  247. }
  248. }
  249. return $groups;
  250. }
  251. /**
  252. * check if a group exists
  253. *
  254. * @param string $gid
  255. * @return bool
  256. */
  257. public function groupExists($gid) {
  258. return $this->handleRequest($gid, 'groupExists', [$gid]);
  259. }
  260. /**
  261. * Check if backend implements actions
  262. *
  263. * @param int $actions bitwise-or'ed actions
  264. * @return boolean
  265. *
  266. * Returns the supported actions as int to be
  267. * compared with \OCP\GroupInterface::CREATE_GROUP etc.
  268. */
  269. public function implementsActions($actions) {
  270. $this->setup();
  271. //it's the same across all our user backends obviously
  272. return $this->refBackend->implementsActions($actions);
  273. }
  274. /**
  275. * Return access for LDAP interaction.
  276. *
  277. * @param string $gid
  278. * @return Access instance of Access for LDAP interaction
  279. */
  280. public function getLDAPAccess($gid) {
  281. return $this->handleRequest($gid, 'getLDAPAccess', [$gid]);
  282. }
  283. /**
  284. * Return a new LDAP connection for the specified group.
  285. * The connection needs to be closed manually.
  286. *
  287. * @param string $gid
  288. * @return resource|\LDAP\Connection The LDAP connection
  289. */
  290. public function getNewLDAPConnection($gid) {
  291. return $this->handleRequest($gid, 'getNewLDAPConnection', [$gid]);
  292. }
  293. public function getDisplayName(string $gid): string {
  294. return $this->handleRequest($gid, 'getDisplayName', [$gid]);
  295. }
  296. /**
  297. * Backend name to be shown in group management
  298. * @return string the name of the backend to be shown
  299. * @since 22.0.0
  300. */
  301. public function getBackendName(): string {
  302. return 'LDAP';
  303. }
  304. public function searchInGroup(string $gid, string $search = '', int $limit = -1, int $offset = 0): array {
  305. return $this->handleRequest($gid, 'searchInGroup', [$gid, $search, $limit, $offset]);
  306. }
  307. }