protected function preFilter($filterChain)
 {
     $sendGridPluginEnabled = (bool) ZurmoConfigurationUtil::getByModuleName('SendGridModule', 'enableSendgrid');
     try {
         if ($sendGridPluginEnabled) {
             SendGridEmailAccount::getByUserAndName(Yii::app()->user->userModel, null);
         } else {
             EmailAccount::getByUserAndName(Yii::app()->user->userModel);
         }
     } catch (NotFoundException $e) {
         $redirectUrl = Yii::app()->request->getParam('redirectUrl');
         if ($sendGridPluginEnabled) {
             try {
                 EmailAccount::getByUserAndName(Yii::app()->user->userModel);
             } catch (NotFoundException $ex) {
                 $messageView = new NoUserEmailConfigurationYetView($redirectUrl);
                 $view = new ModalView($this->controller, $messageView);
                 Yii::app()->getClientScript()->setToAjaxMode();
                 echo $view->render();
                 return false;
             }
         } else {
             $messageView = new NoUserEmailConfigurationYetView($redirectUrl);
             $view = new ModalView($this->controller, $messageView);
             Yii::app()->getClientScript()->setToAjaxMode();
             echo $view->render();
             return false;
         }
     }
     return true;
 }
 /**
  * Given post data and an email message, populate the sender and account on the email message if possible.
  * Also add message recipients and any attachments.
  * @param array $postData
  * @param CreateEmailMessageForm $emailMessageForm
  * @param User $userToSendMessagesFrom
  * @return CreateEmailMessageForm
  */
 public static function resolveEmailMessageFromPostData(array &$postData, CreateEmailMessageForm $emailMessageForm, User $userToSendMessagesFrom)
 {
     $postVariableName = get_class($emailMessageForm);
     $toRecipients = explode(",", $postData[$postVariableName]['recipientsData']['to']);
     // Not Coding Standard
     static::attachRecipientsToMessage($toRecipients, $emailMessageForm->getModel(), EmailMessageRecipient::TYPE_TO);
     if (ArrayUtil::getArrayValue($postData[$postVariableName]['recipientsData'], 'cc') != null) {
         $ccRecipients = explode(",", $postData[$postVariableName]['recipientsData']['cc']);
         // Not Coding Standard
         static::attachRecipientsToMessage($ccRecipients, $emailMessageForm->getModel(), EmailMessageRecipient::TYPE_CC);
     }
     if (ArrayUtil::getArrayValue($postData[$postVariableName]['recipientsData'], 'bcc') != null) {
         $bccRecipients = explode(",", $postData[$postVariableName]['recipientsData']['bcc']);
         // Not Coding Standard
         static::attachRecipientsToMessage($bccRecipients, $emailMessageForm->getModel(), EmailMessageRecipient::TYPE_BCC);
     }
     if (isset($postData['filesIds'])) {
         static::attachFilesToMessage($postData['filesIds'], $emailMessageForm->getModel());
     }
     $sender = new EmailMessageSender();
     $sendGridPluginEnabled = (bool) ZurmoConfigurationUtil::getByModuleName('SendGridModule', 'enableSendgrid');
     if ($sendGridPluginEnabled) {
         try {
             $emailAccount = SendGridEmailAccount::getByUserAndName($userToSendMessagesFrom);
             $emailMessageForm->sendGridAccount = $emailAccount;
         } catch (NotFoundException $e) {
             $emailAccount = EmailAccount::getByUserAndName($userToSendMessagesFrom);
             $emailMessageForm->account = $emailAccount;
         }
     } else {
         $emailAccount = EmailAccount::getByUserAndName($userToSendMessagesFrom);
         $emailMessageForm->account = $emailAccount;
     }
     $sender->fromName = $emailAccount->fromName;
     $sender->fromAddress = $emailAccount->fromAddress;
     $sender->personsOrAccounts->add($userToSendMessagesFrom);
     $emailMessageForm->sender = $sender;
     $box = EmailBoxUtil::getDefaultEmailBoxByUser($userToSendMessagesFrom);
     $emailMessageForm->folder = EmailFolder::getByBoxAndType($box, EmailFolder::TYPE_OUTBOX);
     return $emailMessageForm;
 }
 /**
  * @depends testResolveAndGetByUserAndName
  */
 public function testGetByUserAndName()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $emailAccount = SendGridEmailAccount::getByUserAndName($super);
     $this->assertEquals('Default', $emailAccount->name);
     $this->assertEquals($super, $emailAccount->user);
     $this->assertEquals($super->getFullName(), $emailAccount->fromName);
     $this->assertEquals('*****@*****.**', $emailAccount->fromAddress);
 }