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. use OCP\Util;
  14. // Check user and app status
  15. \OC_JSON::checkAdminUser();
  16. \OC_JSON::checkAppEnabled('user_ldap');
  17. \OC_JSON::callCheck();
  18. $subject = (string)$_POST['ldap_clear_mapping'];
  19. $mapping = null;
  20. try {
  21. if ($subject === 'user') {
  22. $mapping = Server::get(UserMapping::class);
  23. /** @var IEventDispatcher $dispatcher */
  24. $dispatcher = Server::get(IEventDispatcher::class);
  25. $result = $mapping->clearCb(
  26. function (string $uid) use ($dispatcher): void {
  27. $dispatcher->dispatchTyped(new BeforeUserIdUnassignedEvent($uid));
  28. \OC::$server->getUserManager()->emit('\OC\User', 'preUnassignedUserId', [$uid]);
  29. },
  30. function (string $uid) use ($dispatcher): void {
  31. $dispatcher->dispatchTyped(new UserIdUnassignedEvent($uid));
  32. \OC::$server->getUserManager()->emit('\OC\User', 'postUnassignedUserId', [$uid]);
  33. }
  34. );
  35. } elseif ($subject === 'group') {
  36. $mapping = new GroupMapping(\OC::$server->getDatabaseConnection());
  37. $result = $mapping->clear();
  38. }
  39. if ($mapping === null || !$result) {
  40. $l = Util::getL10N('user_ldap');
  41. throw new \Exception($l->t('Failed to clear the mappings.'));
  42. }
  43. \OC_JSON::success();
  44. } catch (\Exception $e) {
  45. \OC_JSON::error(['message' => $e->getMessage()]);
  46. }