togglegroups.php 1.2 KB

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