Skip to content
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
5 changes: 5 additions & 0 deletions resources/js/screens/gates/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
<tr slot="table-header">
<th scope="col">Ability</th>
<th scope="col">Result</th>
<th scope="col">Message</th>
<th scope="col">Happened</th>
<th scope="col"></th>
</tr>
Expand All @@ -26,6 +27,10 @@ export default {
</span>
</td>

<td class="table-fit">
{{truncate(slotProps.entry.content.message, 30)}}
</td>

<td
class="table-fit text-muted"
:data-timeago="slotProps.entry.created_at"
Expand Down
7 changes: 7 additions & 0 deletions resources/js/screens/gates/preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ export default {
</td>
</tr>

<tr>
<td class="table-fit text-muted">Message</td>
<td>
{{slotProps.entry.content.message}}
</td>
</tr>

<tr v-if="slotProps.entry.content.file">
<td class="table-fit text-muted">Location</td>
<td>{{ slotProps.entry.content.file }}:{{ slotProps.entry.content.line }}</td>
Expand Down
16 changes: 16 additions & 0 deletions src/Watchers/GateWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down
87 changes: 77 additions & 10 deletions tests/Watchers/GateWatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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']);
Expand All @@ -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']);
Expand All @@ -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));
Expand All @@ -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']);
Expand All @@ -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()
Expand All @@ -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']);
Expand All @@ -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()
Expand All @@ -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']);
Expand All @@ -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()
Expand All @@ -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']);
}
}

Expand Down