amoori/license-server

Laravel 许可证服务器

dev-main 2024-07-05 10:55 UTC

This package is auto-updated.

Last update: 2024-09-05 11:23:53 UTC


README

EgoistDeveloper Laravel License Server

Stable Version Unstable Version Total Downloads License

📂 关于

许可证服务器 包,是一个 Laravel 包,允许您管理 Laravel 应用程序的许可证。您可以与任何产品或服务一起使用它。许可证服务器包含无差别的许可证管理系统,允许您以简单易行的方式管理您的许可证。只需将许可证关系添加到任何产品模型,然后您就可以在您的逻辑中工作。

此包需要 license-connector 包。 许可证连接器 是许可证服务器的客户端实现。客户端包向 许可证服务器 发送请求并获取响应。

📋 要求

PHP

此包需要 PHP 8.0 或更高版本。还需要以下扩展

如果已安装上述扩展,您可以使用 php.ini 启用它们。

Laravel

此包需要 Laravel 8.x 或更高版本。其他版本将被忽略。

📦 安装(针对主机应用程序)

通过 composer 获取

composer require laravel-ready/license-server

发布迁移并迁移

# publish migrations
php artisan vendor:publish --tag=license-server-migrations

# apply migrations
php artisan migrate --path=/database/migrations/laravel-ready/license-server

配置非常重要。您可以在 license-server.php 文件中找到它们。您应该阅读所有配置并根据您的需求进行配置。

# publish configs
php artisan vendor:publish --tag=license-server-configs

🗝️ 模型关系

每个许可证都必须有一个产品,因为我们需要知道它是什么许可证。客户端应用程序会将此信息发送到许可证服务器。然后我们可以检查许可证是否针对给定的产品有效。

产品模型可以是您想要授权的任何模型。将 Licensable 特性添加到您的产品模型中。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

use LaravelReady\LicenseServer\Traits\Licensable;

class Product extends Model
{
    use HasFactory, Licensable;

    protected $table = 'products';

    protected $fillable = [
        'name',
        'description',
        'price',
        'image',
        ...
    ];

    ...
}

📌 服务方法

将其添加到您的命名空间列表中

use LaravelReady\LicenseServer\Services\LicenseService;

和产品模型

use App\Models\Product;

addLicense

首先,我们需要了解许可证模型。此包支持两种类型的许可证模型:按域名按用户。两者都是有效的。如果您想将许可证添加到域名,必须传递 domain 参数。如果您想将许可证添加到用户,必须传递 userId 参数。此外,当您同时传递这两个参数时,您将获得域名许可证。

// get licensable product
$product = Product::find(1);
$user = User::find(1);

// add license to domain
$license = LicenseService::addLicense($product, 'example.com', $user->id);

// add license to user
$license = LicenseService::addLicense($product, null, $user->id);

// with expiration in days (see configs for defaults)
$license = LicenseService::addLicense($product, null, $user->id, 999);

// with lifetime license (see configs for defaults)
$license = LicenseService::addLicense($product, null, $user->id, null, true);

// with trial license (see configs for defaults)
$license = LicenseService::addLicense($product, null, $user->id, null, false, true);
  • 如果提供域名,则许可证将添加到该域名。如果没有提供域名,则许可证将添加到用户(在这种情况下需要用户 ID)。
  • 其他参数是可选的,不要忘记配置配置。
  • 此方法返回 LaravelReady\LicenseServer\Models\License 模型。
  • 所有许可证密钥都是 UUID 格式。

getLicenseBy*

  • getLicenseByKey:通过许可证密钥获取许可证。
    • LicenseService::getLicenseByKey(string $licenseKey)
  • getLicenseByUserId:通过用户 ID 和许可证密钥获取许可证。
    • LicenseService::getLicenseByUserId(int $userId, string $licenseKey = null)
  • getLicenseByDomain:通过域名和许可证密钥获取许可证。
    • LicenseService::getLicenseByDomain(string $domain, string $licenseKey = null)

checkLicenseStatus

// license key in uuid format
$licenseKey = "46fad906-bc51-435f-9929-db46cb4baf13";

// check license status
$licenseStatus = LicenseService::checkLicenseStatus($licenseKey);

返回 "active"、"inactive"、"suspended"、"expired"、"invalid-license-key" 和 "no-license-found"。

setLicenseStatus

// license key in uuid format
$licenseKey = "46fad906-bc51-435f-9929-db46cb4baf13";

// check license status
$licenseStatus = LicenseService::setLicenseStatus($licenseKey, "suspended");

您只能设置 activeinactivesuspended 状态。

🛠️ 自定义控制器

如果您想使用自己的许可证验证控制器,可以轻松地集成它。

点击查看示例!
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;

use LaravelReady\LicenseServer\Models\License;
use LaravelReady\LicenseServer\Events\LicenseChecked;

class LicenseController extends Controller
{
    /**
     * Custom license validation
     *
     * @param Request $request
     * @param License $license
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function licenseValidate(Request $request, License $license)
    {
        // this controller is works under 'auth:sanctum' and 'ls-license-guard' middleware
        // in this case 'auth()->user()' will be is our license

        $_license = $license->select(
            'domain',
            'license_key',
            'status',
            'expiration_date',
            'is_trial',
            'is_lifetime'
        )->where([
            ['id', '=', auth()->user()->id],
            ['is_trial', '!=', true]
        ])->first();

        $data = $request->input();

        // event will be fired after license is checked
        // this part depends to your logic, you can remove it or change it
        Event::dispatch(new LicenseChecked($_license, $data));

        $_license->appent_some_data = 'some data and date now -> ' . now();

        return $_license;
    }
}

然后您需要在您的 config/license-server.php 文件中注册这个自定义控制器。

点击查看示例!
/**
 * Custom controllers for License Server
 */
'controllers' => [
    /**
     * License validation controller
     *
     * You can use this controller to handle license validating
     *
     * See the documentation for more information.
     *
     */
    'license_validation' => [
        App\Http\Controllers\LicenseController::class,
        'licenseValidate'
    ]
]

🪢 事件

🪡 许可证检查事件

您可以使用连接器发送自定义数据,在许可证服务器端,您可以捕获这些自定义数据。首先,您需要为这个事件创建一个监听器。

php artisan make:listener LicenseCheckedListener --event=LicenseChecked

添加类 LicenseChecked,使用命名空间 LaravelReady\LicenseServer\Events\LicenseChecked。您可以从事件中检索自定义数据。

<?php

namespace App\Listeners;

use LaravelReady\LicenseServer\Events\LicenseChecked;

class LicenseCheckedListener
{
    public function __construct()
    {
        //
    }

    public function handle(LicenseChecked $event)
    {
        // $event->license,
        // $event->data,
    }
}

最后,您需要在您的 config/license-server.php 文件中注册这个监听器。

点击查看示例!
/**
    * Event listeners for License Server
    */
'event_listeners' => [
    /**
        * License checked event listener
        *
        * You can use this event to do something when a license is checked.
        * Also you can handle custom data with this listener.
        *
        * See the documentation for more information.
        *
        * Default: null
        */
    'license_checked' => App\Listeners\LicenseCheckedListener::class,
],

可用的API

可用的API包含简单的资源方法。API端点是 /api/license-server/licenses

域验证

许可证服务器使用缓存来存储公共顶级域列表。查看顶级域列表 https://data.iana.org/TLD/tlds-alpha-by-domain.txt

顶级域列表缓存将存储在 storage/license-server/iana-tld-list.txt 文件中,请不要编辑此文件。

在开发中,您可以使用像 example.test 这样的域名等,但您不会通过域验证,因为 test 不是有效的顶级域。

⚠️ 警告

  • 此软件包处于积极开发中,尚未稳定。后续版本可能会有一些变化。
  • 请记住,此软件包仅提供许可证管理和产品/客户通信。
  • 请勿将其与 ioncube 或类似的源代码加密工具混淆。