togglegroups.php 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. // Init owncloud
  3. require_once('../../lib/base.php');
  4. OC_JSON::checkAdminUser();
  5. OCP\JSON::callCheck();
  6. $success = true;
  7. $error = "add user to";
  8. $action = "add";
  9. $username = $_POST["username"];
  10. $group = OC_Util::sanitizeHTML($_POST["group"]);
  11. if(!OC_Group::groupExists($group)){
  12. OC_Group::createGroup($group);
  13. }
  14. // Toggle group
  15. if( OC_Group::inGroup( $username, $group )){
  16. $action = "remove";
  17. $error = "remove user from";
  18. $success = OC_Group::removeFromGroup( $username, $group );
  19. $usersInGroup=OC_Group::usersInGroup($group);
  20. if(count($usersInGroup)==0){
  21. OC_Group::deleteGroup($group);
  22. }
  23. }
  24. else{
  25. $success = OC_Group::addToGroup( $username, $group );
  26. }
  27. // Return Success story
  28. if( $success ){
  29. OC_JSON::success(array("data" => array( "username" => $username, "action" => $action, "groupname" => $group )));
  30. }
  31. else{
  32. OC_JSON::error(array("data" => array( "message" => "Unable to $error group $group" )));
  33. }