1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

Refactor for subdomain validations:

This commit is contained in:
David Bomba 2023-06-07 11:21:26 +10:00
parent beeefb2811
commit 3f610d68eb
7 changed files with 52 additions and 52 deletions

View File

@ -223,7 +223,7 @@ class Rule extends BaseRule implements RuleInterface
public function calculateRates(): self
{
if ($this->client->is_tax_exempt) {
nlog("tax exempt");
// nlog("tax exempt");
$this->tax_rate = 0;
$this->reduced_tax_rate = 0;
// } elseif($this->client_subregion != $this->client->company->tax_data->seller_subregion && in_array($this->client_subregion, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt) {
@ -247,7 +247,6 @@ class Rule extends BaseRule implements RuleInterface
// $this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_tax_rate;
// }
} else {
nlog("default tax");
$this->tax_rate = $this->client->company->tax_data->regions->AU->subregions->{$this->client->company->country()->iso_3166_2}->tax_rate;
$this->reduced_tax_rate = $this->client->company->tax_data->regions->AU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_tax_rate;
}

View File

@ -217,20 +217,20 @@ class Rule extends BaseRule implements RuleInterface
public function calculateRates(): self
{
if ($this->client->is_tax_exempt) {
nlog("tax exempt");
// nlog("tax exempt");
$this->tax_rate = 0;
$this->reduced_tax_rate = 0;
}
elseif($this->client_subregion != $this->client->company->tax_data->seller_subregion && in_array($this->client_subregion, $this->eu_country_codes) && $this->client->vat_number && $this->eu_business_tax_exempt)
// elseif($this->client_subregion != $this->client->company->tax_data->seller_subregion && in_array($this->client_subregion, $this->eu_country_codes) && $this->client->has_valid_vat_number && $this->eu_business_tax_exempt)
{
nlog("euro zone and tax exempt");
// nlog("euro zone and tax exempt");
$this->tax_rate = 0;
$this->reduced_tax_rate = 0;
}
elseif(!in_array($this->client_subregion, $this->eu_country_codes) && ($this->foreign_consumer_tax_exempt || $this->foreign_business_tax_exempt)) //foreign + tax exempt
{
nlog("foreign and tax exempt");
// nlog("foreign and tax exempt");
$this->tax_rate = 0;
$this->reduced_tax_rate = 0;
}
@ -242,18 +242,18 @@ class Rule extends BaseRule implements RuleInterface
{
if(($this->client->company->tax_data->seller_subregion != $this->client_subregion) && $this->client->company->tax_data->regions->EU->has_sales_above_threshold)
{
nlog("eu zone with sales above threshold");
// nlog("eu zone with sales above threshold");
$this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->tax_rate;
$this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->country->iso_3166_2}->reduced_tax_rate;
}
else {
nlog("EU with intra-community supply ie DE to DE");
// nlog("EU with intra-community supply ie DE to DE");
$this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->tax_rate;
$this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_tax_rate;
}
}
else {
nlog("default tax");
// nlog("default tax");
$this->tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->tax_rate;
$this->reduced_tax_rate = $this->client->company->tax_data->regions->EU->subregions->{$this->client->company->country()->iso_3166_2}->reduced_tax_rate;
}

View File

@ -15,44 +15,18 @@ use App\Libraries\MultiDB;
class SubdomainController extends BaseController
{
private $protected = [
'www',
'app',
'ninja',
'sentry',
'sentry2',
'staging',
'pdf',
'demo',
'docs',
'client_domain',
'custom_domain',
'preview',
'invoiceninja',
'cname',
'sandbox',
'stage',
'html',
'lb',
'shopify',
'beta',
'prometh',
'license',
'socket',
];
public function __construct()
{
}
/**
* Display a listing of the resource.
* Return if a subdomain is available.
*
* @return void
*/
public function index()
{
if (in_array(request()->input('subdomain'), $this->protected) || MultiDB::findAndSetDbByDomain(['subdomain' => request()->input('subdomain')])) {
if (!MultiDB::checkDomainAvailable(request()->input('subdomain'))) {
return response()->json(['message' => 'Domain not available'], 401);
}

View File

@ -61,14 +61,11 @@ class UpdateCompanyRequest extends Request
if (isset($input['portal_mode']) && ($input['portal_mode'] == 'domain' || $input['portal_mode'] == 'iframe')) {
$rules['portal_domain'] = 'sometimes|url';
} else {
if (Ninja::isHosted()) {
$rules['subdomain'] = ['nullable', 'regex:/^[a-zA-Z0-9.-]+[a-zA-Z0-9]$/', new ValidSubdomain($this->all())];
} else {
$rules['subdomain'] = 'nullable|regex:/^[a-zA-Z0-9.-]+[a-zA-Z0-9]$/';
}
}
if (Ninja::isHosted())
$rules['subdomain'] = ['nullable', 'regex:/^[a-zA-Z0-9.-]+[a-zA-Z0-9]$/', new ValidSubdomain()];
return $rules;
}
@ -85,6 +82,10 @@ class UpdateCompanyRequest extends Request
$input['settings'] = (array)$this->filterSaveableSettings($input['settings']);
}
if(array_key_exists('subdomain', $input) && $this->subdomain == $input['subdomain']) {
unset($input['subdomain']);
}
if(array_key_exists('e_invoice_certificate_passphrase', $input) && empty($input['e_invoice_certificate_passphrase'])) {
unset($input['e_invoice_certificate_passphrase']);
}

View File

@ -19,25 +19,18 @@ use Illuminate\Contracts\Validation\Rule;
*/
class ValidSubdomain implements Rule
{
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
private $input;
public function __construct($input)
public function __construct()
{
$this->input = $input;
}
public function passes($attribute, $value)
{
if (empty($input['subdomain'])) {
if (empty($value)) {
return true;
}
return MultiDB::checkDomainAvailable($input['subdomain']);
return MultiDB::checkDomainAvailable($value);
}
/**

View File

@ -45,6 +45,32 @@ class MultiDB
public static $dbs = ['db-ninja-01', 'db-ninja-02'];
private static $protected_domains = [
'www',
'app',
'ninja',
'sentry',
'sentry2',
'staging',
'pdf',
'demo',
'docs',
'client_domain',
'custom_domain',
'preview',
'invoiceninja',
'cname',
'sandbox',
'stage',
'html',
'lb',
'shopify',
'beta',
'prometh',
'license',
'socket',
];
/**
* @return array
*/
@ -55,10 +81,15 @@ class MultiDB
public static function checkDomainAvailable($subdomain) : bool
{
if (! config('ninja.db.multi_db_enabled')) {
return Company::whereSubdomain($subdomain)->count() == 0;
}
if (in_array($subdomain, self::$protected_domains)) {
return false;
}
$current_db = config('database.default');
foreach (self::$dbs as $db) {

View File

@ -38,6 +38,8 @@ class EntityViewedNotification extends Notification
protected $contact;
public $is_system;
public function __construct($invitation, $entity_name, $is_system = false, $settings = null)
{
$this->entity_name = $entity_name;