Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Implement new GateEvaluated event #1018

Merged
merged 1 commit into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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