You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Group_Proxy.php 8.5KB

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