laravel-ready/license-server

v3.0.0 2024-06-04 17:08 UTC

This package is auto-updated.

Last update: 2024-09-04 17:36:07 UTC


README

EgoistDeveloper Laravel License Server

Stable Version Unstable Version Total Downloads License

✅ 版本兼容性

📂 关于

许可服务器包,这是一个 Laravel 包,允许您管理 Laravel 项目的许可。您可以使用它与任何产品或服务配合使用。许可服务器配备了通用的许可管理系统,允许您以简单易用的方式管理您的许可。只需将许可关系添加到任何产品模型,然后您就可以在您的逻辑中进行操作。

此包需要 license-connector 包。 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

添加具有 LaravelReady\LicenseServer\Events\LicenseChecked 命名空间的类 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

域名验证

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

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

在开发中,您可能可以使用例如 example.test 等域名,但您将无法通过域名验证,因为 test 不是一个有效的TLD。

⚠️ 注意

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