Skip to content

Commit

Permalink
Merge pull request #1297 from lee-to/fix-issue-1294
Browse files Browse the repository at this point in the history
fix: Issue (HasMany resource item)
  • Loading branch information
lee-to authored Oct 12, 2024
2 parents 699cf28 + c7f777c commit a1c827c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Buttons/HasManyButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Relationships/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ protected function tablePreview(): TableBuilder
->implode(';');
}

$resource = $this->getResource();
$resource = $this->getResource()->stopGettingItemFromUrl();

return TableBuilder::make(items: $items)
->fields($this->getFieldsOnPreview())
Expand Down Expand Up @@ -372,7 +372,7 @@ protected function tableValue(): MoonShineRenderable

protected function getItemButtons(): array
{
$resource = $this->getResource();
$resource = $this->getResource()->stopGettingItemFromUrl();

$redirectAfter = $this->isAsync()
? ''
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Relationships/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/Http/Controllers/RelationModelFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions src/Traits/Resource/ResourceModelPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
22 changes: 20 additions & 2 deletions src/Traits/Resource/ResourceModelQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

/**
Expand Down

0 comments on commit a1c827c

Please sign in to comment.