2020-03-28 12:34:04 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-28 12:34:04 +01:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-03-28 12:34:04 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-03-28 12:34:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Http\Requests\Email;
|
|
|
|
|
2023-03-08 07:43:52 +01:00
|
|
|
use App\Utils\Ninja;
|
2023-12-17 00:05:33 +01:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use App\Http\Requests\Request;
|
2020-03-29 14:22:14 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2023-12-17 00:05:33 +01:00
|
|
|
use Illuminate\Validation\Rule;
|
2023-03-08 07:43:52 +01:00
|
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
2020-03-28 12:34:04 +01:00
|
|
|
|
|
|
|
class SendEmailRequest extends Request
|
|
|
|
{
|
2020-03-29 14:22:14 +02:00
|
|
|
use MakesHash;
|
2020-03-28 12:34:04 +01:00
|
|
|
|
2024-10-15 05:22:47 +02:00
|
|
|
private string $entity_plural = 'invoices';
|
2023-03-08 07:43:52 +01:00
|
|
|
private string $error_message = '';
|
2023-12-17 00:16:02 +01:00
|
|
|
|
|
|
|
public array $templates = [
|
|
|
|
'email_template_invoice',
|
|
|
|
'email_template_quote',
|
|
|
|
'email_template_credit',
|
|
|
|
'email_template_payment',
|
|
|
|
'email_template_payment_partial',
|
|
|
|
'email_template_statement',
|
|
|
|
'email_template_reminder1',
|
|
|
|
'email_template_reminder2',
|
|
|
|
'email_template_reminder3',
|
|
|
|
'email_template_reminder_endless',
|
|
|
|
'email_template_custom1',
|
|
|
|
'email_template_custom2',
|
|
|
|
'email_template_custom3',
|
|
|
|
'email_template_purchase_order',
|
|
|
|
];
|
|
|
|
|
2020-03-28 12:34:04 +01:00
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
public function authorize(): bool
|
2020-03-28 12:34:04 +01:00
|
|
|
{
|
|
|
|
return $this->checkUserAbleToSend();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
2023-12-17 00:05:33 +01:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
2020-03-28 12:34:04 +01:00
|
|
|
return [
|
2024-07-10 06:32:11 +02:00
|
|
|
'template' => 'bail|required|string|in:'.implode(',', $this->templates),
|
2023-12-17 00:05:33 +01:00
|
|
|
'entity' => 'bail|required|in:App\Models\Invoice,App\Models\Quote,App\Models\Credit,App\Models\RecurringInvoice,App\Models\PurchaseOrder,App\Models\Payment',
|
|
|
|
'entity_id' => ['bail', 'required', Rule::exists($this->entity_plural, 'id')->where('company_id', $user->company()->id)],
|
2023-11-24 04:35:56 +01:00
|
|
|
'cc_email.*' => 'bail|sometimes|email',
|
2020-03-28 12:34:04 +01:00
|
|
|
];
|
2023-11-21 09:35:57 +01:00
|
|
|
|
2020-03-28 12:34:04 +01:00
|
|
|
}
|
|
|
|
|
2022-07-15 14:59:25 +02:00
|
|
|
public function prepareForValidation()
|
2020-03-28 12:34:04 +01:00
|
|
|
{
|
|
|
|
$input = $this->all();
|
|
|
|
|
2023-08-27 00:41:27 +02:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
|
|
|
|
|
|
|
$settings = $user->company()->settings;
|
2020-03-28 12:34:04 +01:00
|
|
|
|
2020-04-04 12:32:42 +02:00
|
|
|
if (empty($input['template'])) {
|
2020-03-29 14:22:14 +02:00
|
|
|
$input['template'] = '';
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-03-29 14:22:14 +02:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! property_exists($settings, $input['template'])) {
|
2020-03-28 12:34:04 +01:00
|
|
|
unset($input['template']);
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2020-03-28 12:34:04 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (array_key_exists('entity_id', $input)) {
|
2022-05-06 01:34:18 +02:00
|
|
|
$input['entity_id'] = $this->decodePrimaryKey($input['entity_id']);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2024-07-17 23:56:44 +02:00
|
|
|
if (isset($input['entity']) && in_array($input['entity'], ['invoice','quote','credit','recurring_invoice','purchase_order','payment'])) {
|
2024-10-15 05:22:47 +02:00
|
|
|
$this->entity_plural = Str::plural($input['entity']) ?? '';
|
2022-07-04 07:27:09 +02:00
|
|
|
$input['entity'] = "App\Models\\".ucfirst(Str::camel($input['entity']));
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2020-03-29 14:22:14 +02:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
if(isset($input['cc_email'])) {
|
|
|
|
$input['cc_email'] = collect(explode(",", $input['cc_email']))->map(function ($email) {
|
2023-11-21 09:35:57 +01:00
|
|
|
return trim($email);
|
2023-11-26 08:41:42 +01:00
|
|
|
})->filter(function ($email) {
|
2023-11-21 09:35:57 +01:00
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL);
|
2023-11-26 08:41:42 +01:00
|
|
|
})->slice(0, 4)->toArray();
|
2023-11-21 09:35:57 +01:00
|
|
|
}
|
|
|
|
|
2024-10-15 05:22:47 +02:00
|
|
|
|
2020-03-28 12:34:04 +01:00
|
|
|
$this->replace($input);
|
|
|
|
}
|
|
|
|
|
2024-10-15 05:22:47 +02:00
|
|
|
public function messages()
|
2020-03-28 12:34:04 +01:00
|
|
|
{
|
|
|
|
return [
|
2024-10-15 05:22:47 +02:00
|
|
|
'template.in' => 'Template :input is anot a valid template.',
|
|
|
|
'entity.in' => 'Entity :input is not a valid entity.'
|
2020-03-28 12:34:04 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkUserAbleToSend()
|
|
|
|
{
|
|
|
|
$input = $this->all();
|
2023-11-01 01:00:23 +01:00
|
|
|
|
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = auth()->user();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-01 01:00:23 +01:00
|
|
|
if (Ninja::isHosted() && !$user->account->account_sms_verified) {
|
2023-03-08 07:43:52 +01:00
|
|
|
$this->error_message = ctrans('texts.authorization_sms_failure');
|
|
|
|
return false;
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-01 01:00:23 +01:00
|
|
|
if (Ninja::isHosted() && $user->account->emailQuotaExceeded()) {
|
|
|
|
$this->error_message = ctrans('texts.email_quota_exceeded_subject');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:34:04 +01:00
|
|
|
/*Make sure we have all the require ingredients to send a template*/
|
2024-10-15 05:22:47 +02:00
|
|
|
if (isset($input['entity']) && array_key_exists('entity_id', $input) && in_array($input['entity'], ['App\Models\Invoice','App\Models\Quote','App\Models\Credit','App\Models\RecurringInvoice','App\Models\PurchaseOrder','App\Models\Payment'])) {
|
2023-04-27 05:15:49 +02:00
|
|
|
|
|
|
|
$company = $user->company();
|
2020-03-28 12:34:04 +01:00
|
|
|
|
2020-03-29 14:22:14 +02:00
|
|
|
$entity = $input['entity'];
|
2020-03-28 12:34:04 +01:00
|
|
|
|
|
|
|
/* Harvest the entity*/
|
2021-09-28 08:12:50 +02:00
|
|
|
$entity_obj = $entity::whereId($input['entity_id'])->withTrashed()->company()->first();
|
2020-03-28 12:34:04 +01:00
|
|
|
|
|
|
|
/* Check object, check user and company id is same as users, and check user can edit the object */
|
2023-04-27 05:15:49 +02:00
|
|
|
if ($entity_obj && ($company->id == $entity_obj->company_id) && $user->can('edit', $entity_obj)) {
|
2020-03-28 12:34:04 +01:00
|
|
|
return true;
|
2020-04-04 12:32:42 +02:00
|
|
|
}
|
2024-10-15 05:22:47 +02:00
|
|
|
else {
|
|
|
|
$this->error_message = "Invalid entity or entity_id";
|
|
|
|
}
|
|
|
|
}
|
2020-03-28 12:34:04 +01:00
|
|
|
|
2024-10-15 05:22:47 +02:00
|
|
|
return true;
|
2020-03-28 12:34:04 +01:00
|
|
|
}
|
2023-03-08 07:43:52 +01:00
|
|
|
|
|
|
|
protected function failedAuthorization()
|
|
|
|
{
|
|
|
|
throw new AuthorizationException($this->error_message);
|
|
|
|
}
|
2020-03-28 12:34:04 +01:00
|
|
|
}
|