From 1e7b3da5a64d01d2faebaccef9b01a8673746ace Mon Sep 17 00:00:00 2001 From: LT Date: Fri, 11 Oct 2024 16:00:35 +0300 Subject: [PATCH 1/5] fix: Issue (HasMany resource item) Issue #1294 --- src/Fields/Relationships/HasMany.php | 1 + src/Traits/Resource/ResourceModelQuery.php | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/Fields/Relationships/HasMany.php b/src/Fields/Relationships/HasMany.php index e3b90cbf8..e1d94207b 100644 --- a/src/Fields/Relationships/HasMany.php +++ b/src/Fields/Relationships/HasMany.php @@ -373,6 +373,7 @@ protected function tableValue(): MoonShineRenderable protected function getItemButtons(): array { $resource = $this->getResource(); + $resource->stopGettingItemFromUrl(); $redirectAfter = $this->isAsync() ? '' diff --git a/src/Traits/Resource/ResourceModelQuery.php b/src/Traits/Resource/ResourceModelQuery.php index 11a331e47..724d74c45 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; @@ -101,6 +110,10 @@ public function getItem(): ?Model return null; } + if($this->stopGettingItemFromUrl && blank($this->itemID)) { + return null; + } + return $this->itemOr( fn () => $this ->resolveItemQuery() From ac98df64c771a7a455f6dd8af96bf996df6e9e38 Mon Sep 17 00:00:00 2001 From: LT Date: Fri, 11 Oct 2024 16:58:07 +0300 Subject: [PATCH 2/5] fix: Issue (HasMany resource item) Issue #1294 --- src/Traits/Resource/ResourceModelPolicy.php | 13 +++++++++---- src/Traits/Resource/ResourceModelQuery.php | 12 ++++++------ 2 files changed, 15 insertions(+), 10 deletions(-) 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 724d74c45..64504082c 100644 --- a/src/Traits/Resource/ResourceModelQuery.php +++ b/src/Traits/Resource/ResourceModelQuery.php @@ -71,11 +71,15 @@ public function setItemID(int|string|null $itemID): static public function getItemID(): int|string|null { - if ($this->itemID === '') { + if(!blank($this->itemID)) { + return $this->itemID; + } + + if($this->stopGettingItemFromUrl) { return null; } - return $this->itemID ?? moonshineRequest()->getItemID(); + return moonshineRequest()->getItemID(); } /** @@ -110,10 +114,6 @@ public function getItem(): ?Model return null; } - if($this->stopGettingItemFromUrl && blank($this->itemID)) { - return null; - } - return $this->itemOr( fn () => $this ->resolveItemQuery() From d412f7853afceaca5c11f1ee962432b036de1eef Mon Sep 17 00:00:00 2001 From: LT Date: Fri, 11 Oct 2024 17:08:29 +0300 Subject: [PATCH 3/5] fix: Issue (HasMany resource item) Issue #1294 --- src/Fields/Relationships/HasOne.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Fields/Relationships/HasOne.php b/src/Fields/Relationships/HasOne.php index 838e66808..8d10f396c 100644 --- a/src/Fields/Relationships/HasOne.php +++ b/src/Fields/Relationships/HasOne.php @@ -135,6 +135,7 @@ private function getFieldsOnPreview(): Closure protected function resolveValue(): MoonShineRenderable { $resource = $this->getResource(); + $resource->stopGettingItemFromUrl(); /** @var ModelResource $parentResource */ $parentResource = moonshineRequest()->getResource(); From 1ad9f0ea430986c20e15d22cac74bede4ae97cd9 Mon Sep 17 00:00:00 2001 From: LT Date: Sat, 12 Oct 2024 09:58:52 +0300 Subject: [PATCH 4/5] fix: Issue (HasMany resource item) Issue #1294 --- src/Buttons/HasManyButton.php | 2 +- src/Fields/Relationships/HasMany.php | 5 ++--- src/Fields/Relationships/HasOne.php | 5 ++--- src/Http/Controllers/RelationModelFieldController.php | 3 ++- 4 files changed, 7 insertions(+), 8 deletions(-) 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 e1d94207b..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,8 +372,7 @@ protected function tableValue(): MoonShineRenderable protected function getItemButtons(): array { - $resource = $this->getResource(); - $resource->stopGettingItemFromUrl(); + $resource = $this->getResource()->stopGettingItemFromUrl(); $redirectAfter = $this->isAsync() ? '' diff --git a/src/Fields/Relationships/HasOne.php b/src/Fields/Relationships/HasOne.php index 8d10f396c..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,8 +134,7 @@ private function getFieldsOnPreview(): Closure */ protected function resolveValue(): MoonShineRenderable { - $resource = $this->getResource(); - $resource->stopGettingItemFromUrl(); + $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; From c7f777ca0a282c29259b23161c19210b01176cc8 Mon Sep 17 00:00:00 2001 From: LT Date: Sat, 12 Oct 2024 14:57:09 +0300 Subject: [PATCH 5/5] fix: Issue (HasMany resource item) Issue #1294 --- src/Traits/Resource/ResourceModelQuery.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Traits/Resource/ResourceModelQuery.php b/src/Traits/Resource/ResourceModelQuery.php index 64504082c..31d69faff 100644 --- a/src/Traits/Resource/ResourceModelQuery.php +++ b/src/Traits/Resource/ResourceModelQuery.php @@ -71,6 +71,11 @@ public function setItemID(int|string|null $itemID): static public function getItemID(): int|string|null { + // empty string is the value that stops the logic + if($this->itemID === '') { + return null; + } + if(!blank($this->itemID)) { return $this->itemID; }