From 3f610d68eb586b676b393fee81007a9798745064 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 7 Jun 2023 11:21:26 +1000 Subject: [PATCH] Refactor for subdomain validations: --- app/DataMapper/Tax/AU/Rule.php | 3 +- app/DataMapper/Tax/DE/Rule.php | 12 +++---- app/Http/Controllers/SubdomainController.php | 30 ++---------------- .../Requests/Company/UpdateCompanyRequest.php | 13 ++++---- .../Company/ValidSubdomain.php | 13 ++------ app/Libraries/MultiDB.php | 31 +++++++++++++++++++ .../Admin/EntityViewedNotification.php | 2 ++ 7 files changed, 52 insertions(+), 52 deletions(-) diff --git a/app/DataMapper/Tax/AU/Rule.php b/app/DataMapper/Tax/AU/Rule.php index b3d3178453..1887b3a241 100644 --- a/app/DataMapper/Tax/AU/Rule.php +++ b/app/DataMapper/Tax/AU/Rule.php @@ -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; } diff --git a/app/DataMapper/Tax/DE/Rule.php b/app/DataMapper/Tax/DE/Rule.php index 6175ad8b7d..c82f701496 100644 --- a/app/DataMapper/Tax/DE/Rule.php +++ b/app/DataMapper/Tax/DE/Rule.php @@ -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; } diff --git a/app/Http/Controllers/SubdomainController.php b/app/Http/Controllers/SubdomainController.php index e32f184d38..673bd642fb 100644 --- a/app/Http/Controllers/SubdomainController.php +++ b/app/Http/Controllers/SubdomainController.php @@ -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); } diff --git a/app/Http/Requests/Company/UpdateCompanyRequest.php b/app/Http/Requests/Company/UpdateCompanyRequest.php index 965e8025d9..2cfc25bd00 100644 --- a/app/Http/Requests/Company/UpdateCompanyRequest.php +++ b/app/Http/Requests/Company/UpdateCompanyRequest.php @@ -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']); } diff --git a/app/Http/ValidationRules/Company/ValidSubdomain.php b/app/Http/ValidationRules/Company/ValidSubdomain.php index 859949556a..1aa2ef6a33 100644 --- a/app/Http/ValidationRules/Company/ValidSubdomain.php +++ b/app/Http/ValidationRules/Company/ValidSubdomain.php @@ -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); } /** diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 2b6274345d..57e734253b 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -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) { diff --git a/app/Notifications/Admin/EntityViewedNotification.php b/app/Notifications/Admin/EntityViewedNotification.php index 4b916800a1..8014aa9045 100644 --- a/app/Notifications/Admin/EntityViewedNotification.php +++ b/app/Notifications/Admin/EntityViewedNotification.php @@ -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;