diff --git a/src/Buttons/HasManyButton.php b/src/Buttons/HasManyButton.php index 7da625207..bc56919c0 100644 --- a/src/Buttons/HasManyButton.php +++ b/src/Buttons/HasManyButton.php @@ -21,7 +21,7 @@ public static function for( ?ActionButton $button = null, ): ActionButton { /** @var ModelResource $resource */ - $resource = $field->getResource(); + $resource = $field->getResource()->stopGettingItemFromUrl(); $parentResource = moonshineRequest()->getResource(); $parentPage = moonshineRequest()->getPage(); diff --git a/src/Fields/Relationships/HasMany.php b/src/Fields/Relationships/HasMany.php index e3b90cbf8..e59fd43b9 100644 --- a/src/Fields/Relationships/HasMany.php +++ b/src/Fields/Relationships/HasMany.php @@ -277,7 +277,7 @@ protected function tablePreview(): TableBuilder ->implode(';'); } - $resource = $this->getResource(); + $resource = $this->getResource()->stopGettingItemFromUrl(); return TableBuilder::make(items: $items) ->fields($this->getFieldsOnPreview()) @@ -372,7 +372,7 @@ protected function tableValue(): MoonShineRenderable protected function getItemButtons(): array { - $resource = $this->getResource(); + $resource = $this->getResource()->stopGettingItemFromUrl(); $redirectAfter = $this->isAsync() ? '' diff --git a/src/Fields/Relationships/HasOne.php b/src/Fields/Relationships/HasOne.php index 838e66808..ff07aec40 100644 --- a/src/Fields/Relationships/HasOne.php +++ b/src/Fields/Relationships/HasOne.php @@ -85,7 +85,7 @@ protected function resolvePreview(): View|string ->implode(';'); } - $resource = $this->getResource(); + $resource = $this->getResource()->stopGettingItemFromUrl(); return TableBuilder::make(items: $items) ->fields($this->getFieldsOnPreview()) @@ -134,7 +134,7 @@ private function getFieldsOnPreview(): Closure */ protected function resolveValue(): MoonShineRenderable { - $resource = $this->getResource(); + $resource = $this->getResource()->stopGettingItemFromUrl(); /** @var ModelResource $parentResource */ $parentResource = moonshineRequest()->getResource(); diff --git a/src/Http/Controllers/RelationModelFieldController.php b/src/Http/Controllers/RelationModelFieldController.php index 246a93db4..43ca8df41 100644 --- a/src/Http/Controllers/RelationModelFieldController.php +++ b/src/Http/Controllers/RelationModelFieldController.php @@ -141,7 +141,8 @@ public function hasManyForm(RelationModelFieldRequest $request): string $resource = $field->getResource(); $item = $resource - ->setItemID($request->input('_key', '')) + ->stopGettingItemFromUrl() + ->setItemID($request->input('_key')) ->getItemOrInstance(); $update = $item->exists; diff --git a/src/Traits/Resource/ResourceModelPolicy.php b/src/Traits/Resource/ResourceModelPolicy.php index 8f362ffea..0a6909f47 100644 --- a/src/Traits/Resource/ResourceModelPolicy.php +++ b/src/Traits/Resource/ResourceModelPolicy.php @@ -43,9 +43,15 @@ public function can(string $ability): bool } $user = MoonShineAuth::guard()->user(); + $item = $this->getModel(); - $checkCustomRules = moonshine()->authorizationRules() - ->every(fn ($rule) => $rule($this, $user, $ability, $this->getItem() ?? $this->getModel())); + if (! in_array($ability, ['create', 'massDelete'])) { + $item = $this->getItemOrInstance(); + } + + $checkCustomRules = moonshine() + ->authorizationRules() + ->every(fn($rule) => $rule($this, $user, $ability, $item)); if (! $checkCustomRules) { return false; @@ -55,8 +61,7 @@ public function can(string $ability): bool return true; } - return Gate::forUser($user) - ->allows($ability, $this->getItem() ?? $this->getModel()); + return Gate::forUser($user)->allows($ability, $item); } public function isWithPolicy(): bool diff --git a/src/Traits/Resource/ResourceModelQuery.php b/src/Traits/Resource/ResourceModelQuery.php index 11a331e47..31d69faff 100644 --- a/src/Traits/Resource/ResourceModelQuery.php +++ b/src/Traits/Resource/ResourceModelQuery.php @@ -48,11 +48,20 @@ trait ResourceModelQuery protected int|string|null $itemID = null; + protected bool $stopGettingItemFromUrl = false; + protected array $parentRelations = []; // TODO 3.0 rename to saveQueryState protected bool $saveFilterState = false; + public function stopGettingItemFromUrl(): static + { + $this->stopGettingItemFromUrl = true; + + return $this; + } + public function setItemID(int|string|null $itemID): static { $this->itemID = $itemID; @@ -62,11 +71,20 @@ public function setItemID(int|string|null $itemID): static public function getItemID(): int|string|null { - if ($this->itemID === '') { + // empty string is the value that stops the logic + if($this->itemID === '') { + return null; + } + + if(!blank($this->itemID)) { + return $this->itemID; + } + + if($this->stopGettingItemFromUrl) { return null; } - return $this->itemID ?? moonshineRequest()->getItemID(); + return moonshineRequest()->getItemID(); } /**