Group_Proxy.php 9.6 KB

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