1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Http/Requests/Email/SendEmailRequest.php

163 lines
4.8 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @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)
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
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;
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;
class SendEmailRequest extends Request
{
use MakesHash;
2023-12-17 00:05:33 +01:00
private string $entity_plural = '';
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',
];
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
2024-01-14 05:05:00 +01:00
public function authorize(): bool
{
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();
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',
];
2023-11-21 09:35:57 +01:00
}
2022-07-15 14:59:25 +02:00
public function prepareForValidation()
{
$input = $this->all();
2023-08-27 00:41:27 +02:00
/** @var \App\Models\User $user */
$user = auth()->user();
$settings = $user->company()->settings;
if (empty($input['template'])) {
$input['template'] = '';
}
if (! property_exists($settings, $input['template'])) {
unset($input['template']);
}
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
2023-12-17 00:05:33 +01:00
$this->entity_plural = Str::plural($input['entity']) ?? '';
2024-07-17 23:56:44 +02:00
if (isset($input['entity']) && in_array($input['entity'], ['invoice','quote','credit','recurring_invoice','purchase_order','payment'])) {
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
}
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
}
$this->replace($input);
}
public function message()
{
return [
'template' => 'Invalid template.',
];
}
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;
}
/*Make sure we have all the require ingredients to send a template*/
2023-08-05 01:31:16 +02:00
if (isset($input['entity']) && array_key_exists('entity_id', $input) && is_string($input['entity']) && $input['entity_id']) {
2023-04-27 05:15:49 +02:00
$company = $user->company();
$entity = $input['entity'];
/* Harvest the entity*/
2021-09-28 08:12:50 +02:00
$entity_obj = $entity::whereId($input['entity_id'])->withTrashed()->company()->first();
/* 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)) {
return true;
}
2023-10-26 04:57:44 +02:00
} else {
2023-08-05 01:31:16 +02:00
$this->error_message = "Invalid entity or entity_id";
}
return false;
}
2023-03-08 07:43:52 +01:00
protected function failedAuthorization()
{
throw new AuthorizationException($this->error_message);
}
}