togglegroups.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Georg Ehrke <georg@owncloud.com>
  8. * @author Jakob Sack <mail@jakobsack.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  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. $isUserAccessible = false;
  39. $isGroupAccessible = false;
  40. $currentUserObject = \OC::$server->getUserSession()->getUser();
  41. $targetUserObject = \OC::$server->getUserManager()->get($username);
  42. $targetGroupObject = \OC::$server->getGroupManager()->get($group);
  43. if($targetUserObject !== null && $currentUserObject !== null && $targetGroupObject !== null) {
  44. $isUserAccessible = \OC::$server->getGroupManager()->getSubAdmin()->isUserAccessible($currentUserObject, $targetUserObject);
  45. $isGroupAccessible = \OC::$server->getGroupManager()->getSubAdmin()->isSubAdminofGroup($currentUserObject, $targetGroupObject);
  46. }
  47. if(!OC_User::isAdminUser(OC_User::getUser())
  48. && (!$isUserAccessible
  49. || !$isGroupAccessible)) {
  50. $l = \OC::$server->getL10N('core');
  51. OC_JSON::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  52. exit();
  53. }
  54. if(!OC_Group::groupExists($group)) {
  55. OC_Group::createGroup($group);
  56. }
  57. $l = \OC::$server->getL10N('settings');
  58. $error = $l->t("Unable to add user to group %s", $group);
  59. $action = "add";
  60. // Toggle group
  61. if( OC_Group::inGroup( $username, $group )) {
  62. $action = "remove";
  63. $error = $l->t("Unable to remove user from group %s", $group);
  64. $success = OC_Group::removeFromGroup( $username, $group );
  65. $usersInGroup=OC_Group::usersInGroup($group);
  66. }
  67. else{
  68. $success = OC_Group::addToGroup( $username, $group );
  69. }
  70. // Return Success story
  71. if( $success ) {
  72. OC_JSON::success(array("data" => array( "username" => $username, "action" => $action, "groupname" => $group )));
  73. }
  74. else{
  75. OC_JSON::error(array("data" => array( "message" => $error )));
  76. }