Przeglądaj źródła

[Admin] Utilisateurs > formulaire : ajouter bouton "Envoyer le mail de bienvenue" #1450

master
Guillaume Bourgeois 1 rok temu
rodzic
commit
866b8671d0
8 zmienionych plików z 112 dodań i 59 usunięć
  1. +33
    -49
      backend/controllers/UserController.php
  2. +13
    -7
      backend/views/user/_form.php
  3. +9
    -1
      common/logic/User/User/Module/UserModule.php
  4. +8
    -0
      common/logic/User/User/Service/UserBuilder.php
  5. +31
    -0
      common/logic/User/User/Service/UserManager.php
  6. +16
    -0
      common/logic/User/User/Service/UserNotifier.php
  7. +1
    -1
      common/mail/createUserAdmin-html.php
  8. +1
    -1
      common/mail/createUserAdmin-text.php

+ 33
- 49
backend/controllers/UserController.php Wyświetl plik

@@ -84,7 +84,7 @@ class UserController extends BackendController
* Liste les utilisateurs.
*/
public function actionIndex(
int $idPointSale = 0,
int $idPointSale = 0,
bool $sectionSubscribers = false,
bool $sectionInactiveUsers = false)
{
@@ -95,8 +95,8 @@ class UserController extends BackendController
'UserSearch' => array_merge(
[
'id_point_sale' => $idPointSale,
'inactive' => (int) $sectionInactiveUsers,
'subscribers' => (int) $sectionSubscribers
'inactive' => (int)$sectionInactiveUsers,
'subscribers' => (int)$sectionSubscribers
],
isset(\Yii::$app->request->queryParams['UserSearch']) ?
Yii::$app->request->queryParams['UserSearch'] :
@@ -149,7 +149,7 @@ class UserController extends BackendController
$model->address,
$model->newsletter,
Password::generate(),
(bool) $model->send_mail_welcome
(bool)$model->send_mail_welcome
);
$userCreate->points_sale = $model->points_sale;
$userCreate->user_groups = $model->user_groups;
@@ -177,7 +177,6 @@ class UserController extends BackendController

$model = $this->findModel($id);
$previousMail = $model->email;
$user = User::find()->with('userProducer')->where(['id' => $model['id']])->one();
$userBelongToProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]);
if ($userBelongToProducer) {
$model->newsletter = $userBelongToProducer->newsletter;
@@ -194,45 +193,33 @@ class UserController extends BackendController
$this->processLinkUserGroup($model);
//$this->processProductPricePercent($model);

if($model->newsletter) {
if ($model->newsletter) {
$userModule->getNewsletterManager()->subscribeUserNewsletter($model);
}
else {
} else {
$userModule->getNewsletterManager()->unsubscribeUserNewsletter($model);
}

$this->setFlash('success', 'Utilisateur <strong>'.Html::encode($userModule->getUsername($model)).'</strong> modifié.');

$this->setFlash('success', 'Utilisateur <strong>' . Html::encode($userModule->getUsername($model)) . '</strong> modifié.');
return $this->redirect(['index']);
}
} else {
throw new UserException("Vous ne pouvez pas modifier cet utilisateur.");
}

// Nouveau mot de passe
$newPassword = Yii::$app->request->post('submit_new_password');
if ($newPassword) {
$password = Password::generate();
$userModule->setPassword($model, $password);
$model->save();

$producer = $this->getProducerCurrent();

\Yii::$app->mailerService->sendFromProducer(
'Nouveau mot de passe',
'newPasswordUserAdmin',
[
'user' => $user,
'producer' => $producer,
'password' => $password,
],
$model->email,
$producer
);

$this->setFlash('success', 'Nouveau mot de passe envoyé à <strong>'.Html::encode($userModule->getUsername($model)).'</strong>.');
// Email de bienvenue
$mailWelcome = Yii::$app->request->post('submit_mail_welcome');
if ($mailWelcome) {
$this->getUserModule()->getManager()->welcome($model);
$this->setFlash('success', 'Email de bienvenue envoyé à <strong>' . Html::encode($userModule->getSolver()->getUsername($model)) . '</strong>.');
return $this->redirect(['update', 'id' => $model->id]);
}

return $this->redirect(['index']);
// Mot de passe oublié
$newPassword = Yii::$app->request->post('submit_new_password');
if ($newPassword) {
$this->getUserModule()->getManager()->newPassword($model);
$this->setFlash('success', 'Nouveau mot de passe envoyé à <strong>' . Html::encode($userModule->getSolver()->getUsername($model)) . '</strong>.');
return $this->redirect(['update', 'id' => $model->id]);
}
} else {
throw new UserException("Vous ne pouvez pas modifier cet utilisateur.");
}

return $this->render('update', array_merge($this->initForm($model), [
@@ -364,10 +351,9 @@ class UserController extends BackendController
$userProducer = $userProducerModule->findOneUserProducer($user);

if ($userProducer) {
if($userProducerModule->hasOutstandingCredit($userProducer)) {
if ($userProducerModule->hasOutstandingCredit($userProducer)) {
$this->setFlash('error', "Vous ne pouvez pas supprimer cet utilisateur car il a toujours du crédit en cours.");
}
else {
} else {
$userProducerModule->unlinkUserProducer($userProducer);
$this->setFlash('success', 'L\'utilisateur a bien été supprimé de votre établissement.');
}
@@ -410,14 +396,14 @@ class UserController extends BackendController
$creditForm->comment
);

if($creditForm->send_mail) {
if ($creditForm->send_mail) {
$paymentModule->getNotifier()
->notifyUserCreditMovement(
$user,
$creditForm->type,
$creditForm->amount
);
if(!$user->email) {
if (!$user->email) {
$this->addFlash('error', "L'utilisateur n'a pas pu être prévenu car son adresse email n'est pas définie.");
}
}
@@ -506,7 +492,7 @@ class UserController extends BackendController
$usersPointSaleHasOrder = 0)
{
$userModule = $this->getUserModule();
$distributionModule = $this-> getDistributionModule();
$distributionModule = $this->getDistributionModule();

if ($idPointSale && !$usersPointSaleLink && !$usersPointSaleHasOrder) {
$usersPointSaleLink = 1;
@@ -526,8 +512,7 @@ class UserController extends BackendController
foreach ($users as $key => $user) {
if (isset($user['email']) && strlen($user['email']) > 0) {
$usersArray[] = $user['email'];
}
else {
} else {
unset($users[$key]);
}
}
@@ -589,17 +574,16 @@ class UserController extends BackendController
public function actionSwitchIdentity(int $id)
{
$userModule = $this->getUserModule();
if($this->isUserCurrentAdmin()) {
if ($this->isUserCurrentAdmin()) {
$initialId = Yii::$app->user->getId();
if($initialId != $id) {
if ($initialId != $id) {
$user = $userModule->findOneUserById($id);
$duration = 0;
Yii::$app->user->switchIdentity($user, $duration);
Yii::$app->session->set('user.idbeforeswitch',$initialId);
Yii::$app->session->set('user.idbeforeswitch', $initialId);
return $this->redirect(['index']);
}
}
else {
} else {
throw new NotFoundHttpException('Page introuvable');
}
}

+ 13
- 7
backend/views/user/_form.php Wyświetl plik

@@ -118,13 +118,12 @@ $distributionModule = DistributionModule::getInstance();
</div>

<div class="col-md-4">

<?php if(isset($pointSaleBillingArray) && $pointSaleBillingArray && count($pointSaleBillingArray) > 0): ?>
<h3>Facturation</h3>
<?php foreach($pointSaleBillingArray as $pointSale): ?>
<a href="<?= Yii::$app->urlManager->createUrl(['point-sale/update', 'id' => $pointSale->id]) ?>"><?= $pointSale->name ?></a><br />
<?php endforeach; ?>
<?php endif; ?>
<h3>Email de bienvenue</h3>
<?php $form = ActiveForm::begin(); ?>
<div class="form-group">
<?= Html::submitButton("Envoyer l'email de bienvenue", ['class' => 'btn btn-primary', 'name' => 'submit_mail_welcome', 'value' => 1]) ?>
</div>
<?php ActiveForm::end(); ?>

<h3>Mot de passe</h3>
<?php $form = ActiveForm::begin(); ?>
@@ -132,5 +131,12 @@ $distributionModule = DistributionModule::getInstance();
<?= Html::submitButton('Envoyer un nouveau mot de passe', ['class' => 'btn btn-primary', 'name' => 'submit_new_password', 'value' => 1]) ?>
</div>
<?php ActiveForm::end(); ?>

<?php if(isset($pointSaleBillingArray) && $pointSaleBillingArray && count($pointSaleBillingArray) > 0): ?>
<h3>Facturation</h3>
<?php foreach($pointSaleBillingArray as $pointSale): ?>
<a href="<?= Yii::$app->urlManager->createUrl(['point-sale/update', 'id' => $pointSale->id]) ?>"><?= $pointSale->name ?></a><br />
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>

+ 9
- 1
common/logic/User/User/Module/UserModule.php Wyświetl plik

@@ -10,6 +10,7 @@ use common\logic\User\User\Service\NewsletterManager;
use common\logic\User\User\Service\UserBuilder;
use common\logic\User\User\Service\UserBulkImporter;
use common\logic\User\User\Service\UserDefinition;
use common\logic\User\User\Service\UserManager;
use common\logic\User\User\Service\UserNotifier;
use common\logic\User\User\Service\UsersCreditCsvGenerator;
use common\logic\User\User\Service\UserSolver;
@@ -24,6 +25,7 @@ use common\logic\User\User\Service\UserSolver;
* @mixin NewsletterManager
* @mixin AuthorizationChecker
* @mixin UserBulkImporter
* @mixin UserManager
*/
class UserModule extends AbstractModule
{
@@ -39,7 +41,8 @@ class UserModule extends AbstractModule
UsersCreditCsvGenerator::class,
NewsletterManager::class,
AuthorizationChecker::class,
UserBulkImporter::class
UserBulkImporter::class,
UserManager::class,
];
}

@@ -87,4 +90,9 @@ class UserModule extends AbstractModule
{
return UserBulkImporter::getInstance();
}

public function getManager(): UserManager
{
return UserManager::getInstance();
}
}

+ 8
- 0
common/logic/User/User/Service/UserBuilder.php Wyświetl plik

@@ -129,6 +129,14 @@ class UserBuilder extends AbstractBuilder
$user->password_hash = \Yii::$app->security->generatePasswordHash($password);
}

public function generatePassword(User $user): string
{
$password = Password::generate();
$this->setPassword($user, $password);
$this->update($user);
return $password;
}

/**
* Generates "remember me" authentication key
*/

+ 31
- 0
common/logic/User/User/Service/UserManager.php Wyświetl plik

@@ -0,0 +1,31 @@
<?php

namespace common\logic\User\User\Service;

use common\logic\AbstractManager;
use common\logic\User\User\Model\User;

class UserManager extends AbstractManager
{
protected UserBuilder $userBuilder;
protected UserNotifier $userNotifier;

public function loadDependencies(): void
{
$this->userBuilder = $this->loadService(UserBuilder::class);
$this->userNotifier = $this->loadService(UserNotifier::class);
}

public function welcome(User $user)
{
$password = $this->userBuilder->generatePassword($user);
$this->userNotifier->sendMailWelcome($user, $password);

}

public function newPassword(User $user)
{
$password = $this->userBuilder->generatePassword($user);
$this->userNotifier->sendMailNewPassword($user, $password);
}
}

+ 16
- 0
common/logic/User/User/Service/UserNotifier.php Wyświetl plik

@@ -64,4 +64,20 @@ class UserNotifier extends AbstractNotifier implements ManagerInterface
]
);
}

public function sendMailNewPassword(User $user, string $password)
{
$producer = $this->getProducerContext();
\Yii::$app->mailerService->sendFromProducer(
'Nouveau mot de passe',
'newPasswordUserAdmin',
[
'user' => $user,
'producer' => $producer,
'password' => $password,
],
$user->email,
$producer
);
}
}

+ 1
- 1
common/mail/createUserAdmin-html.php Wyświetl plik

@@ -45,7 +45,7 @@ use yii\helpers\Html;
<p>Votre producteur <strong><?= Html::encode($producer->name); ?></strong> vient
de créer un compte pour vous sur le site <a href="http://www.opendistrib.net/site/login">Opendistrib</a>.</p>

<p><strong>Ce compte vous permet de passer vos commandes dans cet établissement.</strong></p>
<p><strong>Ce compte vous permet de passer vos commandes chez ce producteur.</strong></p>

<p>Voici vos identifiants de connexion :<br />
Identifiant : <strong><?= Html::encode($user->email); ?></strong><br />

+ 1
- 1
common/mail/createUserAdmin-text.php Wyświetl plik

@@ -43,7 +43,7 @@ Bonjour <?= $user->name ?>,
Votre producteur "<?= $producer->name ?>" vient
de créer un compte pour vous sur le site http://www.laboiteapain.net/.

Ce compte vous permet de passer vos commandes dans cet établissement.
Ce compte vous permet de passer vos commandes chez ce producteur.

Voici vos identifiants de connexion :
Identifiant : <?= $user->email; ?>

Ładowanie…
Anuluj
Zapisz