mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 05:24:03 +00:00
Postgres RLS + permission controlled database managers (#33)
This PR adds Postgres RLS (trait manager + table manager approach) and permission controlled managers for PostgreSQL. --------- Co-authored-by: lukinovec <lukinovec@gmail.com> Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
This commit is contained in:
parent
34297d3e1a
commit
7317d2638a
39 changed files with 2511 additions and 112 deletions
34
src/Concerns/ManagesRLSPolicies.php
Normal file
34
src/Concerns/ManagesRLSPolicies.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Concerns;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Used for easily dropping RLS policies on tables, primarily in migrations.
|
||||
*/
|
||||
trait ManagesRLSPolicies
|
||||
{
|
||||
/** @return string[] */
|
||||
public static function getRLSPolicies(string $table): array
|
||||
{
|
||||
return array_map(
|
||||
fn (stdClass $policy) => $policy->policyname,
|
||||
DB::select("SELECT policyname FROM pg_policies WHERE tablename = '{$table}' AND policyname LIKE '%_rls_policy%'")
|
||||
);
|
||||
}
|
||||
|
||||
public static function dropRLSPolicies(string $table): int
|
||||
{
|
||||
$policies = static::getRLSPolicies($table);
|
||||
|
||||
foreach ($policies as $policy) {
|
||||
DB::statement('DROP POLICY ? ON ?', [$policy, $table]);
|
||||
}
|
||||
|
||||
return count($policies);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue