Added laravel/passport, assign ftn addresses to nodes
This commit is contained in:
@@ -2,12 +2,21 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\Domain;
|
||||
use App\Models\{Address,Domain,Zone};
|
||||
|
||||
class DomainController extends Controller
|
||||
{
|
||||
public const NODE_ZC = 1<<1; // Zone
|
||||
public const NODE_RC = 1<<2; // Region
|
||||
public const NODE_NC = 1<<3; // Host
|
||||
public const NODE_HC = 1<<4; // Hub
|
||||
|
||||
// http://ftsc.org/docs/frl-1002.001
|
||||
public const NUMBER_MAX = 0x7fff;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->middleware('auth');
|
||||
@@ -40,6 +49,69 @@ class DomainController extends Controller
|
||||
->with('o',$o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the hosts for a zone of a particular region (or not)
|
||||
*
|
||||
* @param Zone $o
|
||||
* @param int $region
|
||||
* @return Collection
|
||||
*/
|
||||
public function api_hosts(Zone $o,int $region): Collection
|
||||
{
|
||||
$oo = Address::where('role',self::NODE_NC)
|
||||
->where('zone_id',$o->id)
|
||||
->when($region,function($query,$region) { return $query->where('region_id',$region)->where('node_id','<>',0); })
|
||||
->when((! $region),function($query) use ($region) { return $query->whereNull('region_id'); })
|
||||
->where('point_id',0)
|
||||
->with(['system'])
|
||||
->get();
|
||||
|
||||
return $oo->map(function($item) {
|
||||
return ['id'=>$item->host_id,'value'=>sprintf('%s %s',$item->ftn,$item->system->name)];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all the hubs for a host
|
||||
*
|
||||
* @param Zone $o
|
||||
* @param int $host
|
||||
* @return Collection
|
||||
*/
|
||||
public function api_hubs(Zone $o,int $host): Collection
|
||||
{
|
||||
$oo = Address::where('role',self::NODE_HC)
|
||||
->where('zone_id',$o->id)
|
||||
->when($host,function($query,$host) { return $query->where('host_id',$host)->where('node_id','<>',0); })
|
||||
->with(['system'])
|
||||
->get();
|
||||
|
||||
return $oo->map(function($item) {
|
||||
return ['id'=>$item->host_id,'value'=>sprintf('%s %s',$item->ftn,$item->system->name)];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the regions for a zone
|
||||
*
|
||||
* @param Zone $o
|
||||
* @return Collection
|
||||
*/
|
||||
public function api_regions(Zone $o): Collection
|
||||
{
|
||||
$oo = Address::where('role',self::NODE_RC)
|
||||
->where('zone_id',$o->id)
|
||||
->where('node_id',0)
|
||||
->where('point_id',0)
|
||||
->orderBy('region_id')
|
||||
->with(['system'])
|
||||
->get();
|
||||
|
||||
return $oo->map(function($item) {
|
||||
return ['id'=>$item->region_id,'value'=>sprintf('%s %s',$item->ftn,$item->system->location)];
|
||||
});
|
||||
}
|
||||
|
||||
public function home()
|
||||
{
|
||||
return view('domain.home');
|
||||
|
@@ -4,7 +4,8 @@ namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
use App\Models\System;
|
||||
use App\Models\{Address,System};
|
||||
use App\Rules\TwoByteInteger;
|
||||
|
||||
class SystemController extends Controller
|
||||
{
|
||||
@@ -13,6 +14,171 @@ class SystemController extends Controller
|
||||
$this->middleware('auth');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an address to a system
|
||||
*
|
||||
* @param Request $request
|
||||
* @param System $o
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function add_address(Request $request,System $o)
|
||||
{
|
||||
$this->authorize('admin',$o);
|
||||
session()->flash('add_address',TRUE);
|
||||
|
||||
$request->validate([
|
||||
'action' => 'required|in:region,host,node',
|
||||
'zone_id' => 'required|exists:zones,id',
|
||||
]);
|
||||
|
||||
switch ($request->post('action')) {
|
||||
case 'region':
|
||||
$request->validate([
|
||||
'region_id_new' => [
|
||||
'required',
|
||||
new TwoByteInteger,
|
||||
function ($attribute,$value,$fail) {
|
||||
// Check that the region doesnt already exist
|
||||
$o = Address::where(function($query) use ($value) {
|
||||
return $query->where('region_id',$value)
|
||||
->whereNULL('host_id')
|
||||
->where('node_id',0)
|
||||
->where('point_id',0)
|
||||
->where('role',DomainController::NODE_RC);
|
||||
})
|
||||
// Check that a host doesnt already exist
|
||||
->orWhere(function($query) use ($value) {
|
||||
return $query->where('host_id',$value)
|
||||
->where('point_id',0)
|
||||
->where('role',DomainController::NODE_NC);
|
||||
});
|
||||
|
||||
if ($o->count()) {
|
||||
$fail('Region or host already exists');
|
||||
}
|
||||
},
|
||||
],
|
||||
]);
|
||||
|
||||
$oo = new Address;
|
||||
$oo->zone_id = $request->post('zone_id');
|
||||
$oo->region_id = $request->post('region_id_new');
|
||||
$oo->node_id = 0;
|
||||
$oo->point_id = 0;
|
||||
$oo->role = DomainController::NODE_RC;
|
||||
$oo->active = TRUE;
|
||||
|
||||
$o->addresses()->save($oo);
|
||||
break;
|
||||
|
||||
case 'host':
|
||||
$request->validate([
|
||||
'region_id' => ['nullable',new TwoByteInteger],
|
||||
'host_id_new' => [
|
||||
'required',
|
||||
new TwoByteInteger,
|
||||
function ($attribute,$value,$fail) {
|
||||
// Check that the region doesnt already exist
|
||||
$o = Address::where(function($query) use ($value) {
|
||||
return $query->where('region_id',$value)
|
||||
->whereNULL('host_id')
|
||||
->where('node_id',0)
|
||||
->where('point_id',0)
|
||||
->where('role',DomainController::NODE_RC);
|
||||
})
|
||||
// Check that a host doesnt already exist
|
||||
->orWhere(function($query) use ($value) {
|
||||
return $query->where('host_id',$value)
|
||||
->where('point_id',0)
|
||||
->where('role',DomainController::NODE_NC);
|
||||
});
|
||||
|
||||
if ($o->count()) {
|
||||
$fail('Region or host already exists');
|
||||
}
|
||||
},
|
||||
],
|
||||
'node_id_new' => [
|
||||
'required',
|
||||
new TwoByteInteger,
|
||||
function ($attribute,$value,$fail) use ($request) {
|
||||
// Check that the region doesnt already exist
|
||||
$o = Address::where(function($query) use ($request,$value) {
|
||||
return $query
|
||||
->where('host_id',$request->post('host_id_new'))
|
||||
->where('node_id',$value)
|
||||
->where('point_id',0)
|
||||
->where('role',DomainController::NODE_RC);
|
||||
});
|
||||
|
||||
if ($o->count()) {
|
||||
$fail('Host already exists');
|
||||
}
|
||||
},
|
||||
]
|
||||
]);
|
||||
|
||||
$oo = new Address;
|
||||
$oo->zone_id = $request->post('zone_id');
|
||||
$oo->region_id = ($x=$request->post('region_id')) == 'no' ? NULL : $x;
|
||||
$oo->host_id = $request->post('host_id_new');
|
||||
$oo->node_id = $request->post('node_id_new');
|
||||
$oo->point_id = 0;
|
||||
$oo->role = DomainController::NODE_NC;
|
||||
$oo->active = TRUE;
|
||||
|
||||
$o->addresses()->save($oo);
|
||||
break;
|
||||
|
||||
case 'node':
|
||||
$request->validate([
|
||||
'region_id' => ['nullable',new TwoByteInteger],
|
||||
'host_id' => ['nullable',new TwoByteInteger],
|
||||
'node_id' => [
|
||||
'required',
|
||||
new TwoByteInteger,
|
||||
function ($attribute,$value,$fail) use ($request) {
|
||||
// Check that the region doesnt already exist
|
||||
$o = Address::where(function($query) use ($request,$value) {
|
||||
return $query
|
||||
->where('host_id',$request->post('host_id_new'))
|
||||
->where('node_id',$value)
|
||||
->where('point_id',0)
|
||||
->where('role',DomainController::NODE_RC);
|
||||
});
|
||||
|
||||
if ($o->count()) {
|
||||
$fail('Host already exists');
|
||||
}
|
||||
},
|
||||
],
|
||||
'point_id' => ['required',function($attribute,$value,$fail) {
|
||||
if (! is_numeric($value) || $value > DomainController::NUMBER_MAX)
|
||||
$fail(sprintf('Point numbers must be between 0 and %d',DomainController::NUMBER_MAX));
|
||||
}],
|
||||
'hub' => 'required|boolean',
|
||||
]);
|
||||
|
||||
$oo = new Address;
|
||||
$oo->zone_id = $request->post('zone_id');
|
||||
$oo->region_id = ($x=$request->post('region_id')) == 'no' ? NULL : $x;
|
||||
$oo->host_id = $request->post('host_id');
|
||||
$oo->node_id = $request->post('node_id');
|
||||
$oo->point_id = $request->post('point_id');
|
||||
$oo->role = (! $oo->point_id) && $request->post('hub') ? DomainController::NODE_HC : NULL;
|
||||
$oo->active = TRUE;
|
||||
|
||||
$o->addresses()->save($oo);
|
||||
break;
|
||||
|
||||
default:
|
||||
return redirect()->back()->withErrors(['action'=>'Unknown action: '.$request->post('action')]);
|
||||
}
|
||||
|
||||
return redirect()->to(sprintf('ftn/system/addedit/%d',$o->id));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or edit a node
|
||||
*/
|
||||
|
@@ -35,6 +35,7 @@ class Kernel extends HttpKernel
|
||||
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
|
||||
\App\Http\Middleware\VerifyCsrfToken::class,
|
||||
\Illuminate\Routing\Middleware\SubstituteBindings::class,
|
||||
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
|
||||
],
|
||||
|
||||
'api' => [
|
||||
|
52
app/Models/Address.php
Normal file
52
app/Models/Address.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use App\Http\Controllers\DomainController;
|
||||
|
||||
class Address extends Model
|
||||
{
|
||||
/* RELATIONS */
|
||||
|
||||
public function system()
|
||||
{
|
||||
return $this->belongsTo(System::class);
|
||||
}
|
||||
|
||||
public function zone()
|
||||
{
|
||||
return $this->belongsTo(Zone::class);
|
||||
}
|
||||
|
||||
/* ATTRIBUTES */
|
||||
|
||||
/**
|
||||
* Render the node name in full 5D
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getFTNAttribute()
|
||||
{
|
||||
return sprintf('%d:%d/%d.%d@%s',$this->zone->zone_id,$this->host_id ?: $this->region_id,$this->node_id,$this->point_id,$this->zone->domain->name);
|
||||
}
|
||||
|
||||
public function getRoleAttribute($value)
|
||||
{
|
||||
switch ($value) {
|
||||
case DomainController::NODE_ZC;
|
||||
return 'Zone';
|
||||
case DomainController::NODE_RC;
|
||||
return 'Region';
|
||||
case DomainController::NODE_NC;
|
||||
return 'Host';
|
||||
case DomainController::NODE_HC;
|
||||
return 'Hub';
|
||||
case NULL:
|
||||
return 'Node';
|
||||
default:
|
||||
return '?';
|
||||
}
|
||||
}
|
||||
}
|
@@ -15,5 +15,14 @@ class System extends Model
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
public function addresses()
|
||||
{
|
||||
return $this->hasMany(Address::class)
|
||||
->orderBy('region_id')
|
||||
->orderBy('host_id')
|
||||
->orderBy('node_id')
|
||||
->orderBy('point_id');
|
||||
}
|
||||
|
||||
/* CASTS */
|
||||
}
|
@@ -6,40 +6,68 @@ use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
|
||||
/**
|
||||
* Class User
|
||||
*
|
||||
* User Roles:
|
||||
* + Site Admin
|
||||
* + ZC - For Domain/Zone
|
||||
* + RC - For sub portion of a Domain/Zone (aka Region)
|
||||
* + Host Admin - For sub portion of a Region
|
||||
* + Hub Admin - For a sub portion of a Hosts system
|
||||
* + Sysop - Individual system
|
||||
* + Guest
|
||||
*
|
||||
* @package App\Models
|
||||
*/
|
||||
class User extends Authenticatable implements MustVerifyEmail
|
||||
{
|
||||
use HasFactory, Notifiable;
|
||||
use HasFactory,Notifiable,HasApiTokens;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
/**
|
||||
* The attributes that should be hidden for arrays.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
|
||||
protected $dates = ['last_on'];
|
||||
protected $dates = ['last_on'];
|
||||
|
||||
/* GENERAL METHODS */
|
||||
|
||||
/**
|
||||
* See if the user is already a member of the chosen network
|
||||
*
|
||||
* @param Domain $o
|
||||
* @return bool
|
||||
*/
|
||||
public function isMember(Domain $o): bool
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
@@ -12,6 +12,11 @@ class Zone extends Model
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
public function addresses()
|
||||
{
|
||||
return $this->hasMany(Address::class);
|
||||
}
|
||||
|
||||
public function domain()
|
||||
{
|
||||
return $this->belongsTo(Domain::class);
|
||||
|
@@ -4,6 +4,7 @@ namespace App\Providers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Laravel\Passport\Passport;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -14,7 +15,7 @@ class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
//
|
||||
Passport::ignoreMigrations();
|
||||
}
|
||||
|
||||
/**
|
||||
|
33
app/Rules/TwoByteInteger.php
Normal file
33
app/Rules/TwoByteInteger.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
use App\Http\Controllers\DomainController;
|
||||
|
||||
class TwoByteInteger implements Rule
|
||||
{
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
* This will check that a number used for zone, net, host is between 1 and DomainController::NUMBER_MAX.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
return (is_numeric($value) && ($value > 0) && ($value < DomainController::NUMBER_MAX)) || ($value === 'no');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return sprintf('The number must be between 1 and %d.',DomainController::NUMBER_MAX);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user