增加 MQTT 发布授权
This commit is contained in:
parent
8a3ba3296a
commit
2e42b2c952
@ -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', '取消信任完成。');
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
27
app/Models/ModuleAllow.php
Normal file
27
app/Models/ModuleAllow.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class ModuleAllow extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'module_id',
|
||||
'allowed_module_id',
|
||||
];
|
||||
|
||||
public function module(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Module::class);
|
||||
}
|
||||
|
||||
public function allowed_module(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Module::class, 'allowed_module_id');
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('module_allows', function (Blueprint $table) {
|
||||
$table->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');
|
||||
}
|
||||
};
|
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('work_orders', function (Blueprint $table) {
|
||||
//
|
||||
|
||||
// 删除外键
|
||||
$table->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) {
|
||||
//
|
||||
});
|
||||
}
|
||||
};
|
55
resources/views/admin/modules/allows.blade.php
Normal file
55
resources/views/admin/modules/allows.blade.php
Normal file
@ -0,0 +1,55 @@
|
||||
@extends('layouts.admin')
|
||||
|
||||
@section('title', '模块订阅授权')
|
||||
|
||||
@section('content')
|
||||
<h3>授权</h3>
|
||||
<p>允许多个模块之间互相发布消息。</p>
|
||||
<a href="{{ route('admin.modules.show', $module) }}">查看</a>
|
||||
<a href="{{ route('admin.modules.edit', $module) }}">编辑</a>
|
||||
|
||||
<div class="overflow-auto mt-3">
|
||||
<table class="table table-hover">
|
||||
<thead>
|
||||
<th>模块 ID</th>
|
||||
<th>显示名称</th>
|
||||
<th>操作</th>
|
||||
</thead>
|
||||
|
||||
<tbody>
|
||||
@foreach ($allows as $allow)
|
||||
<tr>
|
||||
<td>
|
||||
{{ $allow->allowed_module_id }}
|
||||
</td>
|
||||
|
||||
<td>
|
||||
{{ $allow->allowed_module->name }}
|
||||
</td>
|
||||
<td>
|
||||
<form method="POST" action="{{ route('admin.modules.allows.destroy', [$module, $allow]) }}">
|
||||
@method('delete')
|
||||
@csrf
|
||||
<button type="submit" class="btn btn-danger btn-sm">删除</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<form action="{{ route('admin.modules.allows.store', $module) }}" method="POST">
|
||||
@csrf
|
||||
|
||||
<div class="form-group">
|
||||
<label for="allowed_module_id">另一个 模块</label>
|
||||
<input type="text" class="form-control" id="allowed_module_id" name="allowed_module_id"/>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="form-group mt-3">
|
||||
<button type="submit" class="btn btn-primary">提交</button>
|
||||
</div>
|
||||
</form>
|
||||
@endsection
|
@ -5,6 +5,7 @@
|
||||
@section('content')
|
||||
<h3>{{ $module->name }}</h3>
|
||||
<a class="mt-3" href="{{ route('admin.modules.show', $module) }}">查看</a>
|
||||
<a class="mt-3" href="{{ route('admin.modules.allows', $module) }}">MQTT 授权</a>
|
||||
|
||||
<form method="POST" action="{{ route('admin.modules.update', $module)}}">
|
||||
@csrf
|
||||
|
@ -28,6 +28,7 @@
|
||||
<td>
|
||||
<a href="{{ route('admin.modules.show', $module) }}" class="btn btn-primary btn-sm">查看</a>
|
||||
<a href="{{ route('admin.modules.edit', $module) }}" class="btn btn-primary btn-sm">编辑</a>
|
||||
<a href="{{ route('admin.modules.allows', $module) }}" class="btn btn-primary btn-sm">MQTT 授权</a>
|
||||
</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
|
@ -6,6 +6,7 @@
|
||||
<h3>{{ $module->name }}</h3>
|
||||
<p>状态: {{ $module->status }}</p>
|
||||
<a class="mt-3" href="{{ route('admin.modules.edit', $module) }}">编辑</a>
|
||||
<a class="mt-3" href="{{ route('admin.modules.allows', $module) }}">MQTT 授权</a>
|
||||
<h4 class="mt-2">收益</h4>
|
||||
<div>
|
||||
<x-module-earning :module="$module" />
|
||||
|
@ -24,8 +24,15 @@
|
||||
'middleware' => 'auth:admin',
|
||||
], function () {
|
||||
Route::resource('admins', AdminController::class)->except('show');
|
||||
|
||||
Route::resource('users', UserController::class)->only(['index', 'show', 'edit', 'update']);
|
||||
|
||||
Route::resource('modules', ModuleController::class);
|
||||
|
||||
Route::get('modules/{module}/allows', [ModuleController::class, 'allows'])->name('modules.allows');
|
||||
Route::post('modules/{module}/allows', [ModuleController::class, 'allows_store'])->name('modules.allows.store');
|
||||
Route::delete('modules/{module}/allows/{allow}', [ModuleController::class, 'allows_destroy'])->name('modules.allows.destroy');
|
||||
|
||||
Route::resource('applications', ApplicationController::class);
|
||||
Route::resource('hosts', HostController::class)->only(['index', 'edit', 'update', 'destroy']);
|
||||
Route::resource('work-orders', WorkOrderController::class)->only(['index', 'show', 'edit', 'update', 'destroy']);
|
||||
|
Loading…
Reference in New Issue
Block a user