clearMappings.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. use OCA\User_LDAP\Mapping\GroupMapping;
  8. use OCA\User_LDAP\Mapping\UserMapping;
  9. use OCP\EventDispatcher\IEventDispatcher;
  10. use OCP\Server;
  11. use OCP\User\Events\BeforeUserIdUnassignedEvent;
  12. use OCP\User\Events\UserIdUnassignedEvent;
  13. // Check user and app status
  14. \OC_JSON::checkAdminUser();
  15. \OC_JSON::checkAppEnabled('user_ldap');
  16. \OC_JSON::callCheck();
  17. $subject = (string)$_POST['ldap_clear_mapping'];
  18. $mapping = null;
  19. try {
  20. if ($subject === 'user') {
  21. $mapping = Server::get(UserMapping::class);
  22. /** @var IEventDispatcher $dispatcher */
  23. $dispatcher = Server::get(IEventDispatcher::class);
  24. $result = $mapping->clearCb(
  25. function (string $uid) use ($dispatcher): void {
  26. $dispatcher->dispatchTyped(new BeforeUserIdUnassignedEvent($uid));
  27. \OC::$server->getUserManager()->emit('\OC\User', 'preUnassignedUserId', [$uid]);
  28. },
  29. function (string $uid) use ($dispatcher): void {
  30. $dispatcher->dispatchTyped(new UserIdUnassignedEvent($uid));
  31. \OC::$server->getUserManager()->emit('\OC\User', 'postUnassignedUserId', [$uid]);
  32. }
  33. );
  34. } elseif ($subject === 'group') {
  35. $mapping = new GroupMapping(\OC::$server->getDatabaseConnection());
  36. $result = $mapping->clear();
  37. }
  38. if ($mapping === null || !$result) {
  39. $l = \OCP\Util::getL10N('user_ldap');
  40. throw new \Exception($l->t('Failed to clear the mappings.'));
  41. }
  42. \OC_JSON::success();
  43. } catch (\Exception $e) {
  44. \OC_JSON::error(['message' => $e->getMessage()]);
  45. }