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

Trace file for Gate checks(GateCollector) #1770

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 4 additions & 1 deletion config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,13 @@
'show_name' => true, // Also show the users name/email in the debugbar
'show_guards' => true, // Show the guards that are used
],
'gate' => [
'trace' => false, // Trace the origin of the Gate checks
],
'db' => [
'with_params' => true, // Render SQL with the parameters substituted
'exclude_paths' => [ // Paths to exclude entirely from the collector
// 'vendor/laravel/framework/src/Illuminate/Session', // Exclude sessions queries
//'vendor/laravel/framework/src/Illuminate/Session', // Exclude sessions queries
],
'backtrace' => true, // Use a backtrace to find the origin of the query in your files.
'backtrace_exclude_paths' => [], // Paths to exclude from backtrace. (in addition to defaults)
Expand Down
99 changes: 98 additions & 1 deletion src/DataCollector/GateCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Contracts\Auth\Access\Gate;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Routing\Router;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Illuminate\Support\Str;

Expand All @@ -16,12 +17,22 @@
*/
class GateCollector extends MessagesCollector
{
/** @var int */
protected $backtraceLimit = 15;

/** @var array */
protected $reflection = [];

/** @var \Illuminate\Routing\Router */
protected $router;

/**
* @param Gate $gate
*/
public function __construct(Gate $gate)
public function __construct(Gate $gate, Router $router)
{
parent::__construct('gate');
$this->router = $router;
$this->setDataFormatter(new SimpleFormatter());
$gate->after(function ($user, $ability, $result, $arguments = []) {
$this->addCheck($user, $ability, $result, $arguments);
Expand Down Expand Up @@ -82,4 +93,90 @@ public function addCheck($user, $ability, $result, $arguments = [])
'arguments' => $this->getDataFormatter()->formatVar($arguments),
], $label, false);
}

/**
* @param array $stacktrace
*
* @return array
*/
protected function getStackTraceItem($stacktrace)
{
foreach ($stacktrace as $i => $trace) {
if (!isset($trace['file'])) {
continue;
}

if (str_ends_with($trace['file'], 'Illuminate/Routing/ControllerDispatcher.php')) {
$trace = $this->findControllerFromDispatcher($trace);
} elseif (str_starts_with($trace['file'], storage_path())) {
$hash = pathinfo($trace['file'], PATHINFO_FILENAME);

if ($file = $this->findViewFromHash($hash)) {
$trace['file'] = $file;
}
}

if ($this->fileIsInExcludedPath($trace['file'])) {
continue;
}

return $trace;
}

return $stacktrace[0];
}

/**
* Find the route action file
*
* @param array $trace
* @return array
*/
protected function findControllerFromDispatcher($trace)
{
/** @var \Closure|string|array $action */
$action = $this->router->current()->getAction('uses');

if (is_string($action)) {
[$controller, $method] = explode('@', $action);

$reflection = new \ReflectionMethod($controller, $method);
$trace['file'] = $reflection->getFileName();
$trace['line'] = $reflection->getStartLine();
} elseif ($action instanceof \Closure) {
$reflection = new \ReflectionFunction($action);
$trace['file'] = $reflection->getFileName();
$trace['line'] = $reflection->getStartLine();
}

return $trace;
}

/**
* Find the template name from the hash.
*
* @param string $hash
* @return null|array
*/
protected function findViewFromHash($hash)
{
$finder = app('view')->getFinder();

if (isset($this->reflection['viewfinderViews'])) {
$property = $this->reflection['viewfinderViews'];
} else {
$reflection = new \ReflectionClass($finder);
$property = $reflection->getProperty('views');
$property->setAccessible(true);
$this->reflection['viewfinderViews'] = $property;
}

$xxh128Exists = in_array('xxh128', hash_algos());

foreach ($property->getValue($finder) as $name => $path) {
if (($xxh128Exists && hash('xxh128', 'v2' . $path) == $hash) || sha1('v2' . $path) == $hash) {
return $path;
}
}
}
}
5 changes: 5 additions & 0 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,11 @@ public function __toString(): string
if ($this->shouldCollect('gate', false)) {
try {
$this->addCollector($app->make(GateCollector::class));

if ($config->get('debugbar.options.gate.trace', false)) {
$this['gate']->collectFileTrace(true);
$this['gate']->addBacktraceExcludePaths($config->get('debugbar.options.gate.exclude_paths',[]));
}
} catch (Exception $e) {
$this->addCollectorException('Cannot add GateCollector', $e);
}
Expand Down
Loading