Skip to content

Commit cfebfec

Browse files
committed
fix: rename ABAC foreign keys
1 parent 456d048 commit cfebfec

16 files changed

Lines changed: 93 additions & 85 deletions

benchmarks/AbacEvaluationBench.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ public function setUpFlatAndScenario(): void
302302

303303
$this->rootChain = AbacChain::query()->create([
304304
'operator' => LogicalOperators::AND->value,
305-
'policy_id' => 'policy-1',
305+
'_policy' => 'policy-1',
306306
]);
307307

308308
$this->createCheck($this->rootChain->getKey(), ArithmeticOperators::EQUALS->value, 'resource.category', 'public');
@@ -318,7 +318,7 @@ public function setUpFlatAndStringScenario(): void
318318

319319
$this->rootChain = AbacChain::query()->create([
320320
'operator' => LogicalOperators::AND->value,
321-
'policy_id' => 'policy-1',
321+
'_policy' => 'policy-1',
322322
]);
323323

324324
$this->createCheck($this->rootChain->getKey(), StringOperators::STARTS_WITH->value, 'resource.category', 'pub');
@@ -335,22 +335,22 @@ public function setUpNestedOrScenario(): void
335335

336336
$this->rootChain = AbacChain::query()->create([
337337
'operator' => LogicalOperators::OR->value,
338-
'policy_id' => 'policy-1',
338+
'_policy' => 'policy-1',
339339
]);
340340

341341
$left = AbacChain::query()->create([
342342
'operator' => LogicalOperators::AND->value,
343-
'chain_id' => $this->rootChain->getKey(),
343+
'_chain' => $this->rootChain->getKey(),
344344
]);
345345

346346
$right = AbacChain::query()->create([
347347
'operator' => LogicalOperators::AND->value,
348-
'chain_id' => $this->rootChain->getKey(),
348+
'_chain' => $this->rootChain->getKey(),
349349
]);
350350

351351
$middle = AbacChain::query()->create([
352352
'operator' => LogicalOperators::AND->value,
353-
'chain_id' => $this->rootChain->getKey(),
353+
'_chain' => $this->rootChain->getKey(),
354354
]);
355355

356356
$this->createCheck($left->getKey(), ArithmeticOperators::EQUALS->value, 'resource.region', 'emea');
@@ -372,37 +372,37 @@ public function setUpNestedOrDeepScenario(): void
372372

373373
$this->rootChain = AbacChain::query()->create([
374374
'operator' => LogicalOperators::OR->value,
375-
'policy_id' => 'policy-1',
375+
'_policy' => 'policy-1',
376376
]);
377377

378378
$leftGroup = AbacChain::query()->create([
379379
'operator' => LogicalOperators::OR->value,
380-
'chain_id' => $this->rootChain->getKey(),
380+
'_chain' => $this->rootChain->getKey(),
381381
]);
382382

383383
$rightGroup = AbacChain::query()->create([
384384
'operator' => LogicalOperators::OR->value,
385-
'chain_id' => $this->rootChain->getKey(),
385+
'_chain' => $this->rootChain->getKey(),
386386
]);
387387

388388
$leftA = AbacChain::query()->create([
389389
'operator' => LogicalOperators::AND->value,
390-
'chain_id' => $leftGroup->getKey(),
390+
'_chain' => $leftGroup->getKey(),
391391
]);
392392

393393
$leftB = AbacChain::query()->create([
394394
'operator' => LogicalOperators::AND->value,
395-
'chain_id' => $leftGroup->getKey(),
395+
'_chain' => $leftGroup->getKey(),
396396
]);
397397

398398
$rightA = AbacChain::query()->create([
399399
'operator' => LogicalOperators::AND->value,
400-
'chain_id' => $rightGroup->getKey(),
400+
'_chain' => $rightGroup->getKey(),
401401
]);
402402

403403
$rightB = AbacChain::query()->create([
404404
'operator' => LogicalOperators::AND->value,
405-
'chain_id' => $rightGroup->getKey(),
405+
'_chain' => $rightGroup->getKey(),
406406
]);
407407

408408
$this->createCheck($leftA->getKey(), ArithmeticOperators::EQUALS->value, 'resource.region', 'emea');
@@ -425,7 +425,7 @@ public function setUpActorCheckScenario(): void
425425

426426
$this->rootChain = AbacChain::query()->create([
427427
'operator' => LogicalOperators::AND->value,
428-
'policy_id' => 'policy-1',
428+
'_policy' => 'policy-1',
429429
]);
430430

431431
$this->createCheck($this->rootChain->getKey(), ArithmeticOperators::EQUALS->value, 'resource.region', 'emea');
@@ -441,7 +441,7 @@ public function setUpActorCheckVariantScenario(): void
441441

442442
$this->rootChain = AbacChain::query()->create([
443443
'operator' => LogicalOperators::AND->value,
444-
'policy_id' => 'policy-1',
444+
'_policy' => 'policy-1',
445445
]);
446446

447447
$this->createCheck($this->rootChain->getKey(), ArithmeticOperators::EQUALS->value, 'resource.category', 'public');
@@ -541,13 +541,13 @@ protected function setUpComprehensiveScenario(PolicyMethod $method, int $postCou
541541

542542
$this->rootChain = AbacChain::query()->create([
543543
'operator' => LogicalOperators::OR->value,
544-
'policy_id' => 'policy-1',
544+
'_policy' => 'policy-1',
545545
]);
546546

547547
for ($i = 0; $i < $branchCount; $i++) {
548548
$group = AbacChain::query()->create([
549549
'operator' => $i % 2 === 0 ? LogicalOperators::AND->value : LogicalOperators::OR->value,
550-
'chain_id' => $this->rootChain->getKey(),
550+
'_chain' => $this->rootChain->getKey(),
551551
]);
552552

553553
$this->attachResourceArithmeticChecks($group->getKey(), $i);
@@ -612,14 +612,14 @@ private function bootstrapDatabase(): void
612612
$schema->create('abac_chains', function (Blueprint $table): void {
613613
$table->uuid('_id')->primary();
614614
$table->string('operator');
615-
$table->uuid('chain_id')->nullable();
616-
$table->uuid('policy_id')->nullable();
615+
$table->uuid('_chain')->nullable();
616+
$table->uuid('_policy')->nullable();
617617
$table->timestamps();
618618
});
619619

620620
$schema->create('abac_checks', function (Blueprint $table): void {
621621
$table->uuid('_id')->primary();
622-
$table->uuid('chain_id');
622+
$table->uuid('_chain');
623623
$table->string('operator');
624624
$table->string('key');
625625
$table->string('value');
@@ -732,7 +732,7 @@ protected function attachActorStringChecks(string $chainId, int $offset): void
732732
protected function createCheck(string $chainId, string $operator, string $key, string $value): void
733733
{
734734
AbacCheck::query()->create([
735-
'chain_id' => $chainId,
735+
'_chain' => $chainId,
736736
'operator' => $operator,
737737
'key' => $key,
738738
'value' => $value,

benchmarks/Concerns/AbacBenchmarkSupport.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ protected function setUpComprehensiveScenario(PolicyMethod $method, int $postCou
5959

6060
$this->rootChain = AbacChain::query()->create([
6161
'operator' => LogicalOperators::OR->value,
62-
'policy_id' => 'policy-1',
62+
'_policy' => 'policy-1',
6363
]);
6464

6565
for ($i = 0; $i < $branchCount; $i++) {
6666
$group = AbacChain::query()->create([
6767
'operator' => $i % 2 === 0 ? LogicalOperators::AND->value : LogicalOperators::OR->value,
68-
'chain_id' => $this->rootChain->getKey(),
68+
'_chain' => $this->rootChain->getKey(),
6969
]);
7070

7171
$this->attachResourceArithmeticChecks($group->getKey(), $i);
@@ -130,14 +130,14 @@ private function bootstrapDatabase(): void
130130
$schema->create('abac_chains', function (Blueprint $table): void {
131131
$table->uuid('_id')->primary();
132132
$table->string('operator');
133-
$table->uuid('chain_id')->nullable();
134-
$table->uuid('policy_id')->nullable();
133+
$table->uuid('_chain')->nullable();
134+
$table->uuid('_policy')->nullable();
135135
$table->timestamps();
136136
});
137137

138138
$schema->create('abac_checks', function (Blueprint $table): void {
139139
$table->uuid('_id')->primary();
140-
$table->uuid('chain_id');
140+
$table->uuid('_chain');
141141
$table->string('operator');
142142
$table->string('key');
143143
$table->string('value');
@@ -250,7 +250,7 @@ protected function attachActorStringChecks(string $chainId, int $offset): void
250250
protected function createCheck(string $chainId, string $operator, string $key, string $value): void
251251
{
252252
AbacCheck::query()->create([
253-
'chain_id' => $chainId,
253+
'_chain' => $chainId,
254254
'operator' => $operator,
255255
'key' => $key,
256256
'value' => $value,

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "zennit/abac",
33
"description": "Attribute-Based Access Control (ABAC) for Laravel",
4-
"version": "2.0.1",
4+
"version": "2.0.2",
55
"type": "library",
66
"license": "MIT",
77
"keywords": [

database/factories/AbacChainFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private function chainOrPolicy(): array
2222

2323
return [
2424
'operator' => $this->faker->randomElement(AllOperators::values()),
25-
$isChain ? 'chain_id' : 'policy_id' => $isChain
25+
$isChain ? '_chain' : '_policy' => $isChain
2626
? AbacChain::factory()->create()->getKey()
2727
: AbacPolicy::factory()->create()->getKey(),
2828
];

database/factories/AbacCheckFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function definition(): array
1616
{
1717
return [
1818
'operator' => $this->faker->randomElement(AllOperators::values([LogicalOperators::class])),
19-
'chain_id' => AbacChain::factory()->create()->getKey(),
19+
'_chain' => AbacChain::factory()->create()->getKey(),
2020
'key' => $this->faker->word,
2121
'value' => $this->faker->word,
2222
];

database/migrations/create_abac_tables.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public function up(): void
4141
$table->uuid('_id')->primary();
4242
$table->timestamps();
4343
$table->enum('operator', LogicalOperators::values());
44-
$table->foreignUuid('chain_id')->nullable()->constrained('abac_chains', '_id')->cascadeOnDelete();
45-
$table->foreignUuid('policy_id')->unique()->nullable()->constrained('abac_policies', '_id')->cascadeOnDelete();
44+
$table->foreignUuid('_chain')->nullable()->constrained('abac_chains', '_id')->cascadeOnDelete();
45+
$table->foreignUuid('_policy')->unique()->nullable()->constrained('abac_policies', '_id')->cascadeOnDelete();
4646
});
4747

4848
Schema::create('abac_checks', function (Blueprint $table) {
4949
$table->uuid('_id')->primary();
5050
$table->timestamps();
51-
$table->foreignUuid('chain_id')->constrained('abac_chains', '_id')->cascadeOnDelete();
51+
$table->foreignUuid('_chain')->constrained('abac_chains', '_id')->cascadeOnDelete();
5252
$table->enum('operator', AllOperators::values([LogicalOperators::class]));
5353
$table->string('key');
5454
$table->string('value');

src/Logging/AbacAuditLogger.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function logPolicyMiss(AccessContext $context): void
4242
);
4343
}
4444

45-
public function logChainOutcome(AccessContext $context, bool $allowed, ?string $policyId, ?string $chainId, ?string $reason = null): void
45+
public function logChainOutcome(AccessContext $context, bool $allowed, ?string $policyKey, ?string $chainKey, ?string $reason = null): void
4646
{
4747
$level = $allowed ? 'info' : 'warning';
4848

@@ -51,8 +51,8 @@ public function logChainOutcome(AccessContext $context, bool $allowed, ?string $
5151
$this->buildContext($context) + [
5252
'event' => 'abac.chain_outcome',
5353
'allowed' => $allowed,
54-
'policy_id' => $policyId,
55-
'chain_id' => $chainId,
54+
'_policy' => $policyKey,
55+
'_chain' => $chainKey,
5656
'reason' => $reason,
5757
]
5858
);

src/Models/AbacChain.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
/**
1515
* @property string $_id
1616
* @property string $operator
17-
* @property string|null $chain_id
18-
* @property string|null $policy_id
17+
* @property string|null $_chain
18+
* @property string|null $_policy
1919
*/
2020
class AbacChain extends Model
2121
{
@@ -34,26 +34,26 @@ class AbacChain extends Model
3434

3535
protected $fillable = [
3636
'operator',
37-
'chain_id',
38-
'policy_id',
37+
'_chain',
38+
'_policy',
3939
];
4040

4141
protected $casts = [
4242
'_id' => 'string',
4343
'operator' => 'string',
44-
'chain_id' => 'string',
45-
'policy_id' => 'string',
44+
'_chain' => 'string',
45+
'_policy' => 'string',
4646
];
4747

4848
protected static function booted(): void
4949
{
5050
static::creating(function (AbacChain $chain) {
51-
if ($chain->isDirty('chain_id') && $chain->isDirty('policy_id')) {
52-
throw new Exception('You can not set both chain_id and policy_id at the same time');
51+
if ($chain->isDirty('_chain') && $chain->isDirty('_policy')) {
52+
throw new Exception('You can not set both _chain and _policy at the same time');
5353
}
5454

55-
if (! $chain->chain_id && ! $chain->policy_id) {
56-
throw new Exception('You must set either chain_id or policy_id');
55+
if (! $chain->_chain && ! $chain->_policy) {
56+
throw new Exception('You must set either _chain or _policy');
5757
}
5858
});
5959

@@ -65,14 +65,22 @@ protected static function booted(): void
6565
*/
6666
public function chain(): BelongsTo
6767
{
68-
return $this->belongsTo(AbacChain::class, 'chain_id');
68+
return $this->belongsTo(AbacChain::class, '_chain');
69+
}
70+
71+
/**
72+
* @return BelongsTo<AbacPolicy, $this>
73+
*/
74+
public function policy(): BelongsTo
75+
{
76+
return $this->belongsTo(AbacPolicy::class, '_policy');
6977
}
7078

7179
/**
7280
* @return HasMany<AbacCheck, $this>
7381
*/
7482
public function checks(): HasMany
7583
{
76-
return $this->hasMany(AbacCheck::class, 'chain_id');
84+
return $this->hasMany(AbacCheck::class, '_chain');
7785
}
7886
}

src/Models/AbacCheck.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
/**
1212
* @property string $_id
13-
* @property string $chain_id
13+
* @property string $_chain
1414
* @property string $operator
1515
* @property string $key
1616
* @property string $value
@@ -31,15 +31,15 @@ class AbacCheck extends Model
3131
protected $keyType = 'string';
3232

3333
protected $fillable = [
34-
'chain_id',
34+
'_chain',
3535
'operator',
3636
'key',
3737
'value',
3838
];
3939

4040
protected $casts = [
4141
'_id' => 'string',
42-
'chain_id' => 'string',
42+
'_chain' => 'string',
4343
'operator' => 'string',
4444
'key' => 'string',
4545
'value' => 'string',

src/Models/AbacPolicy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ protected static function booted(): void
5050
*/
5151
public function chains(): HasMany
5252
{
53-
return $this->hasMany(AbacChain::class, 'policy_id');
53+
return $this->hasMany(AbacChain::class, '_policy');
5454
}
5555
}

0 commit comments

Comments
 (0)