1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Http/Requests/Email/SendEmailRequest.php

116 lines
3.1 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
use App\Http\Requests\Request;
2023-03-08 07:43:52 +01:00
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
2023-03-08 07:43:52 +01:00
use Illuminate\Auth\Access\AuthorizationException;
2022-07-04 07:27:09 +02:00
use Illuminate\Support\Str;
class SendEmailRequest extends Request
{
use MakesHash;
2023-03-08 07:43:52 +01:00
private string $error_message = '';
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() : bool
{
return $this->checkUserAbleToSend();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
2022-05-06 01:34:18 +02:00
'template' => 'bail|required',
'entity' => 'bail|required',
'entity_id' => 'bail|required',
'cc_email' => 'bail|sometimes|email',
];
}
2022-07-15 14:59:25 +02:00
public function prepareForValidation()
{
$input = $this->all();
$settings = auth()->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
}
2022-05-06 01:34:18 +02:00
2023-02-16 02:36:09 +01:00
if (array_key_exists('entity', $input)) {
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
}
$this->replace($input);
}
public function message()
{
return [
'template' => 'Invalid template.',
];
}
private function checkUserAbleToSend()
{
$input = $this->all();
2023-03-08 07:43:52 +01:00
2023-03-08 07:56:34 +01:00
if (Ninja::isHosted() && !auth()->user()->account->account_sms_verified) {
2023-03-08 07:43:52 +01:00
$this->error_message = ctrans('texts.authorization_sms_failure');
2023-03-08 07:43:52 +01:00
return false;
}
/*Make sure we have all the require ingredients to send a template*/
if (array_key_exists('entity', $input) && array_key_exists('entity_id', $input) && is_string($input['entity']) && $input['entity_id']) {
$company = auth()->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 */
if ($entity_obj && ($company->id == $entity_obj->company_id) && auth()->user()->can('edit', $entity_obj)) {
return true;
}
}
return false;
}
2023-03-08 07:43:52 +01:00
protected function failedAuthorization()
{
throw new AuthorizationException($this->error_message);
}
}