2021-06-17 14:08:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2021-06-18 15:09:34 +00:00
|
|
|
use Illuminate\Auth\Events\Registered;
|
2021-06-17 14:08:30 +00:00
|
|
|
use Illuminate\Http\Request;
|
2021-11-26 06:19:55 +00:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2022-03-14 11:28:54 +00:00
|
|
|
use Illuminate\Support\Facades\Validator;
|
2021-06-17 14:08:30 +00:00
|
|
|
|
2022-03-14 11:28:54 +00:00
|
|
|
use App\Models\{Address,User};
|
2021-06-17 14:08:30 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Add or edit a node
|
|
|
|
*/
|
|
|
|
public function add_edit(Request $request,User $o)
|
|
|
|
{
|
|
|
|
if ($request->post()) {
|
2021-06-18 13:01:41 +00:00
|
|
|
$this->authorize('admin',$o);
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
$request->validate([
|
2021-06-18 15:09:34 +00:00
|
|
|
'email' => 'required|email|unique:users,email,'.($o->exists ? $o->id : 0),
|
|
|
|
'name' => 'required|min:3',
|
|
|
|
'pgp_pubkey' => [
|
|
|
|
'nullable',
|
|
|
|
'min:64',
|
|
|
|
//function ($attribute,$value,$fail) { return $fail('Not this time'); }
|
|
|
|
],
|
2021-10-19 11:21:32 +00:00
|
|
|
'alias' => 'nullable|min:1',
|
2021-06-18 15:09:34 +00:00
|
|
|
'active' => 'required|boolean',
|
|
|
|
'admin' => 'required|boolean',
|
2021-06-17 14:08:30 +00:00
|
|
|
]);
|
|
|
|
|
2021-10-19 11:21:32 +00:00
|
|
|
foreach (['name','email','pgp_pubkey','active','admin','alias'] as $key)
|
2021-06-17 14:08:30 +00:00
|
|
|
$o->{$key} = $request->post($key);
|
|
|
|
|
2021-06-18 15:09:34 +00:00
|
|
|
if (! $o->exists)
|
|
|
|
$o->password = base64_encode(random_bytes(20));
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
$o->save();
|
|
|
|
|
2021-06-18 15:09:34 +00:00
|
|
|
if ($o->wasRecentlyCreated)
|
|
|
|
event(new Registered($o));
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
return redirect()->action([self::class,'home']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return view('user.addedit')
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:19:55 +00:00
|
|
|
public function dashboard()
|
|
|
|
{
|
2021-11-26 06:19:55 +00:00
|
|
|
$user = Auth::user();
|
2021-12-03 00:24:23 +00:00
|
|
|
$user->load('systems.addresses.zone.domain.echoareas');
|
2021-11-26 06:19:55 +00:00
|
|
|
|
|
|
|
return view('dashboard')
|
|
|
|
->with('user',$user);
|
2021-10-26 12:19:55 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 14:08:30 +00:00
|
|
|
public function home()
|
|
|
|
{
|
|
|
|
return view('user.home');
|
|
|
|
}
|
2021-11-11 11:57:13 +00:00
|
|
|
|
2022-03-14 11:28:54 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2021-11-11 11:57:13 +00:00
|
|
|
public function register()
|
|
|
|
{
|
|
|
|
return view('user/system/register');
|
|
|
|
}
|
2021-06-17 14:08:30 +00:00
|
|
|
}
|