移除 Provider
This commit is contained in:
parent
b347bf980b
commit
1d49de6ead
@ -5,7 +5,7 @@
|
|||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
class ProviderController extends Controller
|
class ModuleController extends Controller
|
||||||
{
|
{
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
@ -46,23 +46,24 @@ public function index()
|
|||||||
* @param \Illuminate\Http\Request $request
|
* @param \Illuminate\Http\Request $request
|
||||||
* @return \Illuminate\Http\Response
|
* @return \Illuminate\Http\Response
|
||||||
*/
|
*/
|
||||||
public function store(Request $request, Status $status)
|
public function store(Request $request, Status $server)
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
$request->validate([
|
$request->validate([
|
||||||
'name' => 'required|string',
|
'name' => 'required|string',
|
||||||
'ip' => 'sometimes|ip',
|
'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,
|
'name' => $request->name,
|
||||||
'ip' => $request->ip,
|
'ip' => $request->ip,
|
||||||
'status' => $request->status,
|
'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
|
* @param int $id
|
||||||
* @return \Illuminate\Http\Response
|
* @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([
|
$request->validate([
|
||||||
'name' => 'sometimes|string',
|
'name' => 'sometimes|string',
|
||||||
'ip' => 'sometimes|ip',
|
'ip' => 'sometimes|ip',
|
||||||
'status' => 'sometimes|string',
|
'status' => 'sometimes|in:online,offline,maintenance',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$status->provider()->update([
|
$server->update($request->only(['name', 'ip', 'status']));
|
||||||
'name' => $request->name,
|
|
||||||
'ip' => $request->ip,
|
|
||||||
'status' => $request->status,
|
|
||||||
]);
|
|
||||||
|
|
||||||
return $this->updated($status);
|
return $this->updated($server);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
namespace App\Models\Module;
|
namespace App\Models\Module;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
|
||||||
class Module extends Model
|
class Module extends Model
|
||||||
{
|
{
|
||||||
@ -11,8 +12,25 @@ class Module extends Model
|
|||||||
|
|
||||||
protected $table = 'modules';
|
protected $table = 'modules';
|
||||||
|
|
||||||
|
// primary key
|
||||||
|
public $incrementing = false;
|
||||||
|
protected $keyType = 'string';
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'name',
|
'id',
|
||||||
'type',
|
'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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -15,12 +15,24 @@ class Status extends Model
|
|||||||
'name',
|
'name',
|
||||||
'ip',
|
'ip',
|
||||||
'status',
|
'status',
|
||||||
'provider_id',
|
'module_id',
|
||||||
];
|
];
|
||||||
|
|
||||||
// scope
|
// 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.');
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,7 +82,7 @@
|
|||||||
|
|
||||||
'providers' => [
|
'providers' => [
|
||||||
'driver' => 'eloquent',
|
'driver' => 'eloquent',
|
||||||
'model' => App\Models\Module\Provider::class,
|
'model' => App\Models\Module\Module::class,
|
||||||
],
|
],
|
||||||
|
|
||||||
// 'users' => [
|
// 'users' => [
|
||||||
|
@ -15,22 +15,21 @@
|
|||||||
public function up()
|
public function up()
|
||||||
{
|
{
|
||||||
Schema::create('modules', function (Blueprint $table) {
|
Schema::create('modules', function (Blueprint $table) {
|
||||||
$table->id();
|
$table->string('id')->index()->primary()->unique();
|
||||||
|
|
||||||
// name
|
|
||||||
$table->string('name')->index();
|
|
||||||
|
|
||||||
// type
|
// type
|
||||||
$table->string('type')->index();
|
$table->string('type')->index();
|
||||||
|
|
||||||
$table->timestamps();
|
// api token
|
||||||
|
$table->string('api_token')->nullable()->unique()->index();
|
||||||
});
|
});
|
||||||
|
|
||||||
// if env is local
|
// if env is local
|
||||||
if (env('APP_ENV') == 'local') {
|
if (env('APP_ENV') == 'local') {
|
||||||
$module = [
|
$module = [
|
||||||
'name' => 'Example Model',
|
'id' => 'Example Model',
|
||||||
'type' => 'test',
|
'type' => 'test',
|
||||||
|
'api_token' => '123456'
|
||||||
];
|
];
|
||||||
|
|
||||||
Module::create($module);
|
Module::create($module);
|
||||||
|
@ -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');
|
|
||||||
}
|
|
||||||
};
|
|
@ -22,8 +22,8 @@ public function up()
|
|||||||
$table->string('name')->index();
|
$table->string('name')->index();
|
||||||
|
|
||||||
// provider id
|
// provider id
|
||||||
$table->unsignedBigInteger('provider_id')->index();
|
$table->string('module_id')->index();
|
||||||
$table->foreign('provider_id')->references('id')->on('providers')->onDelete('cascade');
|
// $table->foreign('module_id')->references('id')->on('modules')->onDelete('set null');
|
||||||
|
|
||||||
// user_id
|
// user_id
|
||||||
$table->unsignedBigInteger('user_id')->index();
|
$table->unsignedBigInteger('user_id')->index();
|
||||||
|
@ -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');
|
|
||||||
}
|
|
||||||
};
|
|
@ -31,9 +31,8 @@ public function up()
|
|||||||
$table->unsignedBigInteger('user_id')->index();
|
$table->unsignedBigInteger('user_id')->index();
|
||||||
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
|
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
|
||||||
|
|
||||||
// provider module id
|
$table->string('module_id')->index();
|
||||||
$table->unsignedBigInteger('provider_module_id')->index();
|
$table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
|
||||||
$table->foreign('provider_module_id')->references('id')->on('provider_modules')->onDelete('cascade');
|
|
||||||
|
|
||||||
// host id
|
// host id
|
||||||
$table->unsignedBigInteger('host_id')->index();
|
$table->unsignedBigInteger('host_id')->index();
|
||||||
|
@ -23,9 +23,8 @@ public function up()
|
|||||||
|
|
||||||
$table->string('status');
|
$table->string('status');
|
||||||
|
|
||||||
// provider foreign key
|
$table->string('module_id')->index();
|
||||||
$table->unsignedBigInteger('provider_id')->index();
|
$table->foreign('module_id')->references('id')->on('modules')->onDelete('cascade');
|
||||||
$table->foreign('provider_id')->references('id')->on('providers')->onDelete('cascade');
|
|
||||||
|
|
||||||
|
|
||||||
$table->timestamps();
|
$table->timestamps();
|
||||||
|
@ -3,7 +3,8 @@
|
|||||||
use App\Http\Controllers\Remote;
|
use App\Http\Controllers\Remote;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
Route::name('remote.')->middleware(['api', 'auth:remote'])->group(function () {
|
Route::name('remote.')->middleware(['api'])->group(function () {
|
||||||
Route::apiResource('providers', Remote\ProviderController::class)->only(['index']);
|
Route::apiResource('providers', Remote\ModuleController::class)->only(['index']);
|
||||||
|
Route::apiResource('servers', Remote\ServerController::class);
|
||||||
// Route::apiResource('users', Controllers\User\UserController::class);
|
// Route::apiResource('users', Controllers\User\UserController::class);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user