togglegroups.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. OC_JSON::checkSubAdminUser();
  3. OCP\JSON::callCheck();
  4. $success = true;
  5. $username = $_POST["username"];
  6. $group = $_POST["group"];
  7. if($username === OC_User::getUser() && $group === "admin" && OC_User::isAdminUser($username)) {
  8. $l = OC_L10N::get('core');
  9. OC_JSON::error(array( 'data' => array( 'message' => $l->t('Admins can\'t remove themself from the admin group'))));
  10. exit();
  11. }
  12. if(!OC_User::isAdminUser(OC_User::getUser())
  13. && (!OC_SubAdmin::isUserAccessible(OC_User::getUser(), $username)
  14. || !OC_SubAdmin::isGroupAccessible(OC_User::getUser(), $group))) {
  15. $l = OC_L10N::get('core');
  16. OC_JSON::error(array( 'data' => array( 'message' => $l->t('Authentication error') )));
  17. exit();
  18. }
  19. if(!OC_Group::groupExists($group)) {
  20. OC_Group::createGroup($group);
  21. }
  22. $l = OC_L10N::get('settings');
  23. $error = $l->t("Unable to add user to group %s", $group);
  24. $action = "add";
  25. // Toggle group
  26. if( OC_Group::inGroup( $username, $group )) {
  27. $action = "remove";
  28. $error = $l->t("Unable to remove user from group %s", $group);
  29. $success = OC_Group::removeFromGroup( $username, $group );
  30. $usersInGroup=OC_Group::usersInGroup($group);
  31. if(count($usersInGroup) === 0) {
  32. OC_Group::deleteGroup($group);
  33. }
  34. }
  35. else{
  36. $success = OC_Group::addToGroup( $username, $group );
  37. }
  38. // Return Success story
  39. if( $success ) {
  40. OC_JSON::success(array("data" => array( "username" => $username, "action" => $action, "groupname" => $group )));
  41. }
  42. else{
  43. OC_JSON::error(array("data" => array( "message" => $error )));
  44. }