mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-10-31 08:17:32 -04:00 
			
		
		
		
	
		
			
				
	
	
		
			50 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php namespace App\Http\Requests;
 | |
| 
 | |
| use App\Http\Requests\Request;
 | |
| use Input;
 | |
| use Utils;
 | |
| 
 | |
| class EntityRequest extends Request {
 | |
| 
 | |
|     protected $entityType;
 | |
|     private $entity;
 | |
| 
 | |
|     public function entity() 
 | |
|     {
 | |
|         if ($this->entity) {
 | |
|             return $this->entity;
 | |
|         }
 | |
| 
 | |
|         $paramName = $this->entityType . 's';
 | |
|         $publicId = $this->$paramName ?: (Input::get('public_id') ?: Input::get('id'));
 | |
|         
 | |
|         if ( ! $publicId) {
 | |
|             return null;
 | |
|         } 
 | |
|         
 | |
|         $class = Utils::getEntityClass($this->entityType);
 | |
|         
 | |
|         if (method_exists($class, 'withTrashed')) {
 | |
|             $this->entity = $class::scope($publicId)->withTrashed()->firstOrFail();
 | |
|         } else {
 | |
|             $this->entity = $class::scope($publicId)->firstOrFail();
 | |
|         }
 | |
|         
 | |
|         return $this->entity;
 | |
|     }
 | |
| 
 | |
|     public function authorize()
 | |
|     {
 | |
|         if ($this->entity()) {
 | |
|             return $this->user()->can('view', $this->entity());
 | |
|         } else {
 | |
|             return $this->user()->can('create', $this->entityType);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function rules()
 | |
|     {
 | |
|         return [];
 | |
|     }
 | |
| }
 |