togglegroups.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Christopher Schäpers <kondou@ts.unde.re>
  5. * @author Georg Ehrke <georg@owncloud.com>
  6. * @author Jakob Sack <mail@jakobsack.de>
  7. * @author Lukas Reschke <lukas@owncloud.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @copyright Copyright (c) 2015, ownCloud, Inc.
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. OC_JSON::checkSubAdminUser();
  29. OCP\JSON::callCheck();
  30. $success = true;
  31. $username = (string)$_POST['username'];
  32. $group = (string)$_POST['group'];
  33. if($username === OC_User::getUser() && $group === "admin" && OC_User::isAdminUser($username)) {
  34. $l = \OC::$server->getL10N('core');
  35. OC_JSON::error(array( 'data' => array( 'message' => $l->t('Admins can\'t remove themself from the admin group'))));
  36. exit();
  37. }
  38. if(!OC_User::isAdminUser(OC_User::getUser())
  39. && (!OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username)
  40. || !OC_SubAdmin::isGroupAccessible(OC_User::getUser(), $group))) {
  41. $l = \OC::$server->getL10N('core');
  42. OC_JSON::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  43. exit();
  44. }
  45. if(!OC_Group::groupExists($group)) {
  46. OC_Group::createGroup($group);
  47. }
  48. $l = \OC::$server->getL10N('settings');
  49. $error = $l->t("Unable to add user to group %s", $group);
  50. $action = "add";
  51. // Toggle group
  52. if( OC_Group::inGroup( $username, $group )) {
  53. $action = "remove";
  54. $error = $l->t("Unable to remove user from group %s", $group);
  55. $success = OC_Group::removeFromGroup( $username, $group );
  56. $usersInGroup=OC_Group::usersInGroup($group);
  57. if(count($usersInGroup) === 0) {
  58. OC_Group::deleteGroup($group);
  59. }
  60. }
  61. else{
  62. $success = OC_Group::addToGroup( $username, $group );
  63. }
  64. // Return Success story
  65. if( $success ) {
  66. OC_JSON::success(array("data" => array( "username" => $username, "action" => $action, "groupname" => $group )));
  67. }
  68. else{
  69. OC_JSON::error(array("data" => array( "message" => $error )));
  70. }