aldeebhasan/multi-agents

此软件包将帮助您处理多设备操作

1.0.0 2021-12-11 19:27 UTC

This package is auto-updated.

Last update: 2024-09-12 01:14:57 UTC


README

一个PHP软件包,允许用户同时处理多个代理(设备)。此软件包允许您将任何模型与多个设备相关联,并为该设备可以使用的功能提供一种认证。

安装

使用composer安装

composer require aldeebhasan/multi-agents

完成安装过程后,您需要将软件包表迁移到数据库。运行以下命令

php artisan migrate

基本用法

设备

要标识任何特定型号可以与各种设备相关联,您可以使用HasMultiAgent特质。

class Client 
{
    use HasMultiAgent;
    ...

要创建一个新设备,您可以使用register函数。注意:uuid应对于每个设备是唯一的

$device = Device::register('uuid');

现在,您可以使用以下函数将设备与Client模型相关联/解除关联

use Aldeebhasan\MultiAgents\Models\Device

$device = Device::register('uuid');
$client = new Client();

// Link the device with the model
$client->linkDevices($device);

//you can also pass a list of device objects
$client->linkDevices([$device,$device,....]);

// Unlink the device from the model
$client->unlinkDevices($device);

//you can also pass a list of device objects
$client->unlinkDevices([$device,$device,....]);

//unlink all the devices related to specific model
$client->unlinkAll();

检索与特定型号相关联的所有设备

$client->devices

要获取设备的所有者,请使用以下。

注意:所有者可以是使用HasMultiAgent特质的任何模型,并且设备已经与其关联。

$device->owner

最后,感谢Agent软件包的作者,您可以在其中执行针对当前设备代理的一系列检查函数。您可以在这里检查与设备代理相关的所有函数此处

现在,要检索设备代理对象,您可以使用。

$device = $client->devices->first();
$device->agent

中间件

在注册任何设备后,设备对象将具有一个token属性。此属性应通过Header或作为POSTGET参数发送,参数键为Device-Token以认证已注册的设备。

要使用设备认证中间件,您需要首先将其添加到app\Http\Kernel.php中的$routeMiddleware,如下所示

'auth.device' => \Aldeebhasan\MultiAgents\Middleware\DeviceAuthenticated::class,

接下来,您必须将中间件附加到您的路由或控制器中。

//in the routes
Route::middleware(['auth.device'])->group(function () {
    Route::get('/device-info', function () {
        return getCurrentDevice();
    });
});
//in the controller
 public function __construct()
    {
        $this->middleware('auth.device';
    }

该软件包包含getCurrentDevice()辅助函数,用于检索与传递的Device-Token匹配的设备

此外,如果您只想认证与特定模型相关联的设备,则可以将模型类名传递给中间件。

// if the passed token match with any of the token
// registered for the `Client` model it wil allow
// it to pass to the inner route.
// Otherwise, DeviceAuthenticationException Exception will be thrown
Route::middleware(['auth.device:'.Client::class])->group(function () {
    Route::get('/device-info', function () {
        return getCurrentDevice();
    });
});

异常

从软件包的DeviceAuthenticated中间件可能会抛出两种类型的异常

  • DeviceAuthenticationException:当没有设备与传递的令牌匹配时。
  • DeviceExpiredTokenException:当设备令牌已过期且设备需要重新注册时。

设置

每个设备都可以有一系列与其相关的设置。

您可以为要与其特定设备集相关联的设置列表初始化,并将其附加到设备上。

use Aldeebhasan\MultiAgents\Models\Setting

$setting1 = Setting::register('key1');
$setting2 = Setting::register('key2');
//add settings with its value to specific device
$device->addSettings([
            'key1'=>'value1',
            'key2'=>'value2',
        ]);
//remove setting/s from a specific device
//single setting
$device->deleteSettings('key1'); 
//multiple settings
$device->deleteSettings(['key1','key2']); 

//to perform the two operations togather and sync the settings with their value with specific device
$device2->syncSettings([
         'key1'=>'value1',
        ]);
//the sync function will unlink all the settings doesn't much the keys,
// and link/change the  value of the settings that are appeared in the  provided list

最后,要检索与特定设备相关的所有设置

$device->getSettings(); 
//output: [
//    'key1'=>'value1',
//    'key2'=>'value2',
//  ]

许可

Laravel Multi Agent软件包根据MIT许可(MIT)授权。

安全联系信息

要报告安全漏洞,请直接联系开发者的联系电子邮件此处