Skip to content

Commit

Permalink
Implement new GateEvaluated event (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints authored Feb 22, 2021
1 parent 2544c87 commit 4b9d689
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 3 additions & 2 deletions src/Watchers/FetchesStackTrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
17 changes: 14 additions & 3 deletions src/Watchers/GateWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand All @@ -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,
Expand Down
27 changes: 23 additions & 4 deletions tests/Watchers/GateWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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']);
Expand Down

0 comments on commit 4b9d689

Please sign in to comment.