togglegroups.php 900 B

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