2024-05-28 12:37:52 +10:00

112 lines
2.6 KiB
PHP

<?php
namespace App\Classes\BBS\Frame\Action;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Validator;
use App\Classes\BBS\Frame\{Action,Field};
use App\Classes\BBS\Exceptions\ActionMissingInputsException;
use App\Classes\BBS\Server;
use App\Mail\BBS\SendToken;
use App\Models\User;
/**
* Class Register
* This handles the data received for account registration
*
* @package App\Classes\Frame\Action
*/
class Register extends Action
{
protected const fields = ['EMAIL','USER','PASS','FULLNAME','TOKEN'];
private string $token = '';
public function __get(string $key): mixed
{
switch ($key) {
case 'user': return $this->uo;
default:
return parent::__get($key);
}
}
/**
* Handle user logins
*
* @return bool
* @throws ActionMissingInputsException
*/
public function handle(): bool
{
parent::init();
$this->uo = new User;
$this->uo->name = $this->fields_input->where('name','FULLNAME')->first()->value;
$this->uo->email = $this->fields_input->where('name','EMAIL')->first()->value;
$this->uo->email_verified_at = Carbon::now();
$this->uo->password = Hash::make($x=$this->fields_input->where('name','PASS')->first()->value);
$this->uo->active = TRUE;
$this->uo->last_on = Carbon::now();
$this->uo->alias = $this->fields_input->where('name','USER')->first()->value;
$this->uo->save();
return TRUE;
}
public function preSubmitField(Server $server,Field $field): ?string
{
switch ($field->name) {
// Send a token
case 'EMAIL':
// Make sure we got an email address
if (Validator::make(['email'=>$field->value],[
'email'=>'email',
])->fails()) {
return 'INVALID EMAIL ADDRESS';
}
// See if the requested email already exists
if (User::where('email',$field->value)->exists())
return 'USER ALREADY EXISTS';
Log::info(sprintf('Sending token to [%s]',$field->value));
$server->sendBaseline(RED.'SENDING TOKEN...');
$this->token = sprintf('%06.0f',rand(0,999999));
$sent = Mail::to($field->value)->send(new SendToken($this->token));
$server->sendBaseline(RED.'SENT');
break;
case 'USER':
if (str_contains($field->value,' '))
return 'NO SPACES IN USER NAMES';
// See if the requested username already exists
if (User::where('alias',$field->value)->exists())
return 'USER ALREADY EXISTS';
// Clear the baseline from EMAIL entry
$server->sendBaseline('');
break;
case 'TOKEN':
if ($field->value !== $this->token)
return 'INVALID TOKEN';
break;
}
return NULL;
}
}