Skip to content

Commit

Permalink
Improve QueriesExecute SQL parsing for joining on orgs and limitng us…
Browse files Browse the repository at this point in the history
…er results.
  • Loading branch information
mark-unwin committed Feb 5, 2024
1 parent 5e624f4 commit 6c91f9e
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions app/Models/QueriesModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,14 @@ public function execute(int $id = 0, object $user = null): array
$filter = "devices.org_id IN ({$user->org_list})";
$type = 'devices';
} else {
$words = explode(' ', $sql);
$tables = explode('.', $words[1]);
$filter = $tables[0] . ".org_id IN ({$user->org_list})";
$type = $tables[0];
$split1 = explode('.', $sql);
$split2 = explode(' ', $split1[0]);
$split3 = preg_split("/[^a-z_]/i", end($split2));
$filter = end($split3) . ".org_id IN ({$user->org_list})";
if (end($split3) === 'orgs') {
$filter = end($split3) . ".id IN ({$user->org_list})";
}
$type = end($split3);
}
$sql = str_ireplace('WHERE @filter', "WHERE {$filter}", $sql);
$sql .= ' LIMIT ' . $instance->resp->meta->limit;
Expand Down

0 comments on commit 6c91f9e

Please sign in to comment.