togglegroups.php 2.4 KB

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