移除 Provider

This commit is contained in:
iVampireSP.com 2022-08-14 21:57:56 +08:00
parent b347bf980b
commit 1d49de6ead
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
14 changed files with 62 additions and 232 deletions

View File

@ -5,7 +5,7 @@
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ProviderController extends Controller
class ModuleController extends Controller
{
public function index()
{

View File

@ -46,23 +46,24 @@ public function index()
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Status $status)
public function store(Request $request, Status $server)
{
//
$request->validate([
'name' => 'required|string',
'ip' => 'sometimes|ip',
'status' => 'required|string',
// status only allow online or offline
'status' => 'required|in:online,offline,maintenance',
]);
$status = $status->create([
$server = $server->create([
'name' => $request->name,
'ip' => $request->ip,
'status' => $request->status,
'provider_id' => auth('remote')->id()
'module_id' => auth('remote')->id()
]);
return $this->success($status);
return $this->success($server);
}
@ -73,22 +74,18 @@ public function store(Request $request, Status $status)
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Status $status)
public function update(Request $request, Status $server)
{
// update
// only allow name,ip,status
$request->validate([
'name' => 'sometimes|string',
'ip' => 'sometimes|ip',
'status' => 'sometimes|string',
'status' => 'sometimes|in:online,offline,maintenance',
]);
$status->provider()->update([
'name' => $request->name,
'ip' => $request->ip,
'status' => $request->status,
]);
$server->update($request->only(['name', 'ip', 'status']));
return $this->updated($status);
return $this->updated($server);
}
/**

View File

@ -2,8 +2,9 @@
namespace App\Models\Module;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Module extends Model
{
@ -11,8 +12,25 @@ class Module extends Model
protected $table = 'modules';
// primary key
public $incrementing = false;
protected $keyType = 'string';
public $timestamps = false;
protected $fillable = [
'name',
'id',
'type',
'api_token'
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
// if local
if (!app()->environment('local')) {
$model->api_token = Str::random(60);
}
});
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace App\Models\Module;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Provider extends Model
{
use HasFactory;
protected $table = 'providers';
protected $fillable = [
'name',
'api_token',
];
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
// if local
if (!app()->environment('local')) {
$model->api_token = Str::random(60);
}
});
}
}

View File

@ -1,51 +0,0 @@
<?php
namespace App\Models\Module;
use Illuminate\Support\Str;
use App\Models\Module\Module;
use App\Models\Module\Provider;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class ProviderModule extends Model
{
use HasFactory;
protected $table = 'provider_modules';
protected $fillable = [
'provider_id',
'module_id',
'is_enabled',
// 'api_token'
];
public function provider()
{
return $this->belongsTo(Provider::class);
}
public function module()
{
return $this->belongsTo(Module::class);
}
public function getIsEnabledAttribute($value)
{
return (bool) $value;
}
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
// if local
if (!app()->environment('local')) {
$model->api_token = Str::random(60);
}
});
}
}

View File

@ -15,12 +15,24 @@ class Status extends Model
'name',
'ip',
'status',
'provider_id',
'module_id',
];
// scope
public function scopeProvider($query)
public function scopeModule($query)
{
return $query->where('provider_id', auth('remote')->id());
return $query->where('module_id', auth('remote')->id());
}
// when update, check owner
protected static function boot()
{
parent::boot();
static::updating(function ($model) {
if ($model->module_id !== auth('remote')->id()) {
abort(403, 'Unauthorized action.');
}
});
}
}

View File

@ -82,7 +82,7 @@
'providers' => [
'driver' => 'eloquent',
'model' => App\Models\Module\Provider::class,
'model' => App\Models\Module\Module::class,
],
// 'users' => [

View File

@ -15,22 +15,21 @@
public function up()
{
Schema::create('modules', function (Blueprint $table) {
$table->id();
// name
$table->string('name')->index();
$table->string('id')->index()->primary()->unique();
// type
$table->string('type')->index();
$table->timestamps();
// api token
$table->string('api_token')->nullable()->unique()->index();
});
// if env is local
if (env('APP_ENV') == 'local') {
$module = [
'name' => 'Example Model',
'id' => 'Example Model',
'type' => 'test',
'api_token' => '123456'
];
Module::create($module);

View File

@ -1,49 +0,0 @@
<?php
use App\Models\Module\Provider;
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('providers', function (Blueprint $table) {
$table->id();
// name
$table->string('name')->index();
// api_token
$table->string('api_token')->index()->unique();
$table->timestamps();
});
// if env is local
if (env('APP_ENV') == 'local') {
$provider = [
'name' => 'Example Provider',
'api_token' => 123456,
];
Provider::create($provider);
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('providers');
}
};

View File

@ -22,8 +22,8 @@ public function up()
$table->string('name')->index();
// provider id
$table->unsignedBigInteger('provider_id')->index();
$table->foreign('provider_id')->references('id')->on('providers')->onDelete('cascade');
$table->string('module_id')->index();
// $table->foreign('module_id')->references('id')->on('modules')->onDelete('set null');
// user_id
$table->unsignedBigInteger('user_id')->index();

View File

@ -1,65 +0,0 @@
<?php
use App\Models\Module\Module;
use App\Models\Module\Provider;
use App\Models\Module\ProviderModule;
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('provider_modules', function (Blueprint $table) {
$table->id();
// provider id (on delete cascade)
$table->unsignedBigInteger('provider_id')->index();
$table->foreign('provider_id')->references('id')->on('providers')->onDelete('cascade');
// module id
$table->unsignedBigInteger('module_id')->index();
$table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
// api_token
// $table->string('api_token')->index()->unique();
// backend url
$table->string('backend_url')->nullable();
// enabled
$table->boolean('is_enabled')->default(false)->index();
$table->timestamps();
});
// if env is local
if (env('APP_ENV') == 'local') {
$provider = [
'provider_id' => 1,
'module_id' => 1,
'is_enabled' => 1,
// 'api_token' => 123456,
];
ProviderModule::create($provider);
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('provider_modules');
}
};

View File

@ -31,9 +31,8 @@ public function up()
$table->unsignedBigInteger('user_id')->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
// provider module id
$table->unsignedBigInteger('provider_module_id')->index();
$table->foreign('provider_module_id')->references('id')->on('provider_modules')->onDelete('cascade');
$table->string('module_id')->index();
$table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
// host id
$table->unsignedBigInteger('host_id')->index();

View File

@ -23,9 +23,8 @@ public function up()
$table->string('status');
// provider foreign key
$table->unsignedBigInteger('provider_id')->index();
$table->foreign('provider_id')->references('id')->on('providers')->onDelete('cascade');
$table->string('module_id')->index();
$table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
$table->timestamps();

View File

@ -3,7 +3,8 @@
use App\Http\Controllers\Remote;
use Illuminate\Support\Facades\Route;
Route::name('remote.')->middleware(['api', 'auth:remote'])->group(function () {
Route::apiResource('providers', Remote\ProviderController::class)->only(['index']);
Route::name('remote.')->middleware(['api'])->group(function () {
Route::apiResource('providers', Remote\ModuleController::class)->only(['index']);
Route::apiResource('servers', Remote\ServerController::class);
// Route::apiResource('users', Controllers\User\UserController::class);
});