clearMappings.php 1.2 KB

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