2

我想知道是否可以扩展或替换该php artisan tinker命令,因此它首先要求进行身份验证,以此作为看门人可以使用它的一种方式。

我尝试了以下方法:

<?php

namespace App\Console\Commands;

use Laravel\Tinker\Console\TinkerCommand;
use Illuminate\Support\Facades\Auth;

class Tinker extends TinkerCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'tinker';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $email      = $this->ask('Login (email)');
        $password   = $this->secret('Password for '.$email);

        if (!Auth::attempt(compact('email', 'password'))) {
            $this->error('Invalid Credentials.');
            return;
        }

        if (Auth::user()->cannot('use-tinker')) {
            $this->error('Unauthorized.');
            return;
        }

        parent::handle();
    }
}

但我收到一个错误,因为我没有包含使用的 'include' 参数TinkerCommand@handle

    public function handle()
    {
        $this->getApplication()->setCatchExceptions(false);

        $config = new Configuration([
            'updateCheck' => 'never',
        ]);

        $config->getPresenter()->addCasters(
            $this->getCasters()
        );

        $shell = new Shell($config);
        $shell->addCommands($this->getCommands());
        $shell->setIncludes($this->argument('include')); # <-------- include argument

        if (isset($_ENV['COMPOSER_VENDOR_DIR'])) {
            $path = $_ENV['COMPOSER_VENDOR_DIR'];
        } else {
            $path = $this->getLaravel()->basePath().DIRECTORY_SEPARATOR.'vendor';
        }

        $path .= '/composer/autoload_classmap.php';

        $loader = ClassAliasAutoloader::register($shell, $path);

        try {
            $shell->run();
        } finally {
            $loader->unregister();
        }
    }

我不确定 include 参数是关于什么的。我尝试转储它,它是一个空数组。在这一点上,我想知道是否有更好的方法。

4

1 回答 1

7

如果用户能够运行php artisan tinker,他还能够:

  • 查看项目的源代码。他可能也可以编辑它,但在适当的文件权限下可能不是这样

  • 查看您的.env,包含您的数据库凭据和其他敏感信息,例如 api 密钥

我不确定将 tinker 内部的访问限制为已经拥有如此多特权和可能性的用户是否真的有用。例如,他可以编辑您的数据库users表以授予由他自己控制的用户访问权限,或者他可以编辑源代码以允许访问。

这是问题的一点可视化:

在此处输入图像描述


如果您无论如何都想这样做,我不会扩展 TinkerCommand,而是扩展基础Command,然后在身份验证后运行 tinker comman。

public function handle() {
  
  // Do your own verification

  $this->runCommand(TinkerCommand::class, [], $this->output);
  return;
}
于 2020-08-24T14:57:32.800 回答