From a86e0f8d06c6fd088ef2f243d5cc6f9ed2175a3e Mon Sep 17 00:00:00 2001 From: Thibault Date: Wed, 5 Mar 2025 13:50:00 +0100 Subject: [PATCH 1/3] Allow to view as guest --- testbench.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/testbench.yaml b/testbench.yaml index 4d0c5709b..7a6329c11 100644 --- a/testbench.yaml +++ b/testbench.yaml @@ -10,4 +10,3 @@ seeders: workbench: start: '/telescope' - user: 1 From b83cc09b71faf00291364dd8ab289ac07dcf5220 Mon Sep 17 00:00:00 2001 From: Thibault Date: Wed, 5 Mar 2025 14:16:42 +0100 Subject: [PATCH 2/3] Save and view gate's response message --- resources/js/screens/gates/index.vue | 5 ++ resources/js/screens/gates/preview.vue | 7 +++ src/Watchers/GateWatcher.php | 16 +++++ tests/Watchers/GateWatcherTest.php | 87 +++++++++++++++++++++++--- 4 files changed, 105 insertions(+), 10 deletions(-) diff --git a/resources/js/screens/gates/index.vue b/resources/js/screens/gates/index.vue index de8d7b7d0..b081818a7 100644 --- a/resources/js/screens/gates/index.vue +++ b/resources/js/screens/gates/index.vue @@ -13,6 +13,7 @@ Ability Result + Message Happened @@ -27,6 +28,10 @@ + + {{truncate(slotProps.entry.content.message, 30)}} + + {{timeAgo(slotProps.entry.created_at)}} diff --git a/resources/js/screens/gates/preview.vue b/resources/js/screens/gates/preview.vue index 86a132e03..0cb6f78d5 100644 --- a/resources/js/screens/gates/preview.vue +++ b/resources/js/screens/gates/preview.vue @@ -35,6 +35,13 @@ + + Message + + {{slotProps.entry.content.message}} + + + Location diff --git a/src/Watchers/GateWatcher.php b/src/Watchers/GateWatcher.php index 431613597..dc413a935 100644 --- a/src/Watchers/GateWatcher.php +++ b/src/Watchers/GateWatcher.php @@ -56,6 +56,7 @@ public function recordGateCheck($user, $ability, $result, $arguments) Telescope::recordGate(IncomingEntry::make([ 'ability' => $ability, 'result' => $this->gateResult($result), + 'message' => $this->gateMessage($result), 'arguments' => $this->formatArguments($arguments), 'file' => $caller['file'] ?? null, 'line' => $caller['line'] ?? null, @@ -90,6 +91,21 @@ private function gateResult($result) return $result ? 'allowed' : 'denied'; } + /** + * Get the message returned by the gate + * + * @param bool|\Illuminate\Auth\Access\Response $result + * @return null + */ + private function gateMessage($result) : ?string + { + if ($result instanceof Response) { + return $result->message(); + } + + return null; + } + /** * Format the given arguments. * diff --git a/tests/Watchers/GateWatcherTest.php b/tests/Watchers/GateWatcherTest.php index e6fdf5375..3fcfa1f1c 100644 --- a/tests/Watchers/GateWatcherTest.php +++ b/tests/Watchers/GateWatcherTest.php @@ -31,6 +31,18 @@ protected function getEnvironmentSetUp($app) Gate::define('deny potato', function (?User $user) { return false; }); + + Gate::define('potato message', function (User $user) { + return $user->email === 'allow' ? Response::allow('allow potato') : Response::deny('allow potato'); + }); + + Gate::define('guest potato message', function (?User $user) { + return Response::allow(); + }); + + Gate::define('deny potato message', function (?User $user) { + return Response::deny('deny potato'); + }); } public function test_gate_watcher_registers_allowed_entries() @@ -44,7 +56,7 @@ public function test_gate_watcher_registers_allowed_entries() $this->assertTrue($check); $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(40, $entry->content['line']); + $this->assertSame(52, $entry->content['line']); $this->assertSame('potato', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertEmpty($entry->content['arguments']); @@ -61,7 +73,7 @@ public function test_gate_watcher_registers_denied_entries() $this->assertFalse($check); $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(57, $entry->content['line']); + $this->assertSame(69, $entry->content['line']); $this->assertSame('potato', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame(['banana'], $entry->content['arguments']); @@ -78,12 +90,63 @@ public function test_gate_watcher_registers_allowed_guest_entries() $this->assertTrue($check); $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(74, $entry->content['line']); + $this->assertSame(86, $entry->content['line']); $this->assertSame('guest potato', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertEmpty($entry->content['arguments']); } + public function test_gate_watcher_registers_allowed_entries_with_message() + { + $this->app->setBasePath(dirname(__FILE__, 3)); + + $check = Gate::forUser(new User('allow'))->check('potato message'); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertTrue($check); + $this->assertSame(EntryType::GATE, $entry->type); + $this->assertSame(__FILE__, $entry->content['file']); + $this->assertSame(103, $entry->content['line']); + $this->assertSame('potato message', $entry->content['ability']); + $this->assertSame('allowed', $entry->content['result']); + $this->assertEmpty($entry->content['arguments']); + } + + public function test_gate_watcher_registers_denied_entries_with_message() + { + $this->app->setBasePath(dirname(__FILE__, 3)); + + $check = Gate::forUser(new User('deny'))->check('potato message', ['banana']); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertFalse($check); + $this->assertSame(EntryType::GATE, $entry->type); + $this->assertSame(__FILE__, $entry->content['file']); + $this->assertSame(120, $entry->content['line']); + $this->assertSame('potato message', $entry->content['ability']); + $this->assertSame('denied', $entry->content['result']); + $this->assertSame(['banana'], $entry->content['arguments']); + } + + public function test_gate_watcher_registers_allowed_guest_entries_with_message() + { + $this->app->setBasePath(dirname(__FILE__, 3)); + + $check = Gate::check('guest potato message'); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertTrue($check); + $this->assertSame(EntryType::GATE, $entry->type); + $this->assertSame(__FILE__, $entry->content['file']); + $this->assertSame(137, $entry->content['line']); + $this->assertSame('guest potato message', $entry->content['ability']); + $this->assertSame('allowed', $entry->content['result']); + $this->assertEmpty($entry->content['arguments']); + } + public function test_gate_watcher_registers_denied_guest_entries() { $this->app->setBasePath(dirname(__FILE__, 3)); @@ -95,7 +158,7 @@ public function test_gate_watcher_registers_denied_guest_entries() $this->assertFalse($check); $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(91, $entry->content['line']); + $this->assertSame(154, $entry->content['line']); $this->assertSame('deny potato', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame(['gelato'], $entry->content['arguments']); @@ -113,10 +176,11 @@ public function test_gate_watcher_registers_allowed_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(303, $entry->content['line']); + $this->assertSame(370, $entry->content['line']); $this->assertSame('create', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); + $this->assertNull($entry->content['message']); } public function test_gate_watcher_registers_after_checks() @@ -134,7 +198,7 @@ public function test_gate_watcher_registers_after_checks() $this->assertTrue($check); $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(130, $entry->content['line']); + $this->assertSame(194, $entry->content['line']); $this->assertSame('foo-bar', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertEmpty($entry->content['arguments']); @@ -156,10 +220,11 @@ public function test_gate_watcher_registers_denied_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(308, $entry->content['line']); + $this->assertSame(375, $entry->content['line']); $this->assertSame('update', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); + $this->assertNull($entry->content['message']); } public function test_gate_watcher_calls_format_for_telescope_method_if_it_exists() @@ -178,7 +243,7 @@ public function test_gate_watcher_calls_format_for_telescope_method_if_it_exists $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(308, $entry->content['line']); + $this->assertSame(375, $entry->content['line']); $this->assertSame('update', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame([['Telescope', 'Laravel', 'PHP']], $entry->content['arguments']); @@ -200,10 +265,11 @@ public function test_gate_watcher_registers_allowed_response_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(298, $entry->content['line']); + $this->assertSame(365, $entry->content['line']); $this->assertSame('view', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); + $this->assertSame('this action is allowed', $entry->content['message']); } public function test_gate_watcher_registers_denied_response_policy_entries() @@ -222,10 +288,11 @@ public function test_gate_watcher_registers_denied_response_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(313, $entry->content['line']); + $this->assertSame(380, $entry->content['line']); $this->assertSame('delete', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); + $this->assertSame('this action is denied', $entry->content['message']); } } From 2bd57210af7b750a2ec09661bf6451f355e2d3d0 Mon Sep 17 00:00:00 2001 From: Thibault Date: Wed, 5 Mar 2025 14:23:12 +0100 Subject: [PATCH 3/3] fix style --- src/Watchers/GateWatcher.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Watchers/GateWatcher.php b/src/Watchers/GateWatcher.php index dc413a935..b2f928000 100644 --- a/src/Watchers/GateWatcher.php +++ b/src/Watchers/GateWatcher.php @@ -92,12 +92,12 @@ private function gateResult($result) } /** - * Get the message returned by the gate + * Get the message returned by the gate. * * @param bool|\Illuminate\Auth\Access\Response $result * @return null */ - private function gateMessage($result) : ?string + private function gateMessage($result): ?string { if ($result instanceof Response) { return $result->message();