Skip to content

Commit 22a8368

Browse files
authored
Merge pull request #9 from adhenrique/develop
changing the way that Policies is registered and removing parent policy
2 parents ab587bb + 7063eae commit 22a8368

File tree

7 files changed

+80
-54
lines changed

7 files changed

+80
-54
lines changed

src/Controller/Controller.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ public function response(array $data = [], $status = 200): JsonResponse
3030

3131
public function index(Request $request)
3232
{
33-
$this->authorize('index', $this->searchService->getModel());
33+
$this->authorize('index', $this->searchService->getTableName());
3434

3535
$data = $this->searchService->all($request);
3636
return $this->resource::collection($data);
3737
}
3838

3939
public function show(Request $request, int $id)
4040
{
41-
$this->authorize('show', $this->searchService->getModel());
41+
$this->authorize('show', $this->searchService->getTableName());
4242

4343
$request = $request->merge(['id' => $id]);
4444
$validatedData = $this->validateService->handle($request->all(), ValidateService::SHOW);
@@ -49,7 +49,7 @@ public function show(Request $request, int $id)
4949

5050
public function store(Request $request): JsonResponse
5151
{
52-
$this->authorize('store', $this->searchService->getModel());
52+
$this->authorize('store', $this->persistenceService->getTableName());
5353

5454
$validatedData = $this->validateService->handle($request->all(), ValidateService::STORE);
5555
$id = $this->persistenceService->store($validatedData);
@@ -58,7 +58,7 @@ public function store(Request $request): JsonResponse
5858

5959
public function update(Request $request, $id): JsonResponse
6060
{
61-
$this->authorize('update', $this->searchService->getModel());
61+
$this->authorize('update', $this->persistenceService->getTableName());
6262

6363
$request = $request->merge(['id' => $id]);
6464
$validatedData = $this->validateService->handle($request->all(), ValidateService::UPDATE);
@@ -69,7 +69,7 @@ public function update(Request $request, $id): JsonResponse
6969

7070
public function destroy(Request $request, int $id): JsonResponse
7171
{
72-
$this->authorize('destroy', $this->searchService->getModel());
72+
$this->authorize('destroy', $this->persistenceService->getTableName());
7373

7474
$request = $request->merge(['id' => $id]);
7575
$validatedData = $this->validateService->handle($request->all(), ValidateService::DESTROY);

src/Policy/Policy.php

-42
This file was deleted.

src/ServiceProvider.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\File;
66
use Illuminate\Support\Facades\Gate;
77
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
8+
use Illuminate\Support\Str;
89
use LaravelDomainOriented\Commands\CreateDomain;
910
use LaravelDomainOriented\Commands\RemoveDomain;
1011

@@ -36,8 +37,11 @@ private function registerPolicies()
3637
$domainNames = require (app_path('domains.php'));
3738

3839
foreach ($domainNames as $domainName) {
39-
$policy = 'App\\Domain\\'.$domainName.'\\'.$domainName.'Policy';
40-
Gate::policy('Illuminate\Database\Eloquent\Model', $policy);
40+
$namespace = 'App\\Domain\\'.$domainName;
41+
$policy = $namespace.'\\'.$domainName.'Policy';
42+
$tableName = Str::snake(Str::pluralStudly($domainName));
43+
44+
Gate::policy($tableName, $policy);
4145
}
4246
}
4347

src/Services/PersistenceService.php

+5
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public function destroy(int $id): bool
3737
$model->delete();
3838
return true;
3939
}
40+
41+
public function getTableName(): string
42+
{
43+
return $this->model->getTable();
44+
}
4045
}

src/Services/SearchService.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function findById(int $id)
4444
return $builder->findOrFail($id);
4545
}
4646

47-
public function getModel(): SearchModel
47+
public function getTableName(): string
4848
{
49-
return $this->model;
49+
return $this->model->getTable();
5050
}
5151

5252
public function beforeSearch(Builder $builder, Guard $auth): Builder

src/Stubs/Policy.stub

+29-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,35 @@
22

33
namespace App\Domain\{{singularName}};
44

5-
use LaravelDomainOriented\Policy\Policy;
5+
use Illuminate\Auth\Access\HandlesAuthorization;
6+
use Illuminate\Contracts\Auth\Authenticatable as AuthUser;
67

7-
class {{singularName}}Policy extends Policy
8+
class {{singularName}}Policy
89
{
9-
//
10+
use HandlesAuthorization;
11+
12+
public function index(): bool
13+
{
14+
return true;
15+
}
16+
17+
public function show(): bool
18+
{
19+
return true;
20+
}
21+
22+
public function store(): bool
23+
{
24+
return true;
25+
}
26+
27+
public function update(): bool
28+
{
29+
return true;
30+
}
31+
32+
public function destroy(): bool
33+
{
34+
return true;
35+
}
1036
}

tests/Feature/ControllerTest.php

+33
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests\Feature;
44

5+
use Illuminate\Foundation\Auth\User;
56
use Illuminate\Routing\Router;
67
use LaravelDomainOriented\Tests\Cases\DBTestCase;
78

@@ -24,6 +25,7 @@ public function setUp(): void
2425
/** @test **/
2526
public function it_should_call_list_route_and_assert_count_of_items()
2627
{
28+
$this->loginWithFakeUser();
2729
$response = $this->getJson('tests');
2830
$response->assertOk();
2931

@@ -32,9 +34,17 @@ public function it_should_call_list_route_and_assert_count_of_items()
3234
$this->assertCount(count($this->data), $data['data']);
3335
}
3436

37+
/** @test **/
38+
public function it_should_try_call_list_route_without_login_and_get_403()
39+
{
40+
$response = $this->getJson('tests');
41+
$response->assertForbidden();
42+
}
43+
3544
/** @test **/
3645
public function it_should_call_find_route_and_assert_item()
3746
{
47+
$this->loginWithFakeUser();
3848
$response = $this->getJson('tests/1');
3949
$response->assertOk();
4050

@@ -46,13 +56,15 @@ public function it_should_call_find_route_and_assert_item()
4656
/** @test **/
4757
public function it_should_call_find_route_with_non_existent_id_and_assert_status_404()
4858
{
59+
$this->loginWithFakeUser();
4960
$response = $this->getJson('tests/15');
5061
$response->assertStatus(404);
5162
}
5263

5364
/** @test **/
5465
public function it_should_create_a_item()
5566
{
67+
$this->loginWithFakeUser();
5668
$response = $this->postJson('tests', [
5769
'name' => 'XXX'
5870
]);
@@ -66,6 +78,7 @@ public function it_should_create_a_item()
6678
/** @test **/
6779
public function it_should_try_create_a_item_and_assert_status_422()
6880
{
81+
$this->loginWithFakeUser();
6982
$response = $this->postJson('tests', [
7083
'name' => 1
7184
]);
@@ -75,6 +88,7 @@ public function it_should_try_create_a_item_and_assert_status_422()
7588
/** @test **/
7689
public function it_should_update_a_item()
7790
{
91+
$this->loginWithFakeUser();
7892
$updateName = 'XXX';
7993
$response = $this->putJson('tests/1', [
8094
'name' => $updateName
@@ -89,6 +103,7 @@ public function it_should_update_a_item()
89103
/** @test **/
90104
public function it_should_try_update_a_item_and_assert_status_422()
91105
{
106+
$this->loginWithFakeUser();
92107
$response = $this->putJson('tests/1', [
93108
'name' => 1
94109
]);
@@ -98,6 +113,7 @@ public function it_should_try_update_a_item_and_assert_status_422()
98113
/** @test **/
99114
public function it_should_delete_a_item()
100115
{
116+
$this->loginWithFakeUser();
101117
$this->withoutMiddleware();
102118
$response = $this->deleteJson('tests/1');
103119
$response->assertOk();
@@ -107,4 +123,21 @@ public function it_should_delete_a_item()
107123
$this->assertTrue($data['data']['isDeleted']);
108124
$this->assertSoftDeleted('tests');
109125
}
126+
127+
private function loginWithFakeUser()
128+
{
129+
$user = new MyUserModel([
130+
'id' => 1,
131+
'name' => 'test user',
132+
]);
133+
134+
$this->be($user);
135+
}
136+
}
137+
138+
class MyUserModel extends User {
139+
protected $fillable = [
140+
'id',
141+
'name',
142+
];
110143
}

0 commit comments

Comments
 (0)