Skip to content

Commit 7fefd8f

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents e5760c9 + 49c8e77 commit 7fefd8f

7 files changed

+261
-20
lines changed

src/Concerns/HandlesRelationManyToManyOperations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ protected function updatePivotWithTransaction(Request $request, $parentKey, $rel
632632
$query = $this->buildShowFetchQuery($request, $parentEntity, []);
633633
$entity = $this->runShowFetchQuery($request, $query, $parentEntity, $relatedKey);
634634

635-
$this->authorize('update', $entity);
635+
$this->authorize('update', [$entity, $parentEntity]);
636636

637637
$updateResult = $this->performUpdatePivot($request, $parentEntity, $relatedKey, $request->get('pivot', []));
638638

src/Concerns/HandlesRelationOneToManyOperations.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected function associateWithTransaction(Request $request, $parentKey)
5252
}
5353

5454
$this->authorize('view', $parentEntity);
55-
$this->authorize('update', $entity);
55+
$this->authorize('update', [$entity, $parentEntity]);
5656

5757
$this->performAssociate($request, $parentEntity, $entity);
5858

@@ -204,7 +204,7 @@ protected function dissociateWithTransaction(Request $request, $parentKey, $rela
204204
return $beforeHookResult;
205205
}
206206

207-
$this->authorize('update', $entity);
207+
$this->authorize('update', [$entity, $parentEntity]);
208208

209209
$this->performDissociate($request, $parentEntity, $entity);
210210

src/Concerns/HandlesRelationStandardBatchOperations.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ public function batchStore(Request $request, $parentKey)
4141
*/
4242
protected function batchStoreWithTransaction(Request $request, $parentKey)
4343
{
44-
$resourceModelClass = $this->resolveResourceModelClass();
45-
46-
$this->authorize('create', $resourceModelClass);
47-
4844
$parentQuery = $this->buildBatchStoreParentFetchQuery($request, $parentKey);
4945
$parentEntity = $this->runBatchStoreParentFetchQuery($request, $parentQuery, $parentKey);
5046

47+
$resourceModelClass = $this->resolveResourceModelClass();
48+
49+
$this->authorize('create', [$resourceModelClass, $parentEntity]);
50+
5151
$beforeHookResult = $this->beforeBatchStore($request, $parentEntity);
5252
if ($this->hookResponds($beforeHookResult)) {
5353
return $beforeHookResult;
@@ -194,7 +194,7 @@ protected function batchUpdateWithTransaction(Request $request, $parentKey)
194194

195195
foreach ($entities as $entity) {
196196
/** @var Model $entity */
197-
$this->authorize('update', $entity);
197+
$this->authorize('update', [$entity, $parentEntity]);
198198

199199
$resource = $request->input("resources.{$entity->{$this->keyName()}}");
200200

@@ -393,7 +393,7 @@ protected function batchDestroyWithTransaction(Request $request, $parentKey)
393393

394394
foreach ($entities as $entity) {
395395
/** @var Model $entity */
396-
$this->authorize($forceDeletes ? 'forceDelete' : 'delete', $entity);
396+
$this->authorize($forceDeletes ? 'forceDelete' : 'delete', [$entity, $parentEntity]);
397397

398398
$this->beforeDestroy($request, $parentEntity, $entity);
399399

@@ -560,7 +560,7 @@ protected function batchRestoreWithTransaction(Request $request, $parentKey)
560560

561561
foreach ($entities as $entity) {
562562
/** @var Model $entity */
563-
$this->authorize('restore', $entity);
563+
$this->authorize('restore', [$entity, $parentEntity]);
564564

565565
$this->beforeRestore($request, $parentEntity, $entity);
566566

src/Concerns/HandlesRelationStandardOperations.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ trait HandlesRelationStandardOperations
3232
*/
3333
public function index(Request $request, $parentKey)
3434
{
35-
$this->authorize('viewAny', $this->resolveResourceModelClass());
36-
3735
$requestedRelations = $this->relationsResolver->requestedRelations($request);
3836

3937
$parentQuery = $this->buildIndexParentFetchQuery($request, $parentKey);
4038
$parentEntity = $this->runIndexParentFetchQuery($request, $parentQuery, $parentKey);
4139

40+
$this->authorize('viewAny', [$this->resolveResourceModelClass(), $parentEntity]);
41+
4242
$beforeHookResult = $this->beforeIndex($request, $parentEntity);
4343
if ($this->hookResponds($beforeHookResult)) {
4444
return $beforeHookResult;
@@ -251,13 +251,13 @@ public function store(Request $request, $parentKey)
251251
*/
252252
protected function storeWithTransaction(Request $request, $parentKey)
253253
{
254-
$resourceModelClass = $this->resolveResourceModelClass();
255-
256-
$this->authorize('create', $resourceModelClass);
257-
258254
$parentQuery = $this->buildStoreParentFetchQuery($request, $parentKey);
259255
$parentEntity = $this->runStoreParentFetchQuery($request, $parentQuery, $parentKey);
260256

257+
$resourceModelClass = $this->resolveResourceModelClass();
258+
259+
$this->authorize('create', [$resourceModelClass, $parentEntity]);
260+
261261
/** @var Model $entity */
262262
$entity = new $resourceModelClass;
263263

@@ -434,7 +434,7 @@ public function show(Request $request, $parentKey, $relatedKey = null)
434434
$query = $this->buildShowFetchQuery($request, $parentEntity, $requestedRelations);
435435
$entity = $this->runShowFetchQuery($request, $query, $parentEntity, $relatedKey);
436436

437-
$this->authorize('view', $entity);
437+
$this->authorize('view', [$entity, $parentEntity]);
438438

439439
$entity = $this->cleanupEntity($entity);
440440

@@ -613,7 +613,7 @@ protected function updateWithTransaction(Request $request, $parentKey, $relatedK
613613
$query = $this->buildUpdateFetchQuery($request, $parentEntity, $requestedRelations);
614614
$entity = $this->runUpdateFetchQuery($request, $query, $parentEntity, $relatedKey);
615615

616-
$this->authorize('update', $entity);
616+
$this->authorize('update', [$entity, $parentEntity]);
617617

618618
$beforeHookResult = $this->beforeUpdate($request, $parentEntity, $entity);
619619
if ($this->hookResponds($beforeHookResult)) {
@@ -811,7 +811,7 @@ protected function destroyWithTransaction(Request $request, $parentKey, $related
811811
abort(404);
812812
}
813813

814-
$this->authorize($forceDeletes ? 'forceDelete' : 'delete', $entity);
814+
$this->authorize($forceDeletes ? 'forceDelete' : 'delete', [$entity, $parentEntity]);
815815

816816
$beforeHookResult = $this->beforeDestroy($request, $parentEntity, $entity);
817817
if ($this->hookResponds($beforeHookResult)) {
@@ -994,7 +994,7 @@ protected function restoreWithTransaction(Request $request, $parentKey, $related
994994
$query = $this->buildRestoreFetchQuery($request, $parentEntity, $requestedRelations);
995995
$entity = $this->runRestoreFetchQuery($request, $query, $parentEntity, $relatedKey);
996996

997-
$this->authorize('restore', $entity);
997+
$this->authorize('restore', [$entity, $parentEntity]);
998998

999999
$beforeHookResult = $this->beforeRestore($request, $parentEntity, $entity);
10001000
if ($this->hookResponds($beforeHookResult)) {

src/Concerns/HandlesStandardBatchOperations.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ protected function batchStoreWithTransaction(Request $request)
6262

6363
$this->performStore($request, $entity, $resource);
6464

65+
$this->beforeStoreFresh($request, $entity);
66+
6567
$entity = $entity->fresh($requestedRelations);
6668
$entity->wasRecentlyCreated = true;
6769

@@ -153,6 +155,8 @@ protected function batchUpdateWithTransaction(Request $request)
153155
$request->input("resources.{$entity->{$this->keyName()}}")
154156
);
155157

158+
$this->beforeUpdateFresh($request, $entity);
159+
156160
$entity = $entity->fresh($requestedRelations);
157161

158162
$this->afterSave($request, $entity);
@@ -296,6 +300,7 @@ protected function batchDestroyWithTransaction(Request $request)
296300
if (!$forceDeletes) {
297301
$this->performDestroy($entity);
298302
if ($softDeletes) {
303+
$this->beforeDestroyFresh($request, $entity);
299304
$entity = $entity->fresh($requestedRelations);
300305
}
301306
} else {
@@ -414,6 +419,8 @@ protected function batchRestoreWithTransaction(Request $request)
414419

415420
$this->performRestore($entity);
416421

422+
$this->beforeRestoreFresh($request, $entity);
423+
417424
$entity = $entity->fresh($requestedRelations);
418425

419426
$this->afterRestore($request, $entity);

src/Concerns/HandlesStandardOperations.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ protected function storeWithTransaction(Request $request)
174174
$request->all()
175175
);
176176

177+
$beforeStoreFreshResult = $this->beforeStoreFresh($request, $entity);
178+
if ($this->hookResponds($beforeStoreFreshResult)) {
179+
return $beforeStoreFreshResult;
180+
}
181+
177182
$entity = $entity->fresh($requestedRelations);
178183
$entity->wasRecentlyCreated = true;
179184

@@ -229,6 +234,18 @@ protected function performStore(Request $request, Model $entity, array $attribut
229234
$entity->save();
230235
}
231236

237+
/**
238+
* The hook is executed after creating and before refreshing the resource.
239+
*
240+
* @param Request $request
241+
* @param Model $entity
242+
* @return mixed
243+
*/
244+
protected function beforeStoreFresh(Request $request, Model $entity)
245+
{
246+
return null;
247+
}
248+
232249
/**
233250
* The hook is executed after creating or updating a resource.
234251
*
@@ -398,6 +415,11 @@ protected function updateWithTransaction(Request $request, $key)
398415
$request->all()
399416
);
400417

418+
$beforeUpdateFreshResult = $this->beforeUpdateFresh($request, $entity);
419+
if ($this->hookResponds($beforeUpdateFreshResult)) {
420+
return $beforeUpdateFreshResult;
421+
}
422+
401423
$entity = $entity->fresh($requestedRelations);
402424

403425
$afterSaveHookResult = $this->afterSave($request, $entity);
@@ -465,6 +487,18 @@ protected function performUpdate(Request $request, Model $entity, array $attribu
465487
$entity->save();
466488
}
467489

490+
/**
491+
* The hook is executed after updating and before refreshing the resource.
492+
*
493+
* @param Request $request
494+
* @param Model $entity
495+
* @return mixed
496+
*/
497+
protected function beforeUpdateFresh(Request $request, Model $entity)
498+
{
499+
return null;
500+
}
501+
468502
/**
469503
* The hook is executed after updating a resource.
470504
*
@@ -529,6 +563,11 @@ protected function destroyWithTransaction(Request $request, $key)
529563
if (!$forceDeletes) {
530564
$this->performDestroy($entity);
531565
if ($softDeletes) {
566+
$beforeDestroyFreshResult = $this->beforeDestroyFresh($request, $entity);
567+
if ($this->hookResponds($beforeDestroyFreshResult)) {
568+
return $beforeDestroyFreshResult;
569+
}
570+
532571
$entity = $entity->fresh($requestedRelations);
533572
}
534573
} else {
@@ -610,6 +649,19 @@ protected function performForceDestroy(Model $entity): void
610649
$entity->forceDelete();
611650
}
612651

652+
/**
653+
* The hook is executed after deleting and before refreshing the resource.
654+
* This hook is only called when not using forced deletes
655+
*
656+
* @param Request $request
657+
* @param Model $entity
658+
* @return mixed
659+
*/
660+
protected function beforeDestroyFresh(Request $request, Model $entity)
661+
{
662+
return null;
663+
}
664+
613665
/**
614666
* The hook is executed after deleting a resource.
615667
*
@@ -666,6 +718,11 @@ protected function restoreWithTransaction(Request $request, $key)
666718

667719
$this->performRestore($entity);
668720

721+
$beforeHookResult = $this->beforeRestoreFresh($request, $entity);
722+
if ($this->hookResponds($beforeHookResult)) {
723+
return $beforeHookResult;
724+
}
725+
669726
$entity = $entity->fresh($requestedRelations);
670727

671728
$afterHookResult = $this->afterRestore($request, $entity);
@@ -726,6 +783,19 @@ protected function performRestore(Model $entity): void
726783
$entity->restore();
727784
}
728785

786+
/**
787+
* The hook is executed after force restoring a previously deleted resource but before
788+
* refreshing the resource.
789+
*
790+
* @param Request $request
791+
* @param Model $entity
792+
* @return mixed
793+
*/
794+
protected function beforeRestoreFresh(Request $request, Model $entity)
795+
{
796+
return null;
797+
}
798+
729799
/**
730800
* The hook is executed after force restoring a previously deleted resource.
731801
*

0 commit comments

Comments
 (0)