Add in company columns for SMTP configuration

This commit is contained in:
David Bomba 2024-02-16 13:06:03 +11:00
parent 49dfee7afe
commit 726ba91368
7 changed files with 117 additions and 7 deletions

View File

@ -56,6 +56,12 @@ class StoreCompanyRequest extends Request
} }
} }
$rules['smtp_host'] = 'sometimes|string';
$rules['smtp_port'] = 'sometimes|string';
$rules['smtp_encryption'] = 'sometimes|string';
$rules['smtp_local_domain'] = 'sometimes|string';
// $rules['smtp_verify_peer'] = 'sometimes|in:true,false';
return $rules; return $rules;
} }
@ -67,11 +73,11 @@ class StoreCompanyRequest extends Request
$input['name'] = 'Untitled Company'; $input['name'] = 'Untitled Company';
} }
if (array_key_exists('google_analytics_url', $input)) { if (isset($input['google_analytics_url'])) {
$input['google_analytics_key'] = $input['google_analytics_url']; $input['google_analytics_key'] = $input['google_analytics_url'];
} }
if (array_key_exists('portal_domain', $input)) { if (isset($input['portal_domain'])) {
$input['portal_domain'] = rtrim(strtolower($input['portal_domain']), "/"); $input['portal_domain'] = rtrim(strtolower($input['portal_domain']), "/");
} }
@ -79,6 +85,17 @@ class StoreCompanyRequest extends Request
$input['subdomain'] = MultiDB::randomSubdomainGenerator(); $input['subdomain'] = MultiDB::randomSubdomainGenerator();
} }
if(isset($input['smtp_username']) && strlen(str_replace("*", "", $input['smtp_username'])) < 2) {
unset($input['smtp_username']);
}
if(isset($input['smtp_password']) && strlen(str_replace("*", "", $input['smtp_password'])) < 2) {
unset($input['smtp_password']);
}
if(isset($input['smtp_verify_peer']) && is_string($input['smtp_verify_peer']))
$input['smtp_verify_peer'] == 'true' ? true : false;
$this->replace($input); $this->replace($input);
} }
} }

View File

@ -57,7 +57,13 @@ class UpdateCompanyRequest extends Request
$rules['matomo_id'] = 'nullable|integer'; $rules['matomo_id'] = 'nullable|integer';
$rules['e_invoice_certificate_passphrase'] = 'sometimes|nullable'; $rules['e_invoice_certificate_passphrase'] = 'sometimes|nullable';
$rules['e_invoice_certificate'] = 'sometimes|nullable|file|mimes:p12,pfx,pem,cer,crt,der,txt,p7b,spc,bin'; $rules['e_invoice_certificate'] = 'sometimes|nullable|file|mimes:p12,pfx,pem,cer,crt,der,txt,p7b,spc,bin';
// $rules['client_registration_fields'] = 'array';
$rules['smtp_host'] = 'sometimes|string';
$rules['smtp_port'] = 'sometimes|string';
$rules['smtp_encryption'] = 'sometimes|string';
$rules['smtp_local_domain'] = 'sometimes|string';
// $rules['smtp_verify_peer'] = 'sometimes|string';
if (isset($input['portal_mode']) && ($input['portal_mode'] == 'domain' || $input['portal_mode'] == 'iframe')) { if (isset($input['portal_mode']) && ($input['portal_mode'] == 'domain' || $input['portal_mode'] == 'iframe')) {
$rules['portal_domain'] = 'bail|nullable|sometimes|url'; $rules['portal_domain'] = 'bail|nullable|sometimes|url';
@ -74,23 +80,35 @@ class UpdateCompanyRequest extends Request
{ {
$input = $this->all(); $input = $this->all();
if (array_key_exists('portal_domain', $input) && strlen($input['portal_domain']) > 1) { if (isset($input['portal_domain']) && strlen($input['portal_domain']) > 1) {
$input['portal_domain'] = $this->addScheme($input['portal_domain']); $input['portal_domain'] = $this->addScheme($input['portal_domain']);
$input['portal_domain'] = rtrim(strtolower($input['portal_domain']), "/"); $input['portal_domain'] = rtrim(strtolower($input['portal_domain']), "/");
} }
if (array_key_exists('settings', $input)) { if (isset($input['settings'])) {
$input['settings'] = (array)$this->filterSaveableSettings($input['settings']); $input['settings'] = (array)$this->filterSaveableSettings($input['settings']);
} }
if(array_key_exists('subdomain', $input) && $this->company->subdomain == $input['subdomain']) { if(isset($input['subdomain']) && $this->company->subdomain == $input['subdomain']) {
unset($input['subdomain']); unset($input['subdomain']);
} }
if(array_key_exists('e_invoice_certificate_passphrase', $input) && empty($input['e_invoice_certificate_passphrase'])) { if(isset($input['e_invoice_certificate_passphrase']) && empty($input['e_invoice_certificate_passphrase'])) {
unset($input['e_invoice_certificate_passphrase']); unset($input['e_invoice_certificate_passphrase']);
} }
if(isset($input['smtp_username']) && strlen(str_replace("*","", $input['smtp_username'])) < 2) {
unset($input['smtp_username']);
}
if(isset($input['smtp_password']) && strlen(str_replace("*", "", $input['smtp_password'])) < 2) {
unset($input['smtp_password']);
}
if(isset($input['smtp_verify_peer']) && is_string($input['smtp_verify_peer'])) {
$input['smtp_verify_peer'] == 'true' ? true : false;
}
$this->replace($input); $this->replace($input);
} }

View File

@ -112,6 +112,13 @@ use Laracasts\Presenter\PresentableTrait;
* @property int $notify_vendor_when_paid * @property int $notify_vendor_when_paid
* @property int $invoice_task_hours * @property int $invoice_task_hours
* @property int $deleted_at * @property int $deleted_at
* @property string smtp_username
* @property string smtp_password
* @property string smtp_host
* @property string smtp_port
* @property string smtp_encryption
* @property string smtp_local_domain
* @property boolean smtp_verify_peer
* @property-read \App\Models\Account $account * @property-read \App\Models\Account $account
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities * @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Activity> $activities
* @property-read int|null $activities_count * @property-read int|null $activities_count
@ -352,12 +359,19 @@ class Company extends BaseModel
'calculate_taxes', 'calculate_taxes',
'tax_data', 'tax_data',
'e_invoice_certificate_passphrase', 'e_invoice_certificate_passphrase',
'smtp_host',
'smtp_port',
'smtp_encryption',
'smtp_local_domain',
'smtp_verify_peer',
]; ];
protected $hidden = [ protected $hidden = [
'id', 'id',
'db', 'db',
'ip', 'ip',
'smtp_username',
'smtp_password',
]; ];
protected $casts = [ protected $casts = [
@ -372,6 +386,8 @@ class Company extends BaseModel
'tax_data' => 'object', 'tax_data' => 'object',
'origin_tax_data' => 'object', 'origin_tax_data' => 'object',
'e_invoice_certificate_passphrase' => EncryptedCast::class, 'e_invoice_certificate_passphrase' => EncryptedCast::class,
'smtp_username' => 'encrypted',
'smtp_password' => 'encrypted',
]; ];
protected $with = []; protected $with = [];

View File

@ -39,6 +39,7 @@ class CompanyRepository extends BaseRepository
$company->fill($data); $company->fill($data);
// nlog($data);
/** Only required to handle v4 migration workloads */ /** Only required to handle v4 migration workloads */
if(Ninja::isHosted() && $company->isDirty('is_disabled') && !$company->is_disabled) { if(Ninja::isHosted() && $company->isDirty('is_disabled') && !$company->is_disabled) {
Ninja::triggerForwarding($company->company_key, $company->owner()->email); Ninja::triggerForwarding($company->company_key, $company->owner()->email);
@ -48,6 +49,14 @@ class CompanyRepository extends BaseRepository
$company->saveSettings($data['settings'], $company); $company->saveSettings($data['settings'], $company);
} }
if(isset($data['smtp_username'])) {
$company->smtp_username = $data['smtp_username'];
}
if(isset($data['smtp_password'])) {
$company->smtp_password = $data['smtp_password'];
}
$company->save(); $company->save();
return $company; return $company;

View File

@ -204,6 +204,13 @@ class CompanyTransformer extends EntityTransformer
'invoice_task_project_header' => (bool) $company->invoice_task_project_header, 'invoice_task_project_header' => (bool) $company->invoice_task_project_header,
'invoice_task_item_description' => (bool) $company->invoice_task_item_description, 'invoice_task_item_description' => (bool) $company->invoice_task_item_description,
'origin_tax_data' => $company->origin_tax_data ?: new \stdClass(), 'origin_tax_data' => $company->origin_tax_data ?: new \stdClass(),
'smtp_host' => (string)$company->smtp_host ?? '',
'smtp_port' => (string)$company->smtp_port ?? '',
'smtp_encryption' => (string)$company->smtp_encryption ?? 'tls',
'smtp_username' => $company->smtp_username ? '********' : '',
'smtp_password' => $company->smtp_password ? '********' : '',
'smtp_local_domain' => (string)$company->smtp_local_domain ?? '',
'smtp_verify_peer' => (bool)$company->smtp_verify_peer,
]; ];
} }

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('companies', function (Blueprint $table) {
$table->string('smtp_host')->nullable();
$table->unsignedInteger('smtp_port')->nullable();
$table->string('smtp_encryption')->nullable();
$table->text('smtp_username')->nullable();
$table->text('smtp_password')->nullable();
$table->string('smtp_local_domain')->nullable();
$table->boolean('smtp_verify_peer')->default(0);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

View File

@ -50,6 +50,15 @@ class CompanyTest extends TestCase
$this->makeTestData(); $this->makeTestData();
} }
public function testEnsureStrReplace()
{
$x = '**********';
$new_string = str_replace("*", "", $x);
$this->assertEquals(0, strlen($new_string));
}
public function testCompanyTaxInit() public function testCompanyTaxInit()
{ {
TaxRate::query()->delete(); TaxRate::query()->delete();