UserMapping.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. private IAssertion $assertion;
  20. protected const PROV_API_REGEX = '/\/ocs\/v[1-9].php\/cloud\/(groups|users)/';
  21. public function __construct(IDBConnection $dbc, IAssertion $assertion) {
  22. $this->assertion = $assertion;
  23. parent::__construct($dbc);
  24. }
  25. /**
  26. * @throws HintException
  27. */
  28. public function map($fdn, $name, $uuid): bool {
  29. try {
  30. $this->assertion->createUserIsLegit();
  31. } catch (HintException $e) {
  32. static $isProvisioningApi = null;
  33. if ($isProvisioningApi === null) {
  34. $request = Server::get(IRequest::class);
  35. $isProvisioningApi = \preg_match(self::PROV_API_REGEX, $request->getRequestUri()) === 1;
  36. }
  37. if ($isProvisioningApi) {
  38. // only throw when prov API is being used, since functionality
  39. // should not break for end users (e.g. when sharing).
  40. // On direct API usage, e.g. on users page, this is desired.
  41. throw $e;
  42. }
  43. return false;
  44. }
  45. return parent::map($fdn, $name, $uuid);
  46. }
  47. /**
  48. * returns the DB table name which holds the mappings
  49. * @return string
  50. */
  51. protected function getTableName(bool $includePrefix = true) {
  52. $p = $includePrefix ? '*PREFIX*' : '';
  53. return $p . 'ldap_user_mapping';
  54. }
  55. }