rework env-struct & validation of expense_mailbox

This commit is contained in:
paulwer 2023-12-28 10:00:38 +01:00
parent 80a9d51ffb
commit 21a8f4da76
5 changed files with 39 additions and 42 deletions

View File

@ -51,8 +51,8 @@ class CompanyFactory
$company->first_month_of_year = 1; $company->first_month_of_year = 1;
// default mailbox // default mailbox
$company->expense_mailbox = config('ninja.inbound_expense.webhook.mailbox_template') != '' ? $company->expense_mailbox = config('ninja.ingest_mail.expense_mailbox_template') != '' ?
str_replace('{{company_key}}', $company->company_key, config('ninja.inbound_expense.webhook.mailbox_template')) : null; str_replace('{{company_key}}', $company->company_key, config('ninja.ingest_mail.expense_mailbox_template')) : null;
return $company; return $company;
} }

View File

@ -21,6 +21,7 @@ use Illuminate\Contracts\Validation\Rule;
class ValidExpenseMailbox implements Rule class ValidExpenseMailbox implements Rule
{ {
private $is_enterprise_error = false;
private $validated_schema = false; private $validated_schema = false;
private $company_key = false; private $company_key = false;
private $isEnterprise = false; private $isEnterprise = false;
@ -32,9 +33,7 @@ class ValidExpenseMailbox implements Rule
{ {
$this->company_key = $company_key; $this->company_key = $company_key;
$this->isEnterprise = $isEnterprise; $this->isEnterprise = $isEnterprise;
$this->endings = explode(",", config('ninja.inbound_expense.webhook.mailbox_endings')); $this->endings = explode(",", config('ninja.ingest_mail.expense_mailbox_endings'));
$this->hasCompanyKey = config('ninja.inbound_expense.webhook.mailbox_hascompanykey');
$this->enterprise_endings = explode(",", config('ninja.inbound_expense.webhook.mailbox_enterprise_endings'));
} }
public function passes($attribute, $value) public function passes($attribute, $value)
@ -43,33 +42,28 @@ class ValidExpenseMailbox implements Rule
return true; return true;
} }
// denie on hosted and not enterprise
if (Ninja::isHosted() && !$this->isEnterprise) {
$this->is_enterprise_error = true;
return false;
}
// early return, if we dont have any additional validation // early return, if we dont have any additional validation
if (!config('ninja.inbound_expense.webhook.mailbox_schema') && !(Ninja::isHosted() && config('ninja.inbound_expense.webhook.mailbox_schema_enterprise'))) { if (!config('ninja.ingest_mail.expense_mailbox_endings')) {
$this->validated_schema = true; $this->validated_schema = true;
return MultiDB::checkExpenseMailboxAvailable($value); return MultiDB::checkExpenseMailboxAvailable($value);
} }
// Validate Schema // Validate Schema
$validated_hasCompanyKey = !$this->hasCompanyKey || str_contains($value, $this->company_key);
$validated = false; $validated = false;
if ($validated_hasCompanyKey) foreach ($this->endings as $ending) {
foreach ($this->endings as $ending) { if (str_ends_with($ending, $value)) {
if (str_ends_with($ending, $value)) { $validated = true;
$validated = true; break;
break;
}
} }
}
$validated_enterprise = false; if (!$validated)
if (Ninja::isHosted() && $this->isEnterprise)
foreach ($this->endings as $ending) {
if (str_ends_with($ending, $value)) {
$validated_enterprise = true;
break;
}
}
if (!$validated && !$validated_enterprise)
return false; return false;
$this->validated_schema = true; $this->validated_schema = true;
@ -81,6 +75,12 @@ class ValidExpenseMailbox implements Rule
*/ */
public function message() public function message()
{ {
return $this->validated_schema ? ctrans('texts.expense_mailbox_taken') : ctrans('texts.expense_mailbox_invalid'); if ($this->validated_schema)
return ctrans('texts.expense_mailbox_not_available');
if ($this->validated_schema)
return ctrans('texts.expense_mailbox_taken');
return ctrans('texts.expense_mailbox_invalid');
} }
} }

View File

@ -74,14 +74,14 @@ class ExpenseMailboxJob implements ShouldQueue
private function getImapCredentials() private function getImapCredentials()
{ {
$servers = array_map('trim', explode(",", config('ninja.inbound_expense.imap.servers'))); $servers = array_map('trim', explode(",", config('ninja.ingest_mail.imap.servers')));
$ports = array_map('trim', explode(",", config('ninja.inbound_expense.imap.ports'))); $ports = array_map('trim', explode(",", config('ninja.ingest_mail.imap.ports')));
$users = array_map('trim', explode(",", config('ninja.inbound_expense.imap.users'))); $users = array_map('trim', explode(",", config('ninja.ingest_mail.imap.users')));
$passwords = array_map('trim', explode(",", config('ninja.inbound_expense.imap.passwords'))); $passwords = array_map('trim', explode(",", config('ninja.ingest_mail.imap.passwords')));
$companies = array_map('trim', explode(",", config('ninja.inbound_expense.imap.companies'))); $companies = array_map('trim', explode(",", config('ninja.ingest_mail.imap.companies')));
if (sizeOf($servers) != sizeOf($ports) || sizeOf($servers) != sizeOf($users) || sizeOf($servers) != sizeOf($passwords) || sizeOf($servers) != sizeOf($companies)) if (sizeOf($servers) != sizeOf($ports) || sizeOf($servers) != sizeOf($users) || sizeOf($servers) != sizeOf($passwords) || sizeOf($servers) != sizeOf($companies))
throw new \Exception('invalid configuration inbound_expense.imap (wrong element-count)'); throw new \Exception('invalid configuration ingest_mail.imap (wrong element-count)');
foreach ($companies as $index => $companyId) { foreach ($companies as $index => $companyId) {

View File

@ -228,19 +228,15 @@ return [
'secret' => env('PAYPAL_SECRET', null), 'secret' => env('PAYPAL_SECRET', null),
'client_id' => env('PAYPAL_CLIENT_ID', null), 'client_id' => env('PAYPAL_CLIENT_ID', null),
], ],
'inbound_expense' => [ 'ingest_mail' => [
'imap' => [ 'imap' => [
'servers' => env('INBOUND_EXPENSE_IMAP_SERVERS', ''), 'servers' => env('ingest_mail_IMAP_SERVERS', ''),
'ports' => env('INBOUND_EXPENSE_IMAP_PORTS', ''), 'ports' => env('ingest_mail_IMAP_PORTS', ''),
'users' => env('INBOUND_EXPENSE_IMAP_USERS', ''), 'users' => env('ingest_mail_IMAP_USERS', ''),
'passwords' => env('INBOUND_EXPENSE_IMAP_PASSWORDS', ''), 'passwords' => env('ingest_mail_IMAP_PASSWORDS', ''),
'companies' => env('INBOUND_EXPENSE_IMAP_COMPANIES', '1'), 'companies' => env('ingest_mail_IMAP_COMPANIES', '1'),
],
'webhook' => [
'mailbox_template' => env('INBOUND_EXPENSE_WEBHOOK_MAILBOX_TEMPLATE', null),
'mailbox_endings_endings' => env('INBOUND_EXPENSE_WEBHOOK_MAILBOX_ENDINGS', ''),
'mailbox_hascompanykey' => env('INBOUND_EXPENSE_WEBHOOK_MAILBOX_HASCOMPANYKEY', false),
'mailbox_endings_enterprise' => env('INBOUND_EXPENSE_WEBHOOK_MAILBOX_ENTERPRISE_ENDINGS', '@expense.invoicing.co'),
], ],
'expense_mailbox_template' => env('ingest_mail_EXPENSE_MAILBOX_TEMPLATE', null),
'expense_mailbox_endings' => env('ingest_mail_EXPENSE_MAILBOX_ENDINGS', '@expense.invoicing.co'),
], ],
]; ];

View File

@ -2534,6 +2534,7 @@ $lang = array(
'local_storage_required' => 'Error: local storage is not available.', 'local_storage_required' => 'Error: local storage is not available.',
'your_password_reset_link' => 'Your Password Reset Link', 'your_password_reset_link' => 'Your Password Reset Link',
'subdomain_taken' => 'The subdomain is already in use', 'subdomain_taken' => 'The subdomain is already in use',
'expense_mailbox_not_available' => 'You are not allowed to set an expense_inbox. Please upgrade plan to enterpise.',
'expense_mailbox_taken' => 'The mailbox is already in use', 'expense_mailbox_taken' => 'The mailbox is already in use',
'expense_mailbox_invalid' => 'The mailbox does not match the required schema', 'expense_mailbox_invalid' => 'The mailbox does not match the required schema',
'client_login' => 'Client Login', 'client_login' => 'Client Login',