1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Http/Requests/ClientPortal/RegisterRequest.php

112 lines
3.0 KiB
PHP
Raw Normal View History

2020-05-28 17:39:38 +02:00
<?php
/**
* 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)
*
* @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;
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']) {
$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']);
}
}
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 11:56:04 +02:00
//this should be all we need, the rest SHOULD be redundant because of our Middleware
if ($this->key) {
2021-06-28 11:56:04 +02:00
return Company::where('company_key', $this->key)->first();
}
2020-06-22 10:26:48 +02:00
if ($this->company_key) {
return Company::where('company_key', $this->company_key)->firstOrFail();
}
if (! $this->route()->parameter('company_key') && Ninja::isSelfHost()) {
2021-03-30 14:07:31 +02:00
$company = Account::first()->default_company;
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 11:56:04 +02:00
$query = [
'subdomain' => $subdomain,
'portal_mode' => 'subdomain',
];
if ($company = MultiDB::findAndSetDbByDomain($query)) {
2021-06-28 11:56:04 +02:00
return $company;
}
2021-06-28 11:56:04 +02:00
$query = [
'portal_domain' => $this->getSchemeAndHttpHost(),
'portal_mode' => 'domain',
];
if ($company = MultiDB::findAndSetDbByDomain($query)) {
2021-06-28 11:56:04 +02:00
return $company;
}
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
}