2020-05-28 17:39:38 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests\ClientPortal;
|
|
|
|
|
2021-06-28 11:56:04 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2021-03-30 14:07:31 +02:00
|
|
|
use App\Models\Account;
|
2020-06-22 10:26:48 +02:00
|
|
|
use App\Models\Company;
|
2021-03-30 14:07:31 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-05-28 17:39:38 +02:00
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
|
|
|
|
|
|
class RegisterRequest extends FormRequest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2021-10-04 12:34:00 +02:00
|
|
|
public function rules(): array
|
2020-05-28 17:39:38 +02:00
|
|
|
{
|
2021-10-04 12:34:00 +02:00
|
|
|
$rules = [];
|
|
|
|
|
|
|
|
foreach ($this->company()->client_registration_fields as $field) {
|
|
|
|
if ($field['required']) {
|
|
|
|
$rules[$field['key']] = ['required'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($rules as $field => $properties) {
|
|
|
|
if ($field === 'email') {
|
|
|
|
$rules[$field] = array_merge($rules[$field], ['email:rfc,dns', 'max:255']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($field === 'password') {
|
|
|
|
$rules[$field] = array_merge($rules[$field], ['string', 'min:6', 'confirmed']);
|
|
|
|
}
|
|
|
|
}
|
2021-06-28 12:31:23 +02:00
|
|
|
|
|
|
|
if ($this->company()->settings->client_portal_terms || $this->company()->settings->client_portal_privacy_policy) {
|
|
|
|
$rules['terms'] = ['required'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rules;
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|
2020-06-22 10:26:48 +02:00
|
|
|
|
|
|
|
public function company()
|
2021-06-28 12:31:23 +02:00
|
|
|
{
|
2021-06-28 11:56:04 +02:00
|
|
|
|
|
|
|
//this should be all we need, the rest SHOULD be redundant because of our Middleware
|
|
|
|
if ($this->key)
|
|
|
|
return Company::where('company_key', $this->key)->first();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-06-22 10:26:48 +02:00
|
|
|
if ($this->company_key) {
|
|
|
|
return Company::where('company_key', $this->company_key)->firstOrFail();
|
|
|
|
}
|
|
|
|
|
2021-03-30 14:07:31 +02:00
|
|
|
if (!$this->route()->parameter('company_key') && Ninja::isSelfHost()) {
|
|
|
|
$company = Account::first()->default_company;
|
|
|
|
|
2021-06-28 11:56:04 +02:00
|
|
|
if(!$company->client_can_register)
|
|
|
|
abort(403, "This page is restricted");
|
2021-03-30 14:07:31 +02:00
|
|
|
|
|
|
|
return $company;
|
|
|
|
}
|
|
|
|
|
2021-06-28 11:56:04 +02:00
|
|
|
if (Ninja::isHosted()) {
|
|
|
|
|
|
|
|
$subdomain = explode('.', $this->getHost())[0];
|
2021-06-28 12:31:23 +02:00
|
|
|
|
2021-06-28 11:56:04 +02:00
|
|
|
$query = [
|
|
|
|
'subdomain' => $subdomain,
|
|
|
|
'portal_mode' => 'subdomain',
|
|
|
|
];
|
|
|
|
|
|
|
|
if($company = MultiDB::findAndSetDbByDomain($query))
|
|
|
|
return $company;
|
2021-06-28 12:31:23 +02:00
|
|
|
|
2021-06-28 11:56:04 +02:00
|
|
|
$query = [
|
|
|
|
'portal_domain' => $this->getSchemeAndHttpHost(),
|
|
|
|
'portal_mode' => 'domain',
|
|
|
|
];
|
|
|
|
|
|
|
|
if($company = MultiDB::findAndSetDbByDomain($query))
|
|
|
|
return $company;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
abort(400, 'Register request not found.');
|
2020-06-22 10:26:48 +02:00
|
|
|
}
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|