function healPermissionGroup(SharingTableFlag $flag)
 {
     if ($flag->getObjectId() > 0) {
         try {
             $obj = Objects::findObject($flag->getObjectId());
             if (!$obj instanceof ContentDataObject) {
                 $flag->delete();
                 // if object does not exists then delete the flag
                 return;
             }
             DB::beginWork();
             // update sharing table
             $obj->addToSharingTable();
             DB::commit();
         } catch (Exception $e) {
             DB::rollback();
             Logger::log("Failed to heal object permissions for object " . $flag->getObjectId() . " (flag_id = " . $flag->getId() . ")");
             return false;
         }
         // delete flag
         $flag->delete();
         return true;
     } else {
         // heal
         $controller = new SharingTableController();
         $permissions_string = $flag->getPermissionString();
         $permission_group_id = $flag->getPermissionGroupId();
         $permissions = json_decode($permissions_string);
         if ($flag->getMemberId() > 0) {
             foreach ($permissions as $p) {
                 if (!isset($p->m)) {
                     $p->m = $flag->getMemberId();
                 }
             }
         }
         try {
             DB::beginWork();
             // update sharing table
             $controller->afterPermissionChanged($permission_group_id, $permissions);
             DB::commit();
         } catch (Exception $e) {
             DB::rollback();
             Logger::log("Failed to heal permission group {$permission_group_id} (flag_id = " . $flag->getId() . ")\n" . $e->getTraceAsString());
             return false;
         }
         // delete flag
         $flag->delete();
         return true;
     }
 }
            if (is_numeric($rp_ot) && $rp_ot > 0 && $value == 0) {
                $root_permissions_sharing_table_delete[] = $rp_ot;
            }
            if (!is_numeric($rp_ot) || $rp_ot <= 0 || $value < 1) {
                continue;
            }
            $root_permissions_sharing_table_add[] = $rp_ot;
        }
    }
    $rp_info = array('root_permissions_sharing_table_delete' => $root_permissions_sharing_table_delete, 'root_permissions_sharing_table_add' => $root_permissions_sharing_table_add);
    // update sharing table
    DB::beginWork();
    $sharingTablecontroller = new SharingTableController();
    $sharingTablecontroller->afterPermissionChanged($pg_id, json_decode($permissions), $rp_info);
    // delete flag
    $flag->delete();
    DB::commit();
} catch (Exception $e) {
    DB::rollback();
    Logger::log("Error saving permissions (2): " . $e->getMessage() . "\n" . $e->getTraceAsString());
}
// save tree
try {
    DB::beginWork();
    $contactMemberCacheController = new ContactMemberCacheController();
    $group = PermissionGroups::findById($pg_id);
    $real_group = null;
    if ($group->getType() == 'user_groups') {
        $real_group = $group;
    }
    $users = $group->getUsers();