diff --git a/app/Http/Controllers/Admin/ModuleController.php b/app/Http/Controllers/Admin/ModuleController.php index 28c5a99..963e3d9 100644 --- a/app/Http/Controllers/Admin/ModuleController.php +++ b/app/Http/Controllers/Admin/ModuleController.php @@ -5,6 +5,7 @@ use App\Http\Controllers\Controller; use App\Models\Host; use App\Models\Module; +use App\Models\ModuleAllow; use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Http\Response; @@ -158,4 +159,32 @@ private function rules(): array ]; } + public function allows(Module $module) + { + $allows = ModuleAllow::where('module_id', $module->id)->with('allowed_module')->paginate(50); + + return view('admin.modules.allows', compact('module', 'allows')); + } + + public function allows_store(Request $request, Module $module) + { + $request->validate([ + 'allowed_module_id' => 'required|string|max:255|exists:modules,id', + ]); + + ModuleAllow::where('module_id', $module->id)->where('allowed_module_id', $request->allow_module_id)->firstOrCreate([ + 'module_id' => $module->id, + 'allowed_module_id' => $request->get('allowed_module_id'), + ]); + + return back()->with('success', '已信任该模块。'); + } + + public function allows_destroy(Module $module, ModuleAllow $allow) + { + $allow->delete(); + + return redirect()->route('admin.modules.allows', $module)->with('success', '取消信任完成。'); + } + } diff --git a/app/Http/Controllers/Application/MqttAuthController.php b/app/Http/Controllers/Application/MqttAuthController.php index da9fcee..4db0de3 100644 --- a/app/Http/Controllers/Application/MqttAuthController.php +++ b/app/Http/Controllers/Application/MqttAuthController.php @@ -4,6 +4,7 @@ use App\Http\Controllers\Controller; use App\Models\Module; +use App\Models\ModuleAllow; use Illuminate\Http\Request; class MqttAuthController extends Controller @@ -98,8 +99,12 @@ public function authorization(Request $request) // 设备只能在自己的模块下发布消息 if ($action == 'publish') { if ($topics[0] !== $module_id) { - // Log::debug('设备只能在自己的模块下发布消息'); - return $this->deny(); + // 但是,在拒绝之前,应该检查一下,是否有允许的模块 + $allow = ModuleAllow::where('module_id', $topics[0])->where('allowed_module_id', $module_id)->exists(); + + if (!$allow) { + return $this->deny(); + } } } diff --git a/app/Models/ModuleAllow.php b/app/Models/ModuleAllow.php new file mode 100644 index 0000000..db4d599 --- /dev/null +++ b/app/Models/ModuleAllow.php @@ -0,0 +1,27 @@ +belongsTo(Module::class); + } + + public function allowed_module(): BelongsTo + { + return $this->belongsTo(Module::class, 'allowed_module_id'); + } +} diff --git a/database/migrations/2022_12_03_130419_create_module_allows_table.php b/database/migrations/2022_12_03_130419_create_module_allows_table.php new file mode 100644 index 0000000..b7eef7d --- /dev/null +++ b/database/migrations/2022_12_03_130419_create_module_allows_table.php @@ -0,0 +1,40 @@ +id(); + + $table->string('module_id')->index(); + $table->foreign('module_id')->references('id')->on('modules')->cascadeOnDelete()->cascadeOnUpdate(); + + + $table->string('allowed_module_id')->index(); + $table->foreign('allowed_module_id')->references('id')->on('modules')->cascadeOnDelete()->cascadeOnUpdate(); + + + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('module_allows'); + } +}; diff --git a/database/migrations/2022_12_03_133136_add_cascade_update_to_workorders_table.php b/database/migrations/2022_12_03_133136_add_cascade_update_to_workorders_table.php new file mode 100644 index 0000000..8e280d3 --- /dev/null +++ b/database/migrations/2022_12_03_133136_add_cascade_update_to_workorders_table.php @@ -0,0 +1,38 @@ +dropForeign('work_orders_module_id_foreign'); + + $table->foreign('module_id')->references('id')->on('modules')->cascadeOnDelete()->cascadeOnUpdate(); + + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('workorders', function (Blueprint $table) { + // + }); + } +}; diff --git a/resources/views/admin/modules/allows.blade.php b/resources/views/admin/modules/allows.blade.php new file mode 100644 index 0000000..652d440 --- /dev/null +++ b/resources/views/admin/modules/allows.blade.php @@ -0,0 +1,55 @@ +@extends('layouts.admin') + +@section('title', '模块订阅授权') + +@section('content') +
允许多个模块之间互相发布消息。
+ 查看 + 编辑 + +模块 ID | +显示名称 | +操作 | + + + + @foreach ($allows as $allow) +
---|---|---|
+ {{ $allow->allowed_module_id }} + | + ++ {{ $allow->allowed_module->name }} + | ++ + | +