diff --git a/composer.json b/composer.json index e62559cc0..a36966c4f 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "require": { "php": "^7.3|^8.0", "ext-json": "*", - "laravel/framework": "^8.2", + "laravel/framework": "^8.29", "symfony/var-dumper": "^5.0" }, "require-dev": { diff --git a/src/Watchers/FetchesStackTrace.php b/src/Watchers/FetchesStackTrace.php index 8c8caf504..aa06d8c89 100644 --- a/src/Watchers/FetchesStackTrace.php +++ b/src/Watchers/FetchesStackTrace.php @@ -9,11 +9,12 @@ trait FetchesStackTrace /** * Find the first frame in the stack trace outside of Telescope/Laravel. * + * @param string|array $forgetLines * @return array */ - protected function getCallerFromStackTrace() + protected function getCallerFromStackTrace($forgetLines = 0) { - $trace = collect(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS))->forget(0); + $trace = collect(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS))->forget($forgetLines); return $trace->first(function ($frame) { if (! isset($frame['file'])) { diff --git a/src/Watchers/GateWatcher.php b/src/Watchers/GateWatcher.php index dd990d1de..ae38d4970 100644 --- a/src/Watchers/GateWatcher.php +++ b/src/Watchers/GateWatcher.php @@ -2,10 +2,10 @@ namespace Laravel\Telescope\Watchers; +use Illuminate\Auth\Access\Events\GateEvaluated; use Illuminate\Auth\Access\Response; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Facades\Gate; use Illuminate\Support\Str; use Laravel\Telescope\FormatModel; use Laravel\Telescope\IncomingEntry; @@ -23,7 +23,18 @@ class GateWatcher extends Watcher */ public function register($app) { - Gate::after([$this, 'recordGateCheck']); + $app['events']->listen(GateEvaluated::class, [$this, 'handleGateEvaluated']); + } + + /** + * Handle the GateEvaluated event. + * + * @param \Illuminate\Auth\Access\Events\GateEvaluated $event + * @return void + */ + public function handleGateEvaluated(GateEvaluated $event) + { + $this->recordGateCheck($event->user, $event->ability, $event->result, $event->arguments); } /** @@ -41,7 +52,7 @@ public function recordGateCheck(?Authenticatable $user, $ability, $result, $argu return; } - $caller = $this->getCallerFromStackTrace(); + $caller = $this->getCallerFromStackTrace([0, 1]); Telescope::recordGate(IncomingEntry::make([ 'ability' => $ability, diff --git a/tests/Watchers/GateWatcherTest.php b/tests/Watchers/GateWatcherTest.php index 9b2c19e6c..d67ac250b 100644 --- a/tests/Watchers/GateWatcherTest.php +++ b/tests/Watchers/GateWatcherTest.php @@ -105,12 +105,31 @@ public function test_gate_watcher_registers_allowed_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(231, $entry->content['line']); + $this->assertSame(250, $entry->content['line']); $this->assertSame('create', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); } + public function test_gate_watcher_registers_after_checks() + { + Gate::after(function (?User $user) { + return true; + }); + + $check = Gate::check('foo-bar'); + + $entry = $this->loadTelescopeEntries()->first(); + + $this->assertTrue($check); + $this->assertSame(EntryType::GATE, $entry->type); + $this->assertSame(__FILE__, $entry->content['file']); + $this->assertSame(120, $entry->content['line']); + $this->assertSame('foo-bar', $entry->content['ability']); + $this->assertSame('allowed', $entry->content['result']); + $this->assertEmpty($entry->content['arguments']); + } + public function test_gate_watcher_registers_denied_policy_entries() { Gate::policy(TestResource::class, TestPolicy::class); @@ -125,7 +144,7 @@ public function test_gate_watcher_registers_denied_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(236, $entry->content['line']); + $this->assertSame(255, $entry->content['line']); $this->assertSame('update', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); @@ -145,7 +164,7 @@ public function test_gate_watcher_registers_allowed_response_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(226, $entry->content['line']); + $this->assertSame(245, $entry->content['line']); $this->assertSame('view', $entry->content['ability']); $this->assertSame('allowed', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']); @@ -165,7 +184,7 @@ public function test_gate_watcher_registers_denied_response_policy_entries() $this->assertSame(EntryType::GATE, $entry->type); $this->assertSame(__FILE__, $entry->content['file']); - $this->assertSame(241, $entry->content['line']); + $this->assertSame(260, $entry->content['line']); $this->assertSame('delete', $entry->content['ability']); $this->assertSame('denied', $entry->content['result']); $this->assertSame([[]], $entry->content['arguments']);