Skip to content

Commit feb626a

Browse files
committed
adding Policy stub file
1 parent a92c3f8 commit feb626a

File tree

9 files changed

+87
-1
lines changed

9 files changed

+87
-1
lines changed

src/Builder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Builder
2121
'Migration' => 'database/migrations/',
2222
'Factory' => 'database/factories/',
2323
'Seeder' => 'database/seeders/',
24+
'Policy' => 'app/Domain/%s/',
2425
];
2526
private Collection $names;
2627

src/Controller/Controller.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace LaravelDomainOriented\Controller;
44

5+
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6+
use Illuminate\Foundation\Bus\DispatchesJobs;
57
use Illuminate\Http\JsonResponse;
68
use Illuminate\Http\Request;
79
use Illuminate\Routing\Controller as BaseController;
@@ -11,6 +13,8 @@
1113

1214
class Controller extends BaseController
1315
{
16+
use AuthorizesRequests, DispatchesJobs;
17+
1418
protected SearchService $searchService;
1519

1620
protected PersistenceService $persistenceService;
@@ -26,12 +30,16 @@ public function response(array $data = [], $status = 200): JsonResponse
2630

2731
public function index(Request $request)
2832
{
33+
$this->authorize('index', $this->searchService->getModel());
34+
2935
$data = $this->searchService->all($request);
3036
return $this->resource::collection($data);
3137
}
3238

3339
public function show(Request $request, int $id)
3440
{
41+
$this->authorize('show', $this->searchService->getModel());
42+
3543
$request = $request->merge(['id' => $id]);
3644
$validatedData = $this->validateService->handle($request->all(), ValidateService::SHOW);
3745

@@ -41,13 +49,17 @@ public function show(Request $request, int $id)
4149

4250
public function store(Request $request): JsonResponse
4351
{
52+
$this->authorize('store', $this->searchService->getModel());
53+
4454
$validatedData = $this->validateService->handle($request->all(), ValidateService::STORE);
4555
$id = $this->persistenceService->store($validatedData);
4656
return $this->response(['id' => $id]);
4757
}
4858

4959
public function update(Request $request, $id): JsonResponse
5060
{
61+
$this->authorize('update', $this->searchService->getModel());
62+
5163
$request = $request->merge(['id' => $id]);
5264
$validatedData = $this->validateService->handle($request->all(), ValidateService::UPDATE);
5365

@@ -57,6 +69,8 @@ public function update(Request $request, $id): JsonResponse
5769

5870
public function destroy(Request $request, int $id): JsonResponse
5971
{
72+
$this->authorize('destroy', $this->searchService->getModel());
73+
6074
$request = $request->merge(['id' => $id]);
6175
$validatedData = $this->validateService->handle($request->all(), ValidateService::DESTROY);
6276

src/Policy/Policy.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace LaravelDomainOriented\Policy;
4+
5+
use Illuminate\Auth\Access\HandlesAuthorization;
6+
use Illuminate\Contracts\Auth\Authenticatable as AuthUser;
7+
8+
class Policy
9+
{
10+
use HandlesAuthorization;
11+
12+
public function before(?AuthUser $user, string $ability): bool
13+
{
14+
return true;
15+
}
16+
17+
public function index(AuthUser $user): bool
18+
{
19+
return true;
20+
}
21+
22+
public function show(AuthUser $user): bool
23+
{
24+
return true;
25+
}
26+
27+
public function store(AuthUser $user): bool
28+
{
29+
return true;
30+
}
31+
32+
public function update(AuthUser $user): bool
33+
{
34+
return true;
35+
}
36+
37+
public function destroy(AuthUser $user): bool
38+
{
39+
return true;
40+
}
41+
}
42+

src/ServiceProvider.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LaravelDomainOriented;
44

5+
use Illuminate\Support\Facades\Gate;
56
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
67
use LaravelDomainOriented\Commands\CreateDomain;
78
use LaravelDomainOriented\Commands\RemoveDomain;
@@ -23,6 +24,16 @@ public function boot()
2324

2425
public function register()
2526
{
26-
//
27+
$this->registerPolicies();
28+
}
29+
30+
private function registerPolicies()
31+
{
32+
$domainNames = require (app_path('domains.php'));
33+
34+
foreach ($domainNames as $domainName) {
35+
$policy = 'App\\Domain\\'.$domainName.'\\'.$domainName.'Policy';
36+
Gate::policy('Illuminate\Database\Eloquent\Model', $policy);
37+
}
2738
}
2839
}

src/Services/SearchService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ public function findById(int $id)
3636
{
3737
return $this->model->findOrFail($id);
3838
}
39+
40+
public function getModel(): SearchModel
41+
{
42+
return $this->model;
43+
}
3944
}

src/Stubs/Policy.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Domain\{{singularName}};
4+
5+
use LaravelDomainOriented\Policy\Policy;
6+
7+
class {{singularName}}Policy extends Policy
8+
{
9+
//
10+
}

tests/Feature/CreateDomainTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CreateDomainTest extends BasicTestCase
2828
'Migration' => 'database/migrations/',
2929
'Factory' => 'database/factories/',
3030
'Seeder' => 'database/seeders/',
31+
'Policy' => 'app/Domain/%s/',
3132
];
3233
private string $domainName = 'Test';
3334
private string $tableName = '';

tests/Feature/RemoveDomainTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class RemoveDomainTest extends BasicTestCase
2828
'Migration' => 'database/migrations/',
2929
'Factory' => 'database/factories/',
3030
'Seeder' => 'database/seeders/',
31+
'Policy' => 'app/Domain/%s/',
3132
];
3233

3334
public function setUp(): void

tests/Unit/BuilderTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BuilderTest extends BasicTestCase
2323
'Migration' => 'database/migrations/',
2424
'Factory' => 'database/factories/',
2525
'Seeder' => 'database/seeders/',
26+
'Policy' => 'app/Domain/%s/',
2627
];
2728

2829
public function setUp(): void

0 commit comments

Comments
 (0)