UserCreateService: check if we're from web before setting flash message #195

Also return an error message to console if mail send failed
This commit is contained in:
Lorenzo Milesi
2018-07-13 18:36:04 +02:00
parent 5bdb0cd9db
commit 7a571f4800
2 changed files with 18 additions and 7 deletions

View File

@ -7,6 +7,7 @@
- Enh: Changed `View::render()` calls in views to use absolute paths (ajmedway) - Enh: Changed `View::render()` calls in views to use absolute paths (ajmedway)
- Fix #169: Fix bug in ReCaptchaComponent (BuTaMuH) - Fix #169: Fix bug in ReCaptchaComponent (BuTaMuH)
- Fix #168: Fix spelling in russian language (EvgenyOrekhov) - Fix #168: Fix spelling in russian language (EvgenyOrekhov)
- Fix #195: UserCreateService: check if we're from web before setting flash message (maxxer)
## 1.1.4 - February 19, 2018 ## 1.1.4 - February 19, 2018
- Enh: Check enableEmailConfirmation on registration (faenir) - Enh: Check enableEmailConfirmation on registration (faenir)

View File

@ -16,6 +16,7 @@ use Da\User\Event\UserEvent;
use Da\User\Helper\SecurityHelper; use Da\User\Helper\SecurityHelper;
use Da\User\Model\User; use Da\User\Model\User;
use Da\User\Traits\MailAwareTrait; use Da\User\Traits\MailAwareTrait;
use Da\User\Traits\ModuleAwareTrait;
use Exception; use Exception;
use Yii; use Yii;
use yii\base\InvalidCallException; use yii\base\InvalidCallException;
@ -23,6 +24,7 @@ use yii\base\InvalidCallException;
class UserCreateService implements ServiceInterface class UserCreateService implements ServiceInterface
{ {
use MailAwareTrait; use MailAwareTrait;
use ModuleAwareTrait;
protected $model; protected $model;
protected $securityHelper; protected $securityHelper;
@ -67,14 +69,22 @@ class UserCreateService implements ServiceInterface
$model->trigger(UserEvent::EVENT_AFTER_CREATE, $event); $model->trigger(UserEvent::EVENT_AFTER_CREATE, $event);
if (!$this->sendMail($model)) { if (!$this->sendMail($model)) {
Yii::$app->session->setFlash( $error_msg = Yii::t(
'warning', 'usuario',
Yii::t( 'Error sending welcome message to "{email}". Please try again later.',
'usuario', ['email' => $model->email]
'Error sending welcome message to "{email}". Please try again later.',
['email' => $model->email]
)
); );
// from web display a flash message (if enabled)
if($this->getModule()->enableFlashMessages == TRUE && is_a(Yii::$app, yii\web\Application::class)) {
Yii::$app->session->setFlash(
'warning',
$error_msg
);
}
// if we're from console add an error to the model in order to return an error message
if(is_a(Yii::$app, yii\console\Application::class)) {
$model->addError("username", $error_msg);
}
$transaction->rollBack(); $transaction->rollBack();
return false; return false;
} }