Skip to content

Commit 5d90ce9

Browse files
committed
updates on permissions for property update
1 parent ce8c7db commit 5d90ce9

File tree

5 files changed

+53
-22
lines changed

5 files changed

+53
-22
lines changed

app/Http/Controllers/Api/v1/PropertyController.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111
use Illuminate\Http\Request;
1212
use Illuminate\Http\Response;
1313
use Spatie\QueryBuilder\QueryBuilder;
14-
use App\Services\Properties\PropertyRepositoryService;
14+
use App\Repositories\PropertyRepository;
15+
use Illuminate\Support\Facades\Gate;
1516

1617
class PropertyController extends Controller
1718
{
1819
public function __construct()
1920
{
2021
//the user needs to be logged in for these methods to be accessed
21-
$this->middleware('auth:api')->only('store');
22+
$this->middleware('auth:api')->only('store', 'update');
2223
}
2324

2425
/**
@@ -102,10 +103,11 @@ public function index(GenericListingRequest $request)
102103
*
103104
* `slug` field is set automatically using the `name` field when the status is set to <b>active</b> (value 1). Once set it can't be changed.
104105
*/
105-
public function store(StorePropertyRequest $request, PropertyRepositoryService $propertyRepositoryService)
106+
public function store(StorePropertyRequest $request, PropertyRepository $propertyRepository)
106107
{
107108
//store the new property
108-
$property = $propertyRepositoryService->authUserRequestCreateProperty();
109+
Gate::authorize('create', Property::class);
110+
$property = $propertyRepository->authUserCreateRequestProperty();
109111

110112
return response()
111113
->json(['data' => [
@@ -181,10 +183,13 @@ public function show(Request $request, int $property)
181183

182184
/**
183185
* Update a property
186+
*
187+
* @throws \Illuminate\Auth\Access\AuthorizationException|\Illuminate\Auth\Access\AuthenticationException
184188
*/
185-
public function update(UpdatePropertyRequest $request, Property $property)
189+
public function update(UpdatePropertyRequest $request, Property $property, PropertyRepository $propertyRepository)
186190
{
187-
$property->update($request->only('name', 'status_id'));
191+
Gate::authorize('update', $property);
192+
$propertyRepository->updateRequestProperty(property: $property);
188193

189194
return response([])->setStatusCode(Response::HTTP_NO_CONTENT);
190195
}

app/Repositories/PropertyRepository.php

+10
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,14 @@ public function setSlugProperty(Property &$property)
2222
return;
2323
}
2424
}
25+
26+
public function authUserCreateRequestProperty() : Property
27+
{
28+
return Property::create([...request()->only('name', 'status_id'), ...['owner_id' => auth()->user()->id]]);
29+
}
30+
31+
public function updateRequestProperty(Property $property) : void
32+
{
33+
$property->update(request()->only('name', 'status_id'));
34+
}
2535
}

app/Services/Properties/PropertyRepositoryService.php

-13
This file was deleted.

bootstrap/providers.php

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

33
return [
44
App\Providers\AppServiceProvider::class,
5+
App\Providers\AuthServiceProvider::class,
6+
App\Providers\AuthorizationServiceProvider::class,
57
];

tests/Feature/Api/v1/PropertiesControllerTest.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,34 @@
249249
]);
250250
});
251251

252-
it('updates a property', function (): void {
252+
it('requires to be authenticated to try to update a property', function (): void {
253+
//make a property
254+
$property = Property::factory()->create();
255+
256+
// send request to update
257+
$response = $this->putJson(route('api.v1.properties.update', $property->id), [
258+
'name' => 'Moon Villa',
259+
'status_id' => PropertyStatus::Inactive->value,
260+
]);
261+
262+
$response->assertUnauthorized();
263+
});
264+
265+
it('requires to be autorized to update a property', function (): void {
266+
//make a property
267+
$property = Property::factory()->create();
268+
269+
// send request to update
270+
$anotherUser = User::factory()->create();
271+
$response = $this->actingAs($anotherUser)->putJson(route('api.v1.properties.update', $property->id), [
272+
'name' => 'Moon Villa',
273+
'status_id' => PropertyStatus::Inactive->value,
274+
]);
275+
276+
$response->assertForbidden();
277+
});
278+
279+
it('updates a property with only the allowed fields', function (): void {
253280
$user = User::factory()->create();
254281
$propertyData = [
255282
'name' => 'Sea Villa',
@@ -261,7 +288,7 @@
261288

262289
// send request to update
263290
$failUser = User::factory()->create();
264-
$response = $this->putJson(route('api.v1.properties.update', $property->id), [
291+
$response = $this->actingAs($user)->putJson(route('api.v1.properties.update', $property->id), [
265292
'id' => '5',
266293
'name' => 'Moon Villa',
267294
'owner_id' => $failUser->id,
@@ -282,7 +309,7 @@
282309
it('returns 404 for update on a non-existing property', function (): void {
283310

284311
$user = User::factory()->create();
285-
$response = $this->putJson(route('api.v1.properties.update', 1), [
312+
$response = $this->actingAs($user)->putJson(route('api.v1.properties.update', 1), [
286313
'name' => 'Moon Villa',
287314
'owner_id' => $user->id,
288315
'status_id' => PropertyStatus::Inactive->value,

0 commit comments

Comments
 (0)