yooslim/legit-artisan-commands

Laravel 9 包,允许您在运行 artisan 命令前对用户进行身份验证,从而增强 artisan 命令的安全性。

v1.0.0-beta 2022-09-15 00:07 UTC

This package is auto-updated.

Last update: 2024-09-15 18:14:27 UTC


README

该包允许您作为开发者限制谁可以运行 artisan 命令,尤其是在生产环境中。例如,一个用户(管理员)只有在拥有正确的角色/权限时才允许运行命令。

它是如何工作的?

将为 artisan 用户提供一个令牌,以便在运行命令时将其作为选项使用。该令牌的大小和有效期可配置。用户可以在令牌过期或被撤销之前多次使用该令牌。

为了获取此令牌,artisan 用户必须首先在控制台中执行身份验证,如果身份验证成功,用户将收到令牌,否则将显示警告消息。

身份验证的逻辑是可定制的,开发者可以放置自己的验证规则。例如,有人可能只想授权具有“管理员”角色的用户,另一个人会检查是否有正确的权限,还有一个人会获取活动目录或外部身份验证服务等。

如何实现?

以下是一些实现步骤。

安装包

composer require yooslim/legit-artisan-commands

发布供应商配置文件

php artisan vendor:publish --provider="YOoSlim\LegitArtisanCommands\Providers\LegitCommandsServiceProvider"

编辑配置文件

  • 令牌有效期:控制台令牌的持续时间(以秒为单位)。
  • 令牌大小:要生成的字符数(必须小于255)。
  • 要忽略的环境:无需在本地环境中浪费我们的时间进行身份验证,因此可以忽略一组环境。
  • 用户模型关系:用户实体的模型名称(包括命名空间)。

运行迁移

php artisan migrate

将 ArtisanUserInterface 添加到用户模型中

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use YOoSlim\LegitArtisanCommands\Contracts\ArtisanUserInterface;

class User extends Authenticatable implements ArtisanUserInterface
{
    /**
     * Returns the user ID (the one used as a primary key)
     * 
     * @return int|string
     */
    public function getUserId(): int|string
    {
        return $this->id;
    }
}

在 AppServiceProvider.php 中自定义您的身份验证逻辑

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use YOoSlim\LegitArtisanCommands\Utils\ArtisanAuthenticationHandler;
use YOoSlim\LegitArtisanCommands\Contracts\ArtisanUserInterface;
use App\Models\User;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        ArtisanAuthenticationHandler::localUserAuthentication(function(string $username, string $password): ?ArtisanUserInterface {
            $user = User::where('email', $username)->first();

            if ($user && Hash::check($password, $user->password) && $user->hasRole('admin')) return $user;

            return null;
        });
    }
}

最后,编辑您的 artisan 命令

在您的命令中需要编辑两个主要部分

  1. 添加 LegitArtisanCommandSignature 特性,它将通过添加 --token 选项部分来编辑您的命令签名。
  2. 将您的原始命令包装在 isAuthorized 回调函数中。
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use YOoSlim\LegitArtisanCommands\Utils\Traits\LegitArtisanCommandSignature;
use YOoSlim\LegitArtisanCommands\Facades\LegitArtisanCommand;
use YOoSlim\LegitArtisanCommands\Models\ConsoleToken;

class FilesPurgeCommand extends Command
{
    use LegitArtisanCommandSignature;

    /* ------- */

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        LegitArtisanCommand::authentify($this->option('token'))
            ->isAuthorized(function (?ConsoleToken $token) {
                // The rest of your command
            })->isNotAuthorized(fn ($exception) => $this->error($exception->getMessage()));
    }

如何使用它!

  1. 首先,通过提供您的凭据请求令牌。

php artisan console:authentication --username="admin@domain.com"

这将提示一个随机令牌。

  1. 然后,每次使用受保护的 artisan 命令时,请包括 --token 选项。

php artisan MyCommand:MyAction --token="*"

享受吧:D

请告诉我如果有任何模糊不清、难以理解或错误之处。我很乐意澄清或修复它。