Skip to content

Commit

Permalink
Fix: Ensure e-mails would always have a sender address set
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-rueegg committed Aug 17, 2023
1 parent 28fa6d9 commit a791189
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 110 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ HumHub Changelog

1.15.0-beta.2 (Unreleased)
--------------------------
- Fix #6519: Ensure e-mails would always have a sender address set
- Enh #6478: Add pseudo test class to allow population of DB with standard test data
- Enh #6480: Convert assert* and db* methods to static, in line with general usage pattern
- Enh #6505: Introduce Application interface; now also fire the `onInit` event when the web application has initialized
Expand Down
59 changes: 7 additions & 52 deletions protected/humhub/components/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,31 @@

namespace humhub\components;

use humhub\interfaces\ApplicationInterface;
use Yii;
use yii\helpers\Url;
use yii\base\Exception;
use Exception;

/**
* Description of Application
*
* @inheritdoc
*/
class Application extends \yii\web\Application implements \humhub\interfaces\Application
class Application extends \yii\web\Application implements ApplicationInterface
{
use ApplicationTrait;

/**
* @inheritdoc
*/
public $controllerNamespace = 'humhub\\controllers';

/**
* @var string|array the homepage url
*/
private $_homeUrl = null;

/**
* @var string Minimum PHP version that recommended to work without issues
*/
public $minRecommendedPhpVersion;

/**
* @var string Minimum PHP version that may works but probably with small issues
*/
public $minSupportedPhpVersion;

/**
* @inheritdoc
*/
public function __construct($config = [])
{
// Remove obsolete config params:
unset($config['components']['formatterApp']);

parent::__construct($config);
}

/**
* @inheritdoc
*/
public function init()
{
if (version_compare(phpversion(), $this->minSupportedPhpVersion, '<')) {
throw new \Exception(sprintf(
throw new Exception(sprintf(
'Installed PHP Version is too old! Required minimum version is PHP %s (Installed: %s)',
$this->minSupportedPhpVersion,
phpversion()
Expand Down Expand Up @@ -84,28 +61,6 @@ public function bootstrap()
parent::bootstrap();
}

/**
* @return string the homepage URL
*/
public function getHomeUrl()
{
if ($this->_homeUrl === null) {
return Url::to(['/dashboard/dashboard']);
} elseif (is_array($this->_homeUrl)) {
return Url::to($this->_homeUrl);
} else {
return $this->_homeUrl;
}
}

/**
* @param string|array $value the homepage URL
*/
public function setHomeUrl($value)
{
$this->_homeUrl = $value;
}

/**
* @inheritdoc
*/
Expand Down
69 changes: 69 additions & 0 deletions protected/humhub/components/ApplicationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\components;

use humhub\interfaces\MailerInterface;
use yii\helpers\Url;

trait ApplicationTrait
{
/**
* @var string|array the homepage url
*/
protected $_homeUrl = null;

/**
* @var string Minimum PHP version that recommended to work without issues
*/
public $minRecommendedPhpVersion;

/**
* @var string Minimum PHP version that may works but probably with small issues
*/
public $minSupportedPhpVersion;

/**
* @inheritdoc
*/
public function __construct($config = [])
{
// Remove obsolete config params:
unset($config['components']['formatterApp']);

parent::__construct($config);
}

/**
* @return string the homepage URL
*/
public function getHomeUrl(): string
{
if ($this->_homeUrl === null) {
return Url::to(['/dashboard/dashboard']);
}

if (is_array($this->_homeUrl)) {
return Url::to($this->_homeUrl);
}

return $this->_homeUrl;
}

/**
* @param string|array $value the homepage URL
*/
public function setHomeUrl($value)
{
$this->_homeUrl = $value;
}

public function getMailer(): MailerInterface
{
return parent::getMailer();
}
}
53 changes: 6 additions & 47 deletions protected/humhub/components/console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,25 @@

namespace humhub\components\console;

use humhub\components\ApplicationTrait;
use humhub\interfaces\ApplicationInterface;
use humhub\libs\BaseSettingsManager;
use Yii;
use yii\console\Exception;
use yii\helpers\Url;

/**
* Description of Application
*
* @author luke
* @inheritdoc
*/
class Application extends \yii\console\Application implements \humhub\interfaces\Application
class Application extends \yii\console\Application implements ApplicationInterface
{
/**
* @var string|array the homepage url
*/
private $_homeUrl = null;

/**
* @var string Minimum PHP version that recommended to work without issues
*/
public $minRecommendedPhpVersion;

/**
* @var string Minimum PHP version that may works but probably with small issues
*/
public $minSupportedPhpVersion;
use ApplicationTrait;

/**
* @inheritdoc
*/
public function __construct($config = [])
{
// Remove obsolete config params:
unset($config['components']['formatterApp']);

parent::__construct($config);
}
public $controllerNamespace = 'humhub\\controllers';

/**
* @inheritdoc
Expand Down Expand Up @@ -102,27 +84,4 @@ public function coreCommands()
'fixture' => 'yii\console\controllers\FixtureController',
];
}

/**
* @return string the homepage URL
*/
public function getHomeUrl()
{
if ($this->_homeUrl === null) {
return Url::to(['/dashboard/dashboard']);
} elseif (is_array($this->_homeUrl)) {
return Url::to($this->_homeUrl);
} else {
return $this->_homeUrl;
}
}

/**
* @param string|array $value the homepage URL
*/
public function setHomeUrl($value)
{
$this->_homeUrl = $value;
}

}
31 changes: 23 additions & 8 deletions protected/humhub/components/mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

namespace humhub\components\mail;

use humhub\interfaces\MailerInterface;
use Symfony\Component\Mime\Crypto\SMimeSigner;
use Yii;
use yii\mail\MessageInterface;

/**
* Mailer implements a mailer based on SymfonyMailer.
Expand All @@ -18,7 +20,7 @@
* @since 1.2
* @author Luke
*/
class Mailer extends \yii\symfonymailer\Mailer
class Mailer extends \yii\symfonymailer\Mailer implements MailerInterface
{
/**
* @inheritdoc
Expand Down Expand Up @@ -68,13 +70,7 @@ public function compose($view = null, array $params = [])
{
$message = parent::compose($view, $params);

// Set HumHub default from values
if (empty($message->getFrom())) {
$message->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => Yii::$app->settings->get('mailer.systemEmailName')]);
if ($replyTo = Yii::$app->settings->get('mailer.systemEmailReplyTo')) {
$message->setReplyTo($replyTo);
}
}
self::ensureHumHubDefaultFromValues($message);

if ($this->signingCertificatePath !== null && $this->signingPrivateKeyPath !== null) {
if ($this->signer === null) {
Expand All @@ -92,6 +88,25 @@ public function compose($view = null, array $params = [])
return $message;
}

/**
* @param MessageInterface $message
*
* @return void
*/
public static function ensureHumHubDefaultFromValues(MessageInterface $message): MessageInterface
{
// Set HumHub default from values
if ($message->getFrom()) {
return $message;
}

$message->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => Yii::$app->settings->get('mailer.systemEmailName')]);
if ($replyTo = Yii::$app->settings->get('mailer.systemEmailReplyTo')) {
$message->setReplyTo($replyTo);
}

return $message;
}

/**
* @inheritdoc
Expand Down
2 changes: 1 addition & 1 deletion protected/humhub/config/__autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class Yii {
/**
* @var \yii\web\Application|\yii\console\Application|\humhub\components\Application|\humhub\components\console\Application|\humhub\interfaces\Application|__Application|__WebApplication
* @var \yii\web\Application|\yii\console\Application|\humhub\components\Application|\humhub\components\console\Application|\humhub\interfaces\ApplicationInterface|__Application|__WebApplication
*/
public static $app;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
*
* @since 1.15
*/
interface Application
interface ApplicationInterface
{
/**
* @event ActionEvent an event raised on init of application.
*/
public const EVENT_ON_INIT = 'onInit';

public function getMailer(): MailerInterface;
}
13 changes: 13 additions & 0 deletions protected/humhub/interfaces/MailerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace humhub\interfaces;

interface MailerInterface extends \yii\mail\MailerInterface
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function handle(BaseNotification $notification, User $recipient)
], $notification->getViewParams());

$mail = Yii::$app->mailer->compose($this->view, $viewParams)
->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => Yii::$app->settings->get('mailer.systemEmailName')])
->setTo($recipient->email)
->setSubject(str_replace("\n", " ", trim($notification->getMailSubject())));
if ($replyTo = Yii::$app->settings->get('mailer.systemEmailReplyTo')) {
Expand Down
25 changes: 25 additions & 0 deletions protected/humhub/tests/codeception/_support/TestMailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/

namespace tests\codeception\_support;

use humhub\components\mail\Mailer;
use humhub\interfaces\MailerInterface;

/**
* @since 1.15
*/
abstract class TestMailer extends \Codeception\Lib\Connector\Yii2\TestMailer implements MailerInterface
{
public function compose($view = null, array $params = [])
{
$message = parent::compose($view, $params);

return Mailer::ensureHumHubDefaultFromValues($message);
}
}
2 changes: 2 additions & 0 deletions protected/humhub/tests/codeception/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* Application configuration shared by all test types
*/

$default = [
'name' => 'HumHub Test',
'language' => 'en-US',
Expand All @@ -25,6 +26,7 @@
],
'components' => [
'mailer' => [
'class' => \tests\codeception\_support\TestMailer::class,
'messageClass' => \yii\symfonymailer\Message::class,
],
'urlManager' => [
Expand Down

0 comments on commit a791189

Please sign in to comment.