togglegroups.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $isUserAccessible = false;
  38. $isGroupAccessible = false;
  39. $currentUserObject = \OC::$server->getUserSession()->getUser();
  40. $targetUserObject = \OC::$server->getUserManager()->get($username);
  41. $targetGroupObject = \OC::$server->getGroupManager()->get($group);
  42. if($targetUserObject !== null && $currentUserObject !== null && $targetGroupObject !== null) {
  43. $isUserAccessible = \OC::$server->getGroupManager()->getSubAdmin()->isUserAccessible($currentUserObject, $targetUserObject);
  44. $isGroupAccessible = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdminofGroup($currentUserObject, $targetGroupObject);
  45. }
  46. if(!OC_User::isAdminUser(OC_User::getUser())
  47. && (!$isUserAccessible
  48. || !$isGroupAccessible)) {
  49. $l = \OC::$server->getL10N('core');
  50. OC_JSON::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  51. exit();
  52. }
  53. if(!OC_Group::groupExists($group)) {
  54. OC_Group::createGroup($group);
  55. }
  56. $l = \OC::$server->getL10N('settings');
  57. $error = $l->t("Unable to add user to group %s", $group);
  58. $action = "add";
  59. // Toggle group
  60. if( OC_Group::inGroup( $username, $group )) {
  61. $action = "remove";
  62. $error = $l->t("Unable to remove user from group %s", $group);
  63. $success = OC_Group::removeFromGroup( $username, $group );
  64. $usersInGroup=OC_Group::usersInGroup($group);
  65. }
  66. else{
  67. $success = OC_Group::addToGroup( $username, $group );
  68. }
  69. // Return Success story
  70. if( $success ) {
  71. OC_JSON::success(array("data" => array( "username" => $username, "action" => $action, "groupname" => $group )));
  72. }
  73. else{
  74. OC_JSON::error(array("data" => array( "message" => $error )));
  75. }