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;
 }
 public static function createSendGridEmailAccount(User $user)
 {
     $emailAccount = new SendGridEmailAccount();
     $emailAccount->user = $user;
     $emailAccount->name = EmailAccount::DEFAULT_NAME;
     $emailAccount->fromName = $user->getFullName();
     $emailAccount->fromAddress = '*****@*****.**';
     $emailAccount->apiUsername = Yii::app()->params['emailTestAccounts']['sendGridUserSettings']['apiUsername'];
     $emailAccount->apiPassword = Yii::app()->params['emailTestAccounts']['sendGridUserSettings']['apiPassword'];
     $emailAccount->save();
     return $emailAccount;
 }
 /**
  * @param User $user
  * @param mixed $name null or String representing the email account name
  */
 public static function getByUserAndName(User $user, $name = null)
 {
     if ($name == null) {
         $name = self::DEFAULT_NAME;
     } else {
         //For now Zurmo does not support multiple email accounts
         throw new NotSupportedException();
     }
     assert('is_string($name)');
     $bean = ZurmoRedBean::findOne(SendGridEmailAccount::getTableName(), "_user_id = ? AND name = ?", array($user->id, $name));
     assert('$bean === false || $bean instanceof RedBean_OODBBean');
     if ($bean === false) {
         throw new NotFoundException();
     } else {
         $emailAccount = self::makeModel($bean);
     }
     return $emailAccount;
 }
 /**
  * (non-PHPdoc)
  * @see BaseJob::run()
  */
 public function run()
 {
     $sendGridEmailAccounts = SendGridEmailAccount::getAll();
     foreach ($sendGridEmailAccounts as $sendGridEmailAccount) {
         $eventWebhookFilePath = SendGridLogUtil::getLogFilePath($sendGridEmailAccount->apiUsername);
         if ($eventWebhookFilePath != null && file_exists($eventWebhookFilePath)) {
             $this->processEventData($eventWebhookFilePath);
             @file_put_contents($eventWebhookFilePath, '');
         }
     }
     //Global
     if (Yii::app()->sendGridEmailHelper->apiUsername != '') {
         $globalWebHookFilePath = SendGridLogUtil::getLogFilePath(Yii::app()->sendGridEmailHelper->apiUsername);
         if ($globalWebHookFilePath != null && file_exists($globalWebHookFilePath)) {
             $this->processEventData($globalWebHookFilePath);
             @file_put_contents($globalWebHookFilePath, '');
         }
     }
     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;
 }
 /**
  * Resolve mailer by email message.
  * @return \ZurmoSwiftMailer|\ZurmoSendGridMailer
  */
 public function resolveMailer()
 {
     $apiUser = Yii::app()->sendGridEmailHelper->apiUsername;
     $apiPassword = Yii::app()->sendGridEmailHelper->apiPassword;
     $user = $this->emailMessage->owner;
     $defaultMailerClassName = static::resolveDefaultMailerClassName();
     if ($user != null) {
         $this->sendGridEmailAccount = SendGridEmailAccount::resolveAndGetByUserAndName($user, null, false);
         $this->emailAccount = EmailAccount::resolveAndGetByUserAndName($user, null, false);
     }
     if ($this->sendGridPluginEnabled && $user != null) {
         //Should user settings be used
         if ($this->shouldSendGridUserSettingsBeUsed()) {
             return new ZurmoSendGridMailer($this->emailMessage, $this->sendGridEmailAccount);
         } else {
             //Check for personal settings
             if ($this->shouldCustomUserSettingsBeUsed()) {
                 return new $defaultMailerClassName($this->emailMessage, $this->emailAccount);
             } else {
                 if ($apiUser != null && $apiPassword != null) {
                     $this->updateMailerDetailsForEmailMessage('sendgrid', 'global');
                     return new ZurmoSendGridMailer($this->emailMessage, null);
                 } else {
                     $this->updateMailerDetailsForEmailMessage('smtp', 'global');
                     return new $defaultMailerClassName($this->emailMessage, null);
                 }
             }
         }
     } elseif ($user != null && $this->shouldCustomUserSettingsBeUsed() === true) {
         return new $defaultMailerClassName($this->emailMessage, $this->emailAccount);
     } elseif ($this->sendGridPluginEnabled && $apiUser != null && $apiPassword != null) {
         $this->updateMailerDetailsForEmailMessage('sendgrid', 'global');
         return new ZurmoSendGridMailer($this->emailMessage, null);
     } else {
         $this->updateMailerDetailsForEmailMessage('smtp', 'global');
         return new $defaultMailerClassName($this->emailMessage, null);
     }
 }
 /**
  * Configure sendgrid options.
  * @param int $id
  * @param string $redirectUrl
  * @return void
  */
 protected function processSendGridPostConfiguration($id, $redirectUrl = null)
 {
     SendGridAccessUtil::resolveCanCurrentUserAccessAction(intval($id));
     $user = User::getById(intval($id));
     $emailAccount = SendGridEmailAccount::resolveAndGetByUserAndName($user);
     $userSendGridConfigurationForm = new UserSendGridConfigurationForm($emailAccount);
     $userSendGridConfigurationForm->emailSignatureHtmlContent = $user->getEmailSignature()->htmlContent;
     $postVariableName = get_class($userSendGridConfigurationForm);
     if (isset($_POST[$postVariableName])) {
         $userSendGridConfigurationForm->setAttributes($_POST[$postVariableName]);
         if ($userSendGridConfigurationForm->validate()) {
             $userSendGridConfigurationForm->save();
             Yii::app()->user->setFlash('notification', Zurmo::t('UsersModule', 'User SendGrid API configuration saved successfully.'));
             if ($redirectUrl != null) {
                 $this->redirect($redirectUrl);
             } else {
                 $this->redirect(array($this->getId() . '/details', 'id' => $user->id));
             }
         } else {
             $userSendGridConfigurationForm->apiPassword = ZurmoPasswordSecurityUtil::decrypt($userSendGridConfigurationForm->apiPassword);
         }
     }
     return $userSendGridConfigurationForm;
 }
 /**
  * @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);
 }