hamasakibrain / license-server
Laravel 许可证服务器
Requires
- php: ^8.0.2
- ext-filter: *
- ext-intl: *
- ext-json: *
- illuminate/support: ^9.0
- jeremykendall/php-domain-parser: ^6.1
- laravel-ready/ultimate-support: ^1.0
- laravel/sanctum: ^2.1 || ^3.0
Requires (Dev)
- mockery/mockery: ^1.4
- orchestra/testbench: ^7.0.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-08 01:45:16 UTC
README
📂 关于
许可证服务器 是一个 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 HamasakiBrain\LicenseServer\Traits\Licensable; class Product extends Model { use HasFactory, Licensable; protected $table = 'products'; protected $fillable = [ 'name', 'description', 'price', 'image', ... ]; ... }
📌 服务方法
添加到您的命名空间列表中
use HamasakiBrain\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)。
- 其他参数是可选的,不要忘记配置配置。
- 此方法返回
HamasakiBrain\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");
您只能设置 active
、inactive
、suspended
状态。
🛠️ 自定义控制器
如果您想使用自己的许可证验证控制器,可以轻松集成。
点击查看示例!
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Event; use HamasakiBrain\LicenseServer\Models\License; use HamasakiBrain\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' ] ]
🪢 事件
🪡 LicenseChecked 事件
您可以使用连接器发送自定义数据,在许可证服务器端,您可以捕获这些自定义数据。首先,您需要为该事件创建一个监听器。
php artisan make:listener LicenseCheckedListener --event=LicenseChecked
添加类 LicenseChecked
,并使用 HamasakiBrain\LicenseServer\Events\LicenseChecked
命名空间。您可以从事件中检索自定义数据。
<?php namespace App\Listeners; use HamasakiBrain\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 或类似的源代码加密工具混淆。