diff --git a/src/Models/Link.php b/src/Models/Link.php index dbebf719..2bf82e59 100644 --- a/src/Models/Link.php +++ b/src/Models/Link.php @@ -342,6 +342,57 @@ public function Owner(): ?DataObject return $owner; } + public function canView($member = null) + { + return $this->canPerformAction(__FUNCTION__, $member); + } + + public function canEdit($member = null) + { + return $this->canPerformAction(__FUNCTION__, $member); + } + + public function canDelete($member = null) + { + return $this->canPerformAction(__FUNCTION__, $member); + } + + public function canCreate($member = null, $context = []) + { + return $this->canPerformAction(__FUNCTION__, $member); + } + + public function can($perm, $member = null, $context = []) + { + $check = ucfirst(strtolower($perm)); + return match ($check) { + 'View', 'Create', 'Edit', 'Delete' => $this->{"can$check"}($member, $context), + default => parent::can($perm, $member, $context) + }; + } + + private function canPerformAction(string $canMethod, $member, $context = []) + { + // Allow extensions to override permission checks + $results = $this->extendedCan($canMethod, $member, $context); + if (isset($results)) { + return $results; + } + + // If we have an owner, rely on it to tell us what we can and can't do + $owner = $this->Owner(); + if ($owner && $owner->exists()) { + // Can delete or create links if you can edit its owner. + if ($canMethod === 'canCreate' || $canMethod === 'canDelete') { + $canMethod = 'canEdit'; + } + return $owner->$canMethod($member, $context); + } + + // Default to DataObject's permission checks + return parent::$canMethod($member, $context); + } + /** * Get all link types except the generic one *