checkUserAbleToSend(); } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'template' => 'bail|required', 'entity' => 'bail|required', 'entity_id' => 'bail|required', 'cc_email' => 'bail|sometimes|email|nullable', ]; } 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']); } if (array_key_exists('entity_id', $input)) { $input['entity_id'] = $this->decodePrimaryKey($input['entity_id']); } if (isset($input['entity'])) { $input['entity'] = "App\Models\\".ucfirst(Str::camel($input['entity'])); } $this->replace($input); } public function message() { return [ 'template' => 'Invalid template.', ]; } private function checkUserAbleToSend() { $input = $this->all(); if (Ninja::isHosted() && !auth()->user()->account->account_sms_verified) { $this->error_message = ctrans('texts.authorization_sms_failure'); return false; } /*Make sure we have all the require ingredients to send a template*/ if (isset($input['entity']) && array_key_exists('entity_id', $input) && is_string($input['entity']) && $input['entity_id']) { /** @var \App\Models\User $user */ $user = auth()->user(); $company = $user->company(); $entity = $input['entity']; /* Harvest the entity*/ $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) && $user->can('edit', $entity_obj)) { return true; } } else { $this->error_message = "Invalid entity or entity_id"; } return false; } protected function failedAuthorization() { throw new AuthorizationException($this->error_message); } }