diff --git a/core/lib/Thelia/Action/MailingSystem.php b/core/lib/Thelia/Action/MailingSystem.php new file mode 100644 index 000000000..f46352d53 --- /dev/null +++ b/core/lib/Thelia/Action/MailingSystem.php @@ -0,0 +1,62 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Action; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Thelia\Core\Event\MailingSystem\MailingSystemEvent; +use Thelia\Core\Event\TheliaEvents; +use Thelia\Model\ConfigQuery; + +class MailingSystem extends BaseAction implements EventSubscriberInterface +{ + /** + * @param MailingSystemEvent $event + */ + public function update(MailingSystemEvent $event) + { + if($event->getEnabled()) { + ConfigQuery::enableSmtp(); + } else { + ConfigQuery::disableSmtp(); + } + ConfigQuery::setSmtpHost($event->getHost()); + ConfigQuery::setSmtpPort($event->getPort()); + ConfigQuery::setSmtpEncryption($event->getEncryption()); + ConfigQuery::setSmtpUsername($event->getUsername()); + ConfigQuery::setSmtpPassword($event->getPassword()); + ConfigQuery::setSmtpAuthMode($event->getAuthMode()); + ConfigQuery::setSmtpTimeout($event->getTimeout()); + ConfigQuery::setSmtpSourceIp($event->getSourceIp()); + } + + /** + * {@inheritDoc} + */ + public static function getSubscribedEvents() + { + return array( + TheliaEvents::MAILING_SYSTEM_UPDATE => array("update", 128), + ); + } +} diff --git a/core/lib/Thelia/Config/Resources/action.xml b/core/lib/Thelia/Config/Resources/action.xml index 1901edfec..60c435a05 100755 --- a/core/lib/Thelia/Config/Resources/action.xml +++ b/core/lib/Thelia/Config/Resources/action.xml @@ -166,6 +166,11 @@ + + + + + diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 6f635df9c..c0af99896 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -148,6 +148,8 @@ + + diff --git a/core/lib/Thelia/Config/Resources/routing/admin.xml b/core/lib/Thelia/Config/Resources/routing/admin.xml index afaacb85a..259c0f54a 100755 --- a/core/lib/Thelia/Config/Resources/routing/admin.xml +++ b/core/lib/Thelia/Config/Resources/routing/admin.xml @@ -812,6 +812,18 @@ + + + + Thelia\Controller\Admin\MailingSystemController::defaultAction + + + + Thelia\Controller\Admin\MailingSystemController::updateAction + + + + diff --git a/core/lib/Thelia/Controller/Admin/MailingSystemController.php b/core/lib/Thelia/Controller/Admin/MailingSystemController.php index 5bf2f4e73..cfe6f06dd 100644 --- a/core/lib/Thelia/Controller/Admin/MailingSystemController.php +++ b/core/lib/Thelia/Controller/Admin/MailingSystemController.php @@ -23,20 +23,91 @@ namespace Thelia\Controller\Admin; -use Thelia\Core\Security\Resource\AdminResources; +use Thelia\Core\Event\MailingSystem\MailingSystemEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Security\AccessManager; +use Thelia\Form\Exception\FormValidationException; +use Thelia\Form\MailingSystemModificationForm; +use Thelia\Model\ConfigQuery; -/** - * Class MailingSystemController - * @package Thelia\Controller\Admin - * @author Manuel Raynaud - */ class MailingSystemController extends BaseAdminController { + const RESOURCE_CODE = "admin.mailing-system"; + public function defaultAction() { - if (null !== $response = $this->checkAuth(AdminResources::MAILING_SYSTEM, AccessManager::VIEW)) return $response; - return $this->render("mailing-system"); + if (null !== $response = $this->checkAuth(self::RESOURCE_CODE, AccessManager::VIEW)) return $response; + + // Hydrate the form abd pass it to the parser + $data = array( + 'enabled' => ConfigQuery::isSmtpEnable() ? 1 : 0, + 'host' => ConfigQuery::getSmtpHost(), + 'port' => ConfigQuery::getSmtpPort(), + 'encryption' => ConfigQuery::getSmtpEncryption(), + 'username' => ConfigQuery::getSmtpUsername(), + 'password' => ConfigQuery::getSmtpPassword(), + 'authmode' => ConfigQuery::getSmtpAuthMode(), + 'timeout' => ConfigQuery::getSmtpTimeout(), + 'sourceip' => ConfigQuery::getSmtpSourceIp(), + ); + + // Setup the object form + $form = new MailingSystemModificationForm($this->getRequest(), "form", $data); + + // Pass it to the parser + $this->getParserContext()->addForm($form); + + // Render the edition template. + return $this->render('mailing-system'); } + public function updateAction() + { + // Check current user authorization + if (null !== $response = $this->checkAuth(self::RESOURCE_CODE, AccessManager::UPDATE)) return $response; + + $error_msg = false; + + // Create the form from the request + $form = new MailingSystemModificationForm($this->getRequest()); + + try { + + // Check the form against constraints violations + $formData = $this->validateForm($form, "POST"); + + // Get the form field values + $event = new MailingSystemEvent(); + $event->setEnabled($formData->get('enabled')->getData()); + $event->setHost($formData->get('host')->getData()); + $event->setPort($formData->get('port')->getData()); + $event->setEncryption($formData->get('encryption')->getData()); + $event->setUsername($formData->get('username')->getData()); + $event->setPassword($formData->get('password')->getData()); + $event->setAuthMode($formData->get('authmode')->getData()); + $event->setTimeout($formData->get('timeout')->getData()); + $event->setSourceIp($formData->get('sourceip')->getData()); + + $this->dispatch(TheliaEvents::MAILING_SYSTEM_UPDATE, $event); + + // Redirect to the success URL + $this->redirectToRoute("admin.configuration.mailing-system.view"); + } catch (FormValidationException $ex) { + // Form cannot be validated + $error_msg = $this->createStandardFormValidationErrorMessage($ex); + } catch (\Exception $ex) { + // Any other error + $error_msg = $ex->getMessage(); + } + + $this->setupFormErrorContext( + $this->getTranslator()->trans("mailing system modification", array()), + $error_msg, + $form, + $ex + ); + + // At this point, the form has errors, and should be redisplayed. + return $this->render('mailing-system'); + } } diff --git a/core/lib/Thelia/Core/Event/MailingSystem/MailingSystemEvent.php b/core/lib/Thelia/Core/Event/MailingSystem/MailingSystemEvent.php new file mode 100644 index 000000000..02cb25e6e --- /dev/null +++ b/core/lib/Thelia/Core/Event/MailingSystem/MailingSystemEvent.php @@ -0,0 +1,183 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\Event\MailingSystem; + +use Thelia\Core\Event\ActionEvent; + +class MailingSystemEvent extends ActionEvent +{ + protected $enabled = null; + protected $host = null; + protected $port = null; + protected $encryption = null; + protected $username = null; + protected $password = null; + protected $authMode = null; + protected $timeout = null; + protected $sourceIp = null; + + /** + * @param null $authMode + */ + public function setAuthMode($authMode) + { + $this->authMode = $authMode; + } + + /** + * @return null + */ + public function getAuthMode() + { + return $this->authMode; + } + + /** + * @param null $enabled + */ + public function setEnabled($enabled) + { + $this->enabled = $enabled; + } + + /** + * @return null + */ + public function getEnabled() + { + return $this->enabled; + } + + /** + * @param null $encryption + */ + public function setEncryption($encryption) + { + $this->encryption = $encryption; + } + + /** + * @return null + */ + public function getEncryption() + { + return $this->encryption; + } + + /** + * @param null $host + */ + public function setHost($host) + { + $this->host = $host; + } + + /** + * @return null + */ + public function getHost() + { + return $this->host; + } + + /** + * @param null $password + */ + public function setPassword($password) + { + $this->password = $password; + } + + /** + * @return null + */ + public function getPassword() + { + return $this->password; + } + + /** + * @param null $port + */ + public function setPort($port) + { + $this->port = $port; + } + + /** + * @return null + */ + public function getPort() + { + return $this->port; + } + + /** + * @param null $sourceIp + */ + public function setSourceIp($sourceIp) + { + $this->sourceIp = $sourceIp; + } + + /** + * @return null + */ + public function getSourceIp() + { + return $this->sourceIp; + } + + /** + * @param null $timeout + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + } + + /** + * @return null + */ + public function getTimeout() + { + return $this->timeout; + } + + /** + * @param null $username + */ + public function setUsername($username) + { + $this->username = $username; + } + + /** + * @return null + */ + public function getUsername() + { + return $this->username; + } +} diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 5bb6028a9..42b20d8cd 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -566,6 +566,10 @@ final class TheliaEvents const ADMINISTRATOR_UPDATE = "action.updateAdministrator"; const ADMINISTRATOR_DELETE = "action.deleteAdministrator"; + // -- Mailing System management --------------------------------------------- + + const MAILING_SYSTEM_UPDATE = "action.updateMailingSystem"; + // -- Tax Rules management --------------------------------------------- const TAX_RULE_CREATE = "action.createTaxRule"; diff --git a/core/lib/Thelia/Form/MailingSystemModificationForm.php b/core/lib/Thelia/Form/MailingSystemModificationForm.php new file mode 100644 index 000000000..8334777ab --- /dev/null +++ b/core/lib/Thelia/Form/MailingSystemModificationForm.php @@ -0,0 +1,95 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints; +use Symfony\Component\Validator\Constraints\NotBlank; +use Symfony\Component\Validator\ExecutionContextInterface; +use Thelia\Core\Translation\Translator; +use Thelia\Model\ProfileQuery; + +/** + * Class MailingSystemModificationForm + * @package Thelia\Form + * @author Etienne Roudeix + */ +class MailingSystemModificationForm extends BaseForm +{ + protected function buildForm($change_mode = false) + { + $this->formBuilder + ->add("enabled", "choice", array( + "choices" => array(1 => "Yes", 0 => "No"), + "label" => Translator::getInstance()->trans("Enable remote SMTP use"), + "label_attr" => array("for" => "enabled_field"), + )) + ->add("host", "text", array( + "label" => Translator::getInstance()->trans("Host"), + "label_attr" => array("for" => "host_field"), + )) + ->add("port", "text", array( + "label" => Translator::getInstance()->trans("Port"), + "label_attr" => array("for" => "port_field"), + )) + ->add("encryption", "text", array( + "label" => Translator::getInstance()->trans("Encryption"), + "label_attr" => array("for" => "encryption_field"), + )) + ->add("username", "text", array( + "label" => Translator::getInstance()->trans("Username"), + "label_attr" => array("for" => "username_field"), + )) + ->add("password", "text", array( + "label" => Translator::getInstance()->trans("Password"), + "label_attr" => array("for" => "password_field"), + )) + ->add("authmode", "text", array( + "label" => Translator::getInstance()->trans("Auth mode"), + "label_attr" => array("for" => "authmode_field"), + )) + ->add("timeout", "text", array( + "label" => Translator::getInstance()->trans("Timeout"), + "label_attr" => array("for" => "timeout_field"), + )) + ->add("sourceip", "text", array( + "label" => Translator::getInstance()->trans("Source IP"), + "label_attr" => array("for" => "sourceip_field"), + )) + ; + } + + public function getName() + { + return "thelia_mailing_system_modification"; + } + + /*public function verifyCode($value, ExecutionContextInterface $context) + { + $profile = ProfileQuery::create() + ->findOneByCode($value); + + if (null !== $profile) { + $context->addViolation("Profile `code` already exists"); + } + }*/ +} diff --git a/core/lib/Thelia/Mailer/MailerFactory.php b/core/lib/Thelia/Mailer/MailerFactory.php index a327a155f..e4fee490e 100644 --- a/core/lib/Thelia/Mailer/MailerFactory.php +++ b/core/lib/Thelia/Mailer/MailerFactory.php @@ -52,7 +52,7 @@ class MailerFactory { if($transporterEvent->hasTransporter()) { $transporter = $transporterEvent->getTransporter(); } else { - if (ConfigQuery::read("smtp.enabled")) { + if (ConfigQuery::isSmtpEnable()) { $transporter = $this->configureSmtp(); } else { $transporter = \Swift_MailTransport::newInstance(); @@ -65,14 +65,14 @@ class MailerFactory { private function configureSmtp() { $smtpTransporter = new \Swift_SmtpTransport(); - $smtpTransporter->setHost(Configquery::read('smtp.host', 'localhost')) - ->setPort(ConfigQuery::read('smtp.host')) - ->setEncryption(ConfigQuery::read('smtp.encryption')) - ->setUsername(ConfigQuery::read('smtp.username')) - ->setPassword(ConfigQuery::read('smtp.password')) - ->setAuthMode(ConfigQuery::read('smtp.authmode')) - ->setTimeout(ConfigQuery::read('smtp.timeout', 30)) - ->setSourceIp(ConfigQuery::read('smtp.sourceip')) + $smtpTransporter->setHost(Configquery::getSmtpHost()) + ->setPort(ConfigQuery::getSmtpPort()) + ->setEncryption(ConfigQuery::getSmtpEncryption()) + ->setUsername(ConfigQuery::getSmtpUsername()) + ->setPassword(ConfigQuery::getSmtpPassword()) + ->setAuthMode(ConfigQuery::getSmtpAuthMode()) + ->setTimeout(ConfigQuery::getSmtpTimeout()) + ->setSourceIp(ConfigQuery::getSmtpSourceIp()) ; return $smtpTransporter; } diff --git a/core/lib/Thelia/Model/ConfigQuery.php b/core/lib/Thelia/Model/ConfigQuery.php index fb8d6fe90..11bbc51d6 100755 --- a/core/lib/Thelia/Model/ConfigQuery.php +++ b/core/lib/Thelia/Model/ConfigQuery.php @@ -32,6 +32,23 @@ class ConfigQuery extends BaseConfigQuery { return self::$cache[$search]; } + public static function write($configName, $value, $secured, $hidden) + { + $config = self::create()->findOneByName($configName); + + if(null == $config) { + $config = new Config(); + $config->setName($configName); + } + + $config->setSecured($secured ? 1 : 0); + $config->setHidden($hidden ? 1 : 0); + $config->setValue($value); + $config->save(); + + self::$cache[$configName] = $value; + } + public static function resetCache($key = null) { if($key) { @@ -74,4 +91,101 @@ class ConfigQuery extends BaseConfigQuery { { return self::read('use_tax_free_amounts', 'default') == 1; } + + /* smtp config */ + public static function isSmtpEnable() + { + return self::read('smtp.enabled') == 1; + } + + public static function getSmtpHost() + { + return self::read('smtp.host', 'localhost'); + } + + public static function getSmtpPort() + { + return self::read('smtp.port'); + } + + public static function getSmtpEncryption() + { + return self::read('smtp.encryption'); + } + + public static function getSmtpUsername() + { + return self::read('smtp.username'); + } + + public static function getSmtpPassword() + { + return self::read('smtp.authmode'); + } + + public static function getSmtpAuthMode() + { + return self::read('smtp.host'); + } + + public static function getSmtpTimeout() + { + return self::read('smtp.timeout', 30); + } + + public static function getSmtpSourceIp() + { + return self::read('smtp.sourceip'); + } + + public static function enableSmtp() + { + self::write('smtp.enabled', 1, 1, 1); + } + + public static function disableSmtp() + { + self::write('smtp.enabled', 0, 1, 1); + } + + public static function setSmtpHost($value) + { + return self::write('smtp.host', $value, 1, 1); + } + + public static function setSmtpPort($value) + { + return self::write('smtp.port', $value, 1, 1); + } + + public static function setSmtpEncryption($value) + { + return self::write('smtp.encryption', $value, 1, 1); + } + + public static function setSmtpUsername($value) + { + return self::write('smtp.username', $value, 1, 1); + } + + public static function setSmtpPassword($value) + { + return self::write('smtp.password', $value, 1, 1); + } + + public static function setSmtpAuthMode($value) + { + return self::write('smtp.authmode', $value, 1, 1); + } + + public static function setSmtpTimeout($value) + { + return self::write('smtp.timeout', $value, 1, 1); + } + + public static function setSmtpSourceIp($value) + { + return self::write('smtp.sourceip', $value, 1, 1); + } + /* end smtp config */ } // ConfigQuery diff --git a/templates/admin/default/configuration.html b/templates/admin/default/configuration.html index 4c6acd1db..a17042b4d 100644 --- a/templates/admin/default/configuration.html +++ b/templates/admin/default/configuration.html @@ -151,14 +151,14 @@ {/loop} -{* {loop type="auth" name="pcc6" role="ADMIN" resource="admin.configuration.mailing-system" access="VIEW"} + {loop type="auth" name="pcc6" role="ADMIN" resource="admin.configuration.mailing-system" access="VIEW"} {intl l='Mailing system'} - + {/loop} - {loop type="auth" name="pcc7" role="ADMIN" resource="admin.configuration.admin-logs" access="VIEW"} +{* {loop type="auth" name="pcc7" role="ADMIN" resource="admin.configuration.admin-logs" access="VIEW"} {intl l='Administration logs'} diff --git a/templates/admin/default/mailing-system.html b/templates/admin/default/mailing-system.html index 984dfd8bd..8ee5e3e3e 100644 --- a/templates/admin/default/mailing-system.html +++ b/templates/admin/default/mailing-system.html @@ -24,68 +24,98 @@ {intl l="Configuration variables"} - - - - {intl l="SMTP Server"} - - - - - + {form name="thelia.admin.mailing-system.update"} + + + + + + Save - - {intl l="Port"} - - - - - + {form_hidden_fields form=$form} + + {if $form_error}{$form_error_message}{/if} + + {form_field form=$form field='enabled'} + + {intl l="Enable remote SMTP use : "} + + + + + + - + {/form_field} - - {intl l="Username"} - - - - - + {form_field form=$form field='host'} + + {intl l="Host :"} + - + {/form_field} - - {intl l="Password"} - - - - - + {form_field form=$form field='port'} + + {intl l="Port :"} + - + {/form_field} - - {intl l="Protocol"} - - - - - + {form_field form=$form field='encryption'} + + {intl l="Encryption :"} + - + {/form_field} - - {intl l="Active ?"} + {form_field form=$form field='username'} + + {intl l="Username :"} + + + {/form_field} - - + {form_field form=$form field='password'} + + {intl l="Password :"} + + + {/form_field} + + {form_field form=$form field='authmode'} + + {intl l="Auth Mode :"} + + + {/form_field} + + {form_field form=$form field='timeout'} + + {intl l="Timeout :"} + + + {/form_field} + + {form_field form=$form field='sourceip'} + + {intl l="Source IP :"} + + + {/form_field} + + + + Save + {/form} + @@ -100,4 +130,14 @@ {javascripts file='assets/js/bootstrap-switch/bootstrap-switch.js'} {/javascripts} + + {/block} \ No newline at end of file