Group_Proxy.php 9.2 KB

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