Work on registration of existing systems to users
This commit is contained in:
@@ -7,11 +7,13 @@ use Illuminate\Support\Collection;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Illuminate\Support\ViewErrorBag;
|
||||
|
||||
use App\Http\Requests\SystemRegister;
|
||||
use App\Models\{Address,Echoarea,System,SystemZone,Zone};
|
||||
use App\Models\{Address,Echoarea,Setup,System,SystemZone,Zone};
|
||||
use App\Notifications\AddressLink;
|
||||
use App\Rules\{FidoInteger,TwoByteInteger};
|
||||
|
||||
class SystemController extends Controller
|
||||
@@ -258,6 +260,8 @@ class SystemController extends Controller
|
||||
*/
|
||||
public function add_edit(SystemRegister $request,System $o)
|
||||
{
|
||||
$this->authorize('update',$o);
|
||||
|
||||
if ($request->post()) {
|
||||
foreach (['name','location','sysop','hold','phone','address','port','active','method','notes','mailer_type','mailer_address','mailer_port','zt_id'] as $key)
|
||||
$o->{$key} = $request->post($key);
|
||||
@@ -269,11 +273,9 @@ class SystemController extends Controller
|
||||
|
||||
$o->load(['addresses.zone.domain']);
|
||||
|
||||
return Gate::check('update',$o)
|
||||
? view('system.addedit')
|
||||
return view('system.addedit')
|
||||
->with('action',$o->exists ? 'update' : 'create')
|
||||
->with('o',$o)
|
||||
: redirect()->to('user/system/register');
|
||||
->with('o',$o);
|
||||
}
|
||||
|
||||
public function api_address(Request $request,System $o): Collection
|
||||
@@ -300,6 +302,42 @@ class SystemController extends Controller
|
||||
->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Identify all the addresses from systems that are not owned by a user
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Collection
|
||||
*/
|
||||
public function api_orphan_address(Request $request): Collection
|
||||
{
|
||||
$result = collect();
|
||||
|
||||
list($zone_id,$host_id,$node_id,$point_id,$domain) = sscanf($request->query('term'),'%d:%d/%d.%d@%s');
|
||||
|
||||
# Look for Systems
|
||||
foreach (Address::select(['addresses.id','systems.name',DB::raw('systems.id AS system_id'),'zones.zone_id','region_id','host_id','node_id','point_id','addresses.zone_id'])
|
||||
->join('zones',['zones.id'=>'addresses.zone_id'])
|
||||
->rightjoin('systems',['systems.id'=>'addresses.system_id'])
|
||||
->when($zone_id || $host_id || $node_id,function($query) use ($zone_id,$host_id,$node_id) {
|
||||
return $query
|
||||
->when($zone_id,function($q,$zone_id) { return $q->where('zones.zone_id',$zone_id); })
|
||||
->where(function($q) use ($host_id) {
|
||||
return $q
|
||||
->when($host_id,function($q,$host_id) { return $q->where('region_id',$host_id); })
|
||||
->when($host_id,function($q,$host_id) { return $q->orWhere('host_id',$host_id); });
|
||||
})
|
||||
->when($node_id,function($q,$node_id) { return $q->where('node_id',$node_id); });
|
||||
})
|
||||
->orWhere('systems.name','ilike','%'.$request->query('term').'%')
|
||||
->orderBy('systems.name')
|
||||
->get() as $o)
|
||||
{
|
||||
$result->push(['id'=>$o->id,'name'=>sprintf('%s (%s)',$o->ftn3d,$o->name),'category'=>'Systems']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete address assigned to a host
|
||||
*
|
||||
@@ -450,8 +488,26 @@ class SystemController extends Controller
|
||||
*/
|
||||
public function system_register(SystemRegister $request)
|
||||
{
|
||||
// Step 1, show the user a form to select an existing defined system
|
||||
if ($request->isMethod('GET'))
|
||||
return view('user.system.register');
|
||||
|
||||
$o = System::findOrNew($request->system_id);
|
||||
|
||||
// If the system exists, and we are 'register', we'll start the address claim process
|
||||
if ($o->exists && $request->action == 'register') {
|
||||
$validate = Setup::findOrFail(config('app.id'))->system->inMyZones($o->addresses);
|
||||
|
||||
// If we have addresses, we'll trigger the routed netmail
|
||||
if ($validate->count())
|
||||
Notification::route('netmail',$x=$validate->first())->notify(new AddressLink($x,Auth::user()));
|
||||
|
||||
return view('user.system.widget.register_confirm')
|
||||
->with('validate',$validate)
|
||||
->with('o',$o);
|
||||
}
|
||||
|
||||
// If the system doesnt exist, we'll create it
|
||||
if (! $o->exist) {
|
||||
$o->sysop = Auth::user()->name;
|
||||
|
||||
@@ -479,4 +535,4 @@ class SystemController extends Controller
|
||||
->with('o',$o)
|
||||
->with('errors',new ViewErrorBag);
|
||||
}
|
||||
}
|
||||
}
|
@@ -5,8 +5,9 @@ namespace App\Http\Controllers;
|
||||
use Illuminate\Auth\Events\Registered;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\{Address,User};
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
@@ -63,6 +64,36 @@ class UserController extends Controller
|
||||
return view('user.home');
|
||||
}
|
||||
|
||||
public function link(Request $request)
|
||||
{
|
||||
if ($request->post()) {
|
||||
$request->validate([
|
||||
'address_id'=>'required|exists:addresses,id',
|
||||
'code'=>'required:string',
|
||||
]);
|
||||
|
||||
$ao = Address::findOrFail($request->address_id);
|
||||
if ($ao->check_activation(Auth::user(),$request->code)) {
|
||||
$ao->validated = TRUE;
|
||||
$ao->save();
|
||||
|
||||
$ao->system->users()->save(Auth::user());
|
||||
|
||||
return redirect()->to('/');
|
||||
|
||||
} else {
|
||||
$validator = Validator::make([],[]);
|
||||
$validator->errors()->add(
|
||||
'code', 'Invalid Code!'
|
||||
);
|
||||
|
||||
return back()->withErrors($validator);
|
||||
}
|
||||
}
|
||||
|
||||
return view('user.link');
|
||||
}
|
||||
|
||||
public function register()
|
||||
{
|
||||
return view('user/system/register');
|
||||
|
@@ -21,7 +21,7 @@ class SystemRegister extends FormRequest
|
||||
{
|
||||
$this->so = System::findOrNew($request->system_id);
|
||||
|
||||
return Gate::allows($this->so->exists ? 'update' : 'create',$this->so);
|
||||
return Gate::allows($this->so->users->count() ? 'update' : 'register',$this->so);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,7 +31,7 @@ class SystemRegister extends FormRequest
|
||||
*/
|
||||
public function rules(Request $request)
|
||||
{
|
||||
if (! $request->isMethod('post'))
|
||||
if ((! $request->isMethod('post')) || ($request->action == 'register'))
|
||||
return [];
|
||||
|
||||
if ((! $this->so->exists) && ($request->action == 'create')) {
|
||||
|
Reference in New Issue
Block a user