有办法守护幼虫吗?

问题描述

我想知道是否可以扩展或替换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();
    }
}

但是出现错误,因为我没有包含TinkerCommand@handle

使用的'include'参数
    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参数的含义。我尝试转储它,这是一个空数组。在这一点上,我想知道是否有更好的方法

解决方法

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

  • 查看项目的源代码。他也许也可以编辑它,但是具有适当的文件权限可能不是这种情况

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

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

这是问题的一点可视化:

enter image description here


如果仍然要执行此操作,则无需扩展TinkerCommand,而是扩展基础Command,然后在身份验证后运行tinker comman。

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

  $this->runCommand(TinkerCommand::class,[],$this->output);
  return;
}