diff --git a/src/Controller/Controller.php b/src/Controller/Controller.php index 078fdb5..e1fc13a 100644 --- a/src/Controller/Controller.php +++ b/src/Controller/Controller.php @@ -30,7 +30,7 @@ public function response(array $data = [], $status = 200): JsonResponse public function index(Request $request) { - $this->authorize('index', $this->searchService->getModel()); + $this->authorize('index', $this->searchService->getTableName()); $data = $this->searchService->all($request); return $this->resource::collection($data); @@ -38,7 +38,7 @@ public function index(Request $request) public function show(Request $request, int $id) { - $this->authorize('show', $this->searchService->getModel()); + $this->authorize('show', $this->searchService->getTableName()); $request = $request->merge(['id' => $id]); $validatedData = $this->validateService->handle($request->all(), ValidateService::SHOW); @@ -49,7 +49,7 @@ public function show(Request $request, int $id) public function store(Request $request): JsonResponse { - $this->authorize('store', $this->searchService->getModel()); + $this->authorize('store', $this->persistenceService->getTableName()); $validatedData = $this->validateService->handle($request->all(), ValidateService::STORE); $id = $this->persistenceService->store($validatedData); @@ -58,7 +58,7 @@ public function store(Request $request): JsonResponse public function update(Request $request, $id): JsonResponse { - $this->authorize('update', $this->searchService->getModel()); + $this->authorize('update', $this->persistenceService->getTableName()); $request = $request->merge(['id' => $id]); $validatedData = $this->validateService->handle($request->all(), ValidateService::UPDATE); @@ -69,7 +69,7 @@ public function update(Request $request, $id): JsonResponse public function destroy(Request $request, int $id): JsonResponse { - $this->authorize('destroy', $this->searchService->getModel()); + $this->authorize('destroy', $this->persistenceService->getTableName()); $request = $request->merge(['id' => $id]); $validatedData = $this->validateService->handle($request->all(), ValidateService::DESTROY); diff --git a/src/Policy/Policy.php b/src/Policy/Policy.php deleted file mode 100644 index 89401ff..0000000 --- a/src/Policy/Policy.php +++ /dev/null @@ -1,42 +0,0 @@ -delete(); return true; } + + public function getTableName(): string + { + return $this->model->getTable(); + } } diff --git a/src/Services/SearchService.php b/src/Services/SearchService.php index 60aaeb7..abf84d9 100644 --- a/src/Services/SearchService.php +++ b/src/Services/SearchService.php @@ -44,9 +44,9 @@ public function findById(int $id) return $builder->findOrFail($id); } - public function getModel(): SearchModel + public function getTableName(): string { - return $this->model; + return $this->model->getTable(); } public function beforeSearch(Builder $builder, Guard $auth): Builder diff --git a/src/Stubs/Policy.stub b/src/Stubs/Policy.stub index f91a61e..cf8264b 100644 --- a/src/Stubs/Policy.stub +++ b/src/Stubs/Policy.stub @@ -2,9 +2,35 @@ namespace App\Domain\{{singularName}}; -use LaravelDomainOriented\Policy\Policy; +use Illuminate\Auth\Access\HandlesAuthorization; +use Illuminate\Contracts\Auth\Authenticatable as AuthUser; -class {{singularName}}Policy extends Policy +class {{singularName}}Policy { - // + use HandlesAuthorization; + + public function index(): bool + { + return true; + } + + public function show(): bool + { + return true; + } + + public function store(): bool + { + return true; + } + + public function update(): bool + { + return true; + } + + public function destroy(): bool + { + return true; + } } diff --git a/tests/Feature/ControllerTest.php b/tests/Feature/ControllerTest.php index b2741f8..166d368 100644 --- a/tests/Feature/ControllerTest.php +++ b/tests/Feature/ControllerTest.php @@ -2,6 +2,7 @@ namespace Tests\Feature; +use Illuminate\Foundation\Auth\User; use Illuminate\Routing\Router; use LaravelDomainOriented\Tests\Cases\DBTestCase; @@ -24,6 +25,7 @@ public function setUp(): void /** @test **/ public function it_should_call_list_route_and_assert_count_of_items() { + $this->loginWithFakeUser(); $response = $this->getJson('tests'); $response->assertOk(); @@ -32,9 +34,17 @@ public function it_should_call_list_route_and_assert_count_of_items() $this->assertCount(count($this->data), $data['data']); } + /** @test **/ + public function it_should_try_call_list_route_without_login_and_get_403() + { + $response = $this->getJson('tests'); + $response->assertForbidden(); + } + /** @test **/ public function it_should_call_find_route_and_assert_item() { + $this->loginWithFakeUser(); $response = $this->getJson('tests/1'); $response->assertOk(); @@ -46,6 +56,7 @@ public function it_should_call_find_route_and_assert_item() /** @test **/ public function it_should_call_find_route_with_non_existent_id_and_assert_status_404() { + $this->loginWithFakeUser(); $response = $this->getJson('tests/15'); $response->assertStatus(404); } @@ -53,6 +64,7 @@ public function it_should_call_find_route_with_non_existent_id_and_assert_status /** @test **/ public function it_should_create_a_item() { + $this->loginWithFakeUser(); $response = $this->postJson('tests', [ 'name' => 'XXX' ]); @@ -66,6 +78,7 @@ public function it_should_create_a_item() /** @test **/ public function it_should_try_create_a_item_and_assert_status_422() { + $this->loginWithFakeUser(); $response = $this->postJson('tests', [ 'name' => 1 ]); @@ -75,6 +88,7 @@ public function it_should_try_create_a_item_and_assert_status_422() /** @test **/ public function it_should_update_a_item() { + $this->loginWithFakeUser(); $updateName = 'XXX'; $response = $this->putJson('tests/1', [ 'name' => $updateName @@ -89,6 +103,7 @@ public function it_should_update_a_item() /** @test **/ public function it_should_try_update_a_item_and_assert_status_422() { + $this->loginWithFakeUser(); $response = $this->putJson('tests/1', [ 'name' => 1 ]); @@ -98,6 +113,7 @@ public function it_should_try_update_a_item_and_assert_status_422() /** @test **/ public function it_should_delete_a_item() { + $this->loginWithFakeUser(); $this->withoutMiddleware(); $response = $this->deleteJson('tests/1'); $response->assertOk(); @@ -107,4 +123,21 @@ public function it_should_delete_a_item() $this->assertTrue($data['data']['isDeleted']); $this->assertSoftDeleted('tests'); } + + private function loginWithFakeUser() + { + $user = new MyUserModel([ + 'id' => 1, + 'name' => 'test user', + ]); + + $this->be($user); + } +} + +class MyUserModel extends User { + protected $fillable = [ + 'id', + 'name', + ]; }