Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] ACL Proof of concept #9

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion app/Models/ClientApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Models\Traits\ArnDefaultsTrait;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
Expand All @@ -12,17 +13,20 @@
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Laravel\Sanctum\HasApiTokens;
use RenokiCo\Acl\Concerns\HasPolicies;
use RenokiCo\Acl\Contracts\RuledByPolicies;

/**
* @mixin IdeHelperClientApplication
*/
class ClientApplication extends Model implements AuthorizableContract, AuthenticatableContract
class ClientApplication extends Model implements AuthorizableContract, AuthenticatableContract, RuledByPolicies
{
use HasFactory;
use HasApiTokens;
use Authorizable;
use HasUlidField;
use Authenticatable;
use HasPolicies;

public function getRememberTokenName()
{
Expand Down Expand Up @@ -52,4 +56,14 @@ public function Jobs(): HasMany
{
return $this->hasMany(PrintJob::class, 'client_application_id');
}

public function resolveArnAccountId()
{
return $this->Team->ulid;
}

public function resolveArnRegion()
{
return 'local';
}
}
22 changes: 21 additions & 1 deletion app/Models/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

namespace App\Models;

use App\Models\Traits\ArnDefaultsTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use RenokiCo\Acl\Concerns\HasArn;
use RenokiCo\Acl\Concerns\HasStaticArn;
use RenokiCo\Acl\Contracts\Arnable;

/**
* @mixin IdeHelperPrinter
*/
class Printer extends Model
class Printer extends Model implements Arnable
{
use HasArn, ArnDefaultsTrait {
ArnDefaultsTrait::arnPartition insteadof HasArn;
ArnDefaultsTrait::arnService insteadof HasArn;
ArnDefaultsTrait::arnRegion insteadof HasArn;
}

use HasFactory;
use HasUlidField;

Expand Down Expand Up @@ -61,4 +71,14 @@ public function scopeForType(Builder $query, string $type)
->orWhereJsonContains('raw_languages_supported', '*');
});
}

public function arnResourceAccountId()
{
return $this->Server->Team->ulid;
}

public function arnResourceId()
{
return $this->ulid;
}
}
41 changes: 41 additions & 0 deletions app/Models/Traits/ArnDefaultsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Models\Traits;

trait ArnDefaultsTrait
{
/**
* This is the partition used for this application.
* It is a good practice to change this between projects.
* Can be treated as a namespace, unique for each of your apps.
*
* @return string|int
*/
public static function arnPartition()
{
return 'webprint';
}

/**
* This is the service used under the application. You can group
* multiple regions with accounts and resources under a single service.
*
* @return string|int
*/
public static function arnService()
{
return 'server';
}

/**
* If your application is globally distributed, change this
* field each time to differentiate between services belonging
* to other regions.
*
* @return string|int
*/
public static function arnRegion()
{
return strtolower(config('app.env'));
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"league/uri": "^6.8",
"livewire/livewire": "^2.5",
"monicahq/laravel-cloudflare": "^3.3",
"renoki-co/acl": "^0.4.0",
"spatie/laravel-settings": "^2.6"
},
"require-dev": {
Expand Down
67 changes: 66 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions tests/Feature/AclPlaygroundTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Tests\Feature;

use App\Models\ClientApplication;
use App\Models\Printer;
use App\Models\PrintServer;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Laravel\Sanctum\Sanctum;
use RenokiCo\Acl\Acl;
use RenokiCo\Acl\Statement;
use Tests\TestCase;

class AclPlaygroundTest extends TestCase
{
use RefreshDatabase;

/**
* A basic feature test example.
*
* @return void
*/
public function test_example()
{
/** @var ClientApplication $client */
$client = ClientApplication::factory()->create();

$server = PrintServer::factory()
->recycle($client->Team)
->create();

$printers = Printer::factory()
->active()
->for($server, 'Server')
->count(3)
->create();

$policy = Acl::createPolicy([
Statement::make(
effect: 'Allow',
action: 'printer:Print',
resource: [
'arn:webprint:server:testing:'.$client->Team->ulid.':printer/*',
],
),
Statement::make(
effect: 'Deny',
action: 'printer:Print',
resource: [
'arn:webprint:server:testing:'.$client->Team->ulid.':printer/'.$printers[1]->ulid,
],
),
]);

// dd(
// Printer::resourceIdAgnosticArn($client),
// json_encode($policy->toArray(), JSON_PRETTY_PRINT),
// );

$client->loadPolicies($policy);

$this->assertTrue($client->isAllowedTo('printer:Print', $printers[0]));
$this->assertFalse($client->isAllowedTo('printer:Print', $printers[1]));
$this->assertTrue($client->isAllowedTo('printer:Print', $printers[2]));
}
}