Browse Source

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

master
Guillaume Bourgeois 1 year ago
parent
commit
866b8671d0
8 changed files with 112 additions and 59 deletions
  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 View File

* Liste les utilisateurs. * Liste les utilisateurs.
*/ */
public function actionIndex( public function actionIndex(
int $idPointSale = 0,
int $idPointSale = 0,
bool $sectionSubscribers = false, bool $sectionSubscribers = false,
bool $sectionInactiveUsers = false) bool $sectionInactiveUsers = false)
{ {
'UserSearch' => array_merge( 'UserSearch' => array_merge(
[ [
'id_point_sale' => $idPointSale, 'id_point_sale' => $idPointSale,
'inactive' => (int) $sectionInactiveUsers,
'subscribers' => (int) $sectionSubscribers
'inactive' => (int)$sectionInactiveUsers,
'subscribers' => (int)$sectionSubscribers
], ],
isset(\Yii::$app->request->queryParams['UserSearch']) ? isset(\Yii::$app->request->queryParams['UserSearch']) ?
Yii::$app->request->queryParams['UserSearch'] : Yii::$app->request->queryParams['UserSearch'] :
$model->address, $model->address,
$model->newsletter, $model->newsletter,
Password::generate(), Password::generate(),
(bool) $model->send_mail_welcome
(bool)$model->send_mail_welcome
); );
$userCreate->points_sale = $model->points_sale; $userCreate->points_sale = $model->points_sale;
$userCreate->user_groups = $model->user_groups; $userCreate->user_groups = $model->user_groups;


$model = $this->findModel($id); $model = $this->findModel($id);
$previousMail = $model->email; $previousMail = $model->email;
$user = User::find()->with('userProducer')->where(['id' => $model['id']])->one();
$userBelongToProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]); $userBelongToProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]);
if ($userBelongToProducer) { if ($userBelongToProducer) {
$model->newsletter = $userBelongToProducer->newsletter; $model->newsletter = $userBelongToProducer->newsletter;
$this->processLinkUserGroup($model); $this->processLinkUserGroup($model);
//$this->processProductPricePercent($model); //$this->processProductPricePercent($model);


if($model->newsletter) {
if ($model->newsletter) {
$userModule->getNewsletterManager()->subscribeUserNewsletter($model); $userModule->getNewsletterManager()->subscribeUserNewsletter($model);
}
else {
} else {
$userModule->getNewsletterManager()->unsubscribeUserNewsletter($model); $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']); 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), [ return $this->render('update', array_merge($this->initForm($model), [
$userProducer = $userProducerModule->findOneUserProducer($user); $userProducer = $userProducerModule->findOneUserProducer($user);


if ($userProducer) { 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."); $this->setFlash('error', "Vous ne pouvez pas supprimer cet utilisateur car il a toujours du crédit en cours.");
}
else {
} else {
$userProducerModule->unlinkUserProducer($userProducer); $userProducerModule->unlinkUserProducer($userProducer);
$this->setFlash('success', 'L\'utilisateur a bien été supprimé de votre établissement.'); $this->setFlash('success', 'L\'utilisateur a bien été supprimé de votre établissement.');
} }
$creditForm->comment $creditForm->comment
); );


if($creditForm->send_mail) {
if ($creditForm->send_mail) {
$paymentModule->getNotifier() $paymentModule->getNotifier()
->notifyUserCreditMovement( ->notifyUserCreditMovement(
$user, $user,
$creditForm->type, $creditForm->type,
$creditForm->amount $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."); $this->addFlash('error', "L'utilisateur n'a pas pu être prévenu car son adresse email n'est pas définie.");
} }
} }
$usersPointSaleHasOrder = 0) $usersPointSaleHasOrder = 0)
{ {
$userModule = $this->getUserModule(); $userModule = $this->getUserModule();
$distributionModule = $this-> getDistributionModule();
$distributionModule = $this->getDistributionModule();


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

+ 13
- 7
backend/views/user/_form.php View File

</div> </div>


<div class="col-md-4"> <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> <h3>Mot de passe</h3>
<?php $form = ActiveForm::begin(); ?> <?php $form = ActiveForm::begin(); ?>
<?= Html::submitButton('Envoyer un nouveau mot de passe', ['class' => 'btn btn-primary', 'name' => 'submit_new_password', 'value' => 1]) ?> <?= Html::submitButton('Envoyer un nouveau mot de passe', ['class' => 'btn btn-primary', 'name' => 'submit_new_password', 'value' => 1]) ?>
</div> </div>
<?php ActiveForm::end(); ?> <?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>
</div> </div>

+ 9
- 1
common/logic/User/User/Module/UserModule.php View File

use common\logic\User\User\Service\UserBuilder; use common\logic\User\User\Service\UserBuilder;
use common\logic\User\User\Service\UserBulkImporter; use common\logic\User\User\Service\UserBulkImporter;
use common\logic\User\User\Service\UserDefinition; 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\UserNotifier;
use common\logic\User\User\Service\UsersCreditCsvGenerator; use common\logic\User\User\Service\UsersCreditCsvGenerator;
use common\logic\User\User\Service\UserSolver; use common\logic\User\User\Service\UserSolver;
* @mixin NewsletterManager * @mixin NewsletterManager
* @mixin AuthorizationChecker * @mixin AuthorizationChecker
* @mixin UserBulkImporter * @mixin UserBulkImporter
* @mixin UserManager
*/ */
class UserModule extends AbstractModule class UserModule extends AbstractModule
{ {
UsersCreditCsvGenerator::class, UsersCreditCsvGenerator::class,
NewsletterManager::class, NewsletterManager::class,
AuthorizationChecker::class, AuthorizationChecker::class,
UserBulkImporter::class
UserBulkImporter::class,
UserManager::class,
]; ];
} }


{ {
return UserBulkImporter::getInstance(); return UserBulkImporter::getInstance();
} }

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

+ 8
- 0
common/logic/User/User/Service/UserBuilder.php View File

$user->password_hash = \Yii::$app->security->generatePasswordHash($password); $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 * Generates "remember me" authentication key
*/ */

+ 31
- 0
common/logic/User/User/Service/UserManager.php View File

<?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 View File

] ]
); );
} }

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 View File

<p>Votre producteur <strong><?= Html::encode($producer->name); ?></strong> vient <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> 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 /> <p>Voici vos identifiants de connexion :<br />
Identifiant : <strong><?= Html::encode($user->email); ?></strong><br /> Identifiant : <strong><?= Html::encode($user->email); ?></strong><br />

+ 1
- 1
common/mail/createUserAdmin-text.php View File

Votre producteur "<?= $producer->name ?>" vient Votre producteur "<?= $producer->name ?>" vient
de créer un compte pour vous sur le site http://www.laboiteapain.net/. 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 : Voici vos identifiants de connexion :
Identifiant : <?= $user->email; ?> Identifiant : <?= $user->email; ?>

Loading…
Cancel
Save