clrghouz/app/Http/Controllers/UserController.php

73 lines
1.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Http\Controllers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
2021-11-26 06:19:55 +00:00
use Illuminate\Support\Facades\Auth;
2024-04-25 05:44:06 +00:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\UserRequest;
use App\Models\{Address,User};
class UserController extends Controller
{
/**
* Add or edit a node
*/
public function add_edit(UserRequest $request,User $o)
{
if ($request->post()) {
foreach (['name','email','pgp_pubkey','active','admin','alias','system_id'] as $key)
$o->{$key} = $request->post($key);
if (! $o->exists)
$o->password = base64_encode(random_bytes(20));
2024-04-25 05:44:06 +00:00
elseif ($request->password)
$o->password = Hash::make($request->password);
$o->save();
if ($o->wasRecentlyCreated)
event(new Registered($o));
2024-04-25 05:44:06 +00:00
return redirect()
->to('user/addedit/'.$o->id)
->with('success','User Updated');
}
return view('user.addedit')
->with('o',$o);
}
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->activation_check(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');
}
}