1
0

UserMapping.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\User_LDAP\Mapping;
  8. use OCP\HintException;
  9. use OCP\IDBConnection;
  10. use OCP\IRequest;
  11. use OCP\Server;
  12. use OCP\Support\Subscription\IAssertion;
  13. /**
  14. * Class UserMapping
  15. *
  16. * @package OCA\User_LDAP\Mapping
  17. */
  18. class UserMapping extends AbstractMapping {
  19. protected const PROV_API_REGEX = '/\/ocs\/v[1-9].php\/cloud\/(groups|users)/';
  20. public function __construct(
  21. IDBConnection $dbc,
  22. private IAssertion $assertion,
  23. ) {
  24. parent::__construct($dbc);
  25. }
  26. /**
  27. * @throws HintException
  28. */
  29. public function map($fdn, $name, $uuid): bool {
  30. try {
  31. $this->assertion->createUserIsLegit();
  32. } catch (HintException $e) {
  33. static $isProvisioningApi = null;
  34. if ($isProvisioningApi === null) {
  35. $request = Server::get(IRequest::class);
  36. $isProvisioningApi = \preg_match(self::PROV_API_REGEX, $request->getRequestUri()) === 1;
  37. }
  38. if ($isProvisioningApi) {
  39. // only throw when prov API is being used, since functionality
  40. // should not break for end users (e.g. when sharing).
  41. // On direct API usage, e.g. on users page, this is desired.
  42. throw $e;
  43. }
  44. return false;
  45. }
  46. return parent::map($fdn, $name, $uuid);
  47. }
  48. /**
  49. * returns the DB table name which holds the mappings
  50. * @return string
  51. */
  52. protected function getTableName(bool $includePrefix = true) {
  53. $p = $includePrefix ? '*PREFIX*' : '';
  54. return $p . 'ldap_user_mapping';
  55. }
  56. }