Group_Proxy.php 8.9 KB

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