2020-05-28 17:39:38 +02:00
|
|
|
<?php
|
2022-09-06 13:32:52 +02:00
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2022-09-06 13:32:52 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
2020-05-28 17:39:38 +02:00
|
|
|
|
|
|
|
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;
|
2022-09-06 13:32:52 +02:00
|
|
|
use Illuminate\Validation\Rule;
|
2020-05-28 17:39:38 +02:00
|
|
|
|
|
|
|
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']) {
|
2022-09-06 13:32:52 +02:00
|
|
|
$rules[$field['key']] = ['bail','required'];
|
2021-10-04 12:34:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($rules as $field => $properties) {
|
|
|
|
if ($field === 'email') {
|
2022-12-20 11:49:53 +01:00
|
|
|
$rules[$field] = array_merge($rules[$field], ['email:rfc,dns', 'max:191', Rule::unique('client_contacts')->where('company_id', $this->company()->id)]);
|
2021-10-04 12:34:00 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 11:58:30 +02:00
|
|
|
if ($field === 'current_password') {
|
2021-10-04 12:34:00 +02:00
|
|
|
$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
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->key) {
|
2021-06-28 11:56:04 +02:00
|
|
|
return Company::where('company_key', $this->key)->first();
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
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();
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $this->route()->parameter('company_key') && Ninja::isSelfHost()) {
|
2021-03-30 14:07:31 +02:00
|
|
|
$company = Account::first()->default_company;
|
|
|
|
|
2022-06-21 11:57:17 +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',
|
|
|
|
];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($company = MultiDB::findAndSetDbByDomain($query)) {
|
2021-06-28 11:56:04 +02:00
|
|
|
return $company;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-06-28 12:31:23 +02:00
|
|
|
|
2021-06-28 11:56:04 +02:00
|
|
|
$query = [
|
|
|
|
'portal_domain' => $this->getSchemeAndHttpHost(),
|
|
|
|
'portal_mode' => 'domain',
|
|
|
|
];
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($company = MultiDB::findAndSetDbByDomain($query)) {
|
2021-06-28 11:56:04 +02:00
|
|
|
return $company;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-06-28 11:56:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
abort(400, 'Register request not found.');
|
2020-06-22 10:26:48 +02:00
|
|
|
}
|
2020-05-28 17:39:38 +02:00
|
|
|
}
|