1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 02:54:03 +00:00

Update key names of the formatted constraints

This commit is contained in:
lukinovec 2025-07-02 17:53:15 +02:00
parent 7ae11dca7f
commit 1cfb85e756
2 changed files with 66 additions and 66 deletions

View file

@ -43,14 +43,14 @@ use Stancl\Tenancy\RLS\Exceptions\RLSCommentConstraintException;
* // 'foo_table' => [...$stepsLeadingToTenantsTable], * // 'foo_table' => [...$stepsLeadingToTenantsTable],
* // 'bar_table' => [ * // 'bar_table' => [
* // [ * // [
* // 'foreignKey' => 'post_id', * // 'localColumn' => 'post_id',
* // 'foreignTable' => 'posts', * // 'foreignTable' => 'posts',
* // 'foreignId' => 'id' * // 'foreignColumn' => 'id'
* // ], * // ],
* // [ * // [
* // 'foreignKey' => 'tenant_id', * // 'localColumn' => 'tenant_id',
* // 'foreignTable' => 'tenants', * // 'foreignTable' => 'tenants',
* // 'foreignId' => 'id' * // 'foreignColumn' => 'id'
* // ], * // ],
* // ], * // ],
* // This is used in the CreateUserWithRLSPolicies command. * // This is used in the CreateUserWithRLSPolicies command.
@ -103,21 +103,21 @@ class TableRLSManager implements RLSPolicyManager
* *
* 'posts' => [ * 'posts' => [
* [ * [
* 'foreignKey' => 'tenant_id', * 'localColumn' => 'tenant_id',
* 'foreignTable' => 'tenants', * 'foreignTable' => 'tenants',
* 'foreignId' => 'id' * 'foreignColumn' => 'id'
* ], * ],
* ], * ],
* 'comments' => [ * 'comments' => [
* [ * [
* 'foreignKey' => 'post_id', * 'localColumn' => 'post_id',
* 'foreignTable' => 'posts', * 'foreignTable' => 'posts',
* 'foreignId' => 'id' * 'foreignColumn' => 'id'
* ], * ],
* [ * [
* 'foreignKey' => 'tenant_id', * 'localColumn' => 'tenant_id',
* 'foreignTable' => 'tenants', * 'foreignTable' => 'tenants',
* 'foreignId' => 'id' * 'foreignColumn' => 'id'
* ], * ],
* ], * ],
* *
@ -136,9 +136,9 @@ class TableRLSManager implements RLSPolicyManager
if ($this->isValidPath($shortestPath)) { if ($this->isValidPath($shortestPath)) {
// Format path steps to a more readable format (keep only the needed data) // Format path steps to a more readable format (keep only the needed data)
$shortestPaths[$tableName] = array_map(fn (array $step) => [ $shortestPaths[$tableName] = array_map(fn (array $step) => [
'foreignKey' => $step['foreignKey'], 'localColumn' => $step['localColumn'],
'foreignTable' => $step['foreignTable'], 'foreignTable' => $step['foreignTable'],
'foreignId' => $step['foreignId'], 'foreignColumn' => $step['foreignColumn'],
], $shortestPath['steps']); ], $shortestPath['steps']);
} }
@ -187,9 +187,9 @@ class TableRLSManager implements RLSPolicyManager
* before returning the shortest paths in shortestPath(). * before returning the shortest paths in shortestPath().
* *
* [ * [
* 'foreignKey' => 'tenant_id', * 'localColumn' => 'tenant_id',
* 'foreignTable' => 'tenants', * 'foreignTable' => 'tenants',
* 'foreignId' => 'id', * 'foreignColumn' => 'id',
* 'comment' => 'no-rls', // Used to explicitly enable/disable RLS or to create a comment constraint (internal metadata) * 'comment' => 'no-rls', // Used to explicitly enable/disable RLS or to create a comment constraint (internal metadata)
* 'nullable' => false, // Used to determine if the constraint is nullable (internal metadata) * 'nullable' => false, // Used to determine if the constraint is nullable (internal metadata)
* ]. * ].
@ -229,9 +229,9 @@ class TableRLSManager implements RLSPolicyManager
bool $nullable bool $nullable
): array { ): array {
return [ return [
'foreignKey' => $foreignKey, 'localColumn' => $foreignKey,
'foreignTable' => $foreignTable, 'foreignTable' => $foreignTable,
'foreignId' => $foreignId, 'foreignColumn' => $foreignId,
// Internal metadata omitted in shortestPaths() // Internal metadata omitted in shortestPaths()
'comment' => $comment, 'comment' => $comment,
'nullable' => $nullable, 'nullable' => $nullable,
@ -362,9 +362,9 @@ class TableRLSManager implements RLSPolicyManager
* and that method uses formatConstraint(), which serves as a single source of truth * and that method uses formatConstraint(), which serves as a single source of truth
* for our constraint formatting. A formatted constraint looks like this: * for our constraint formatting. A formatted constraint looks like this:
* [ * [
* 'foreignKey' => 'tenant_id', * 'localColumn' => 'tenant_id',
* 'foreignTable' => 'tenants', * 'foreignTable' => 'tenants',
* 'foreignId' => 'id', * 'foreignColumn' => 'id',
* 'comment' => 'no-rls', * 'comment' => 'no-rls',
* 'nullable' => false * 'nullable' => false
* ] * ]
@ -505,9 +505,9 @@ class TableRLSManager implements RLSPolicyManager
$sessionTenantKey = config('tenancy.rls.session_variable_name'); $sessionTenantKey = config('tenancy.rls.session_variable_name');
foreach ($path as $index => $relation) { foreach ($path as $index => $relation) {
$column = $relation['foreignKey']; $column = $relation['localColumn'];
$table = $relation['foreignTable']; $table = $relation['foreignTable'];
$foreignKey = $relation['foreignId']; $foreignKey = $relation['foreignColumn'];
$indentation = str_repeat(' ', ($index + 1) * 4); $indentation = str_repeat(' ', ($index + 1) * 4);

View file

@ -361,38 +361,38 @@ test('table rls manager generates shortest paths that lead to the tenants table
$expectedShortestPaths = [ $expectedShortestPaths = [
'authors' => [ 'authors' => [
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
'posts' => [ 'posts' => [
[ [
'foreignKey' => 'author_id', 'localColumn' => 'author_id',
'foreignTable' => 'authors', 'foreignTable' => 'authors',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
'comments' => [ 'comments' => [
[ [
'foreignKey' => 'post_id', 'localColumn' => 'post_id',
'foreignTable' => 'posts', 'foreignTable' => 'posts',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'author_id', 'localColumn' => 'author_id',
'foreignTable' => 'authors', 'foreignTable' => 'authors',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
// When scoping by default is enabled (implicit scoping), // When scoping by default is enabled (implicit scoping),
@ -401,25 +401,25 @@ test('table rls manager generates shortest paths that lead to the tenants table
// the shortest path leads through post_id. // the shortest path leads through post_id.
'ratings' => $scopeByDefault ? [ 'ratings' => $scopeByDefault ? [
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
] : [ ] : [
[ [
'foreignKey' => 'post_id', 'localColumn' => 'post_id',
'foreignTable' => 'posts', 'foreignTable' => 'posts',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'author_id', 'localColumn' => 'author_id',
'foreignTable' => 'authors', 'foreignTable' => 'authors',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
// Articles table is ignored because it's not related to the tenant table in any way // Articles table is ignored because it's not related to the tenant table in any way
@ -443,28 +443,28 @@ test('table rls manager generates shortest paths that lead to the tenants table
// Non-nullable paths are preferred over nullable paths // Non-nullable paths are preferred over nullable paths
$expectedShortestPaths['ratings'] = [ $expectedShortestPaths['ratings'] = [
[ [
'foreignKey' => 'comment_id', 'localColumn' => 'comment_id',
'foreignTable' => 'comments', 'foreignTable' => 'comments',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'post_id', 'localColumn' => 'post_id',
'foreignTable' => 'posts', 'foreignTable' => 'posts',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
// Importantly, the best path goes through authors // Importantly, the best path goes through authors
// since ratings -> posts is nullable, as well as // since ratings -> posts is nullable, as well as
// posts -> tenants directly (without going through // posts -> tenants directly (without going through
// authors first). // authors first).
'foreignKey' => 'author_id', 'localColumn' => 'author_id',
'foreignTable' => 'authors', 'foreignTable' => 'authors',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
]; ];
@ -613,38 +613,38 @@ test('table rls manager generates queries correctly', function() {
$paths = [ $paths = [
'primaries' => [ 'primaries' => [
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
'secondaries' => [ 'secondaries' => [
[ [
'foreignKey' => 'primary_id', 'localColumn' => 'primary_id',
'foreignTable' => 'primaries', 'foreignTable' => 'primaries',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
'foo' => [ 'foo' => [
[ [
'foreignKey' => 'secondary_id', 'localColumn' => 'secondary_id',
'foreignTable' => 'secondaries', 'foreignTable' => 'secondaries',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'primary_id', 'localColumn' => 'primary_id',
'foreignTable' => 'primaries', 'foreignTable' => 'primaries',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
]; ];
@ -782,21 +782,21 @@ test('table manager can generate paths leading through comment constraint column
$expectedPaths = [ $expectedPaths = [
'non_constrained_posts' => [ 'non_constrained_posts' => [
[ [
'foreignKey' => 'author_id', 'localColumn' => 'author_id',
'foreignTable' => 'non_constrained_users', 'foreignTable' => 'non_constrained_users',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
'non_constrained_users' => [ 'non_constrained_users' => [
[ [
'foreignKey' => 'tenant_id', 'localColumn' => 'tenant_id',
'foreignTable' => 'tenants', 'foreignTable' => 'tenants',
'foreignId' => 'id', 'foreignColumn' => 'id',
], ],
], ],
]; ];