1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 15:42:51 +01:00
invoiceninja/app/Http/Requests/Activity/StoreNoteRequest.php

80 lines
2.0 KiB
PHP
Raw Normal View History

2024-06-19 07:42:06 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Requests\Activity;
use Illuminate\Support\Str;
use App\Http\Requests\Request;
use Illuminate\Validation\Rule;
class StoreNoteRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return $this->checkAuthority();
}
public function rules()
{
/** @var \App\Models\User $user */
$user = auth()->user();
$rules = [
2024-06-21 06:09:25 +02:00
'entity' => 'required|bail|in:invoices,quotes,credits,recurring_invoices,clients,vendors,credits,payments,projects,tasks,expenses,recurring_expenses,bank_transactions,purchase_orders',
2024-06-19 07:42:06 +02:00
'entity_id' => ['required','bail', Rule::exists($this->entity, 'id')->where('company_id', $user->company()->id)],
2024-06-21 06:09:25 +02:00
'notes' => 'required|bail',
2024-06-19 07:42:06 +02:00
];
return $rules;
}
public function prepareForValidation()
{
$input = $this->all();
if(isset($input['entity_id']) && $input['entity_id'] != null) {
$input['entity_id'] = $this->decodePrimaryKey($input['entity_id']);
}
$this->replace($input);
}
public function checkAuthority(): bool
{
$this->error_message = ctrans('texts.authorization_failure');
/** @var \App\Models\User $user */
$user = auth()->user();
$entity = $this->getEntity();
return $user->isAdmin() || $user->can('view', $entity);
}
public function getEntity()
{
2024-06-21 06:09:25 +02:00
if(!$this->entity)
return false;
2024-06-19 07:42:06 +02:00
$class = "\\App\\Models\\".ucfirst(Str::camel(rtrim($this->entity, 's')));
return $class::withTrashed()->find(is_string($this->entity_id) ? $this->decodePrimaryKey($this->entity_id) : $this->entity_id);
}
}