clearMappings.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. use OCA\User_LDAP\Mapping\UserMapping;
  26. use OCA\User_LDAP\Mapping\GroupMapping;
  27. // Check user and app status
  28. \OC_JSON::checkAdminUser();
  29. \OC_JSON::checkAppEnabled('user_ldap');
  30. \OC_JSON::callCheck();
  31. $subject = (string)$_POST['ldap_clear_mapping'];
  32. $mapping = null;
  33. try {
  34. if($subject === 'user') {
  35. $mapping = new UserMapping(\OC::$server->getDatabaseConnection());
  36. $result = $mapping->clearCb(
  37. function ($uid) {
  38. \OC::$server->getUserManager()->emit('\OC\User', 'preUnassignedUserId', [$uid]);
  39. },
  40. function ($uid) {
  41. \OC::$server->getUserManager()->emit('\OC\User', 'postUnassignedUserId', [$uid]);
  42. }
  43. );
  44. } else if($subject === 'group') {
  45. $mapping = new GroupMapping(\OC::$server->getDatabaseConnection());
  46. $result = $mapping->clear();
  47. }
  48. if($mapping === null || !$result) {
  49. $l = \OC::$server->getL10N('user_ldap');
  50. throw new \Exception($l->t('Failed to clear the mappings.'));
  51. }
  52. \OC_JSON::success();
  53. } catch (\Exception $e) {
  54. \OC_JSON::error(array('message' => $e->getMessage()));
  55. }