You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 line
5.7KB

  1. <?php
  2. namespace domain\User\User;
  3. use common\helpers\Password;
  4. use domain\Producer\Producer\Producer;
  5. use domain\User\User\Event\UserCreateEvent;
  6. use domain\User\UserGroup\UserGroup;
  7. use domain\User\UserProducer\UserProducerBuilder;
  8. use domain\User\UserUserGroup\UserUserGroupBuilder;
  9. use domain\_\AbstractBuilder;
  10. class UserBuilder extends AbstractBuilder
  11. {
  12. protected UserRepository $userRepository;
  13. protected UserNotifier $userNotifier;
  14. protected UserProducerBuilder $userProducerBuilder;
  15. protected UserUserGroupBuilder $userUserGroupBuilder;
  16. public function loadDependencies(): void
  17. {
  18. $this->userRepository = $this->loadService(UserRepository::class);
  19. $this->userNotifier = $this->loadService(UserNotifier::class);
  20. $this->userProducerBuilder = $this->loadService(UserProducerBuilder::class);
  21. $this->userUserGroupBuilder = $this->loadService(UserUserGroupBuilder::class);
  22. }
  23. public function instanciateUser(
  24. string $type = null,
  25. string $email = null,
  26. string $firstname = null,
  27. string $lastname = null,
  28. string $nameLegalPerson = null,
  29. string $phone = null,
  30. string $address = null,
  31. string $password = null
  32. ): User
  33. {
  34. $user = new User();
  35. $user->id_producer = 0;
  36. $user->type = $type;
  37. $this->initUsername($user, $email);
  38. $user->email = $email;
  39. $user->name = $firstname;
  40. $user->lastname = $lastname;
  41. $user->name_legal_person = $nameLegalPerson;
  42. $user->phone = $phone;
  43. $user->address = $address;
  44. $this->initPassword($user, $password);
  45. return $user;
  46. }
  47. public function createUser(
  48. string $type = null,
  49. string $email = null,
  50. string $firstname = null,
  51. string $lastname = null,
  52. string $nameLegalPerson = null,
  53. string $phone = null,
  54. string $address = null,
  55. bool $newsletter = true,
  56. string $password = null,
  57. bool $sendMailWelcome = true
  58. ): User
  59. {
  60. $userExist = null;
  61. if($email && strlen($email)) {
  62. $userExist = $this->userRepository->findOneUserByEmail($email);
  63. $user = $userExist;
  64. }
  65. if(!$userExist) {
  66. $user = $this->instanciateUser(
  67. $type,
  68. $email,
  69. $firstname,
  70. $lastname,
  71. $nameLegalPerson,
  72. $phone,
  73. $address,
  74. $password
  75. );
  76. $this->create($user);
  77. if($sendMailWelcome) {
  78. $this->userNotifier->sendMailWelcome($user, $password);
  79. }
  80. }
  81. $user->triggerEvent(
  82. UserCreateEvent::class,
  83. [
  84. 'user' => $user,
  85. 'producer' => $this->getProducerContext(),
  86. 'newsletter' => $newsletter
  87. ]
  88. );
  89. return $user;
  90. }
  91. public function initUsername(User $user, string $email = null)
  92. {
  93. $user->username = $email;
  94. if (!$email) {
  95. $user->username = 'inconnu@souke.fr';
  96. }
  97. }
  98. public function initPassword(User $user, string $password = null)
  99. {
  100. if(!$password) {
  101. $password = Password::generate();
  102. }
  103. $this->setPassword($user, $password);
  104. $this->generateAuthKey($user);
  105. }
  106. /**
  107. * Generates password hash from password and sets it to the model
  108. */
  109. public function setPassword(User $user, string $password): void
  110. {
  111. $user->password_hash = \Yii::$app->security->generatePasswordHash($password);
  112. }
  113. public function generatePassword(User $user): string
  114. {
  115. $password = Password::generate();
  116. $this->setPassword($user, $password);
  117. $this->update($user);
  118. return $password;
  119. }
  120. /**
  121. * Generates "remember me" authentication key
  122. */
  123. public function generateAuthKey(User $user): void
  124. {
  125. $user->auth_key = \Yii::$app->security->generateRandomString();
  126. }
  127. public function initProducer(User $user, Producer $producer)
  128. {
  129. $user->id_producer = $producer->id;
  130. $user->status = User::STATUS_PRODUCER;
  131. }
  132. /**
  133. * Met à jour la date de dernière connexion de l'utilisateur.
  134. */
  135. public function updateUserLastConnection(User $user)
  136. {
  137. $user->date_last_connection = date('Y-m-d H:i:s');
  138. $user->save();
  139. }
  140. /**
  141. * Generates new password reset token
  142. */
  143. public function generatePasswordResetToken(User $user): void
  144. {
  145. $user->password_reset_token = \Yii::$app->security->generateRandomString() . '_' . time();
  146. }
  147. /**
  148. * Removes password reset token
  149. */
  150. public function removePasswordResetToken(User $user)
  151. {
  152. $user->password_reset_token = null;
  153. }
  154. public function grantAccess(User $user): bool
  155. {
  156. if($this->userRepository->isActive($user)) {
  157. $user->id_producer = $this->getProducerContextId();
  158. $user->status = User::STATUS_PRODUCER;
  159. return $user->save();
  160. }
  161. return false;
  162. }
  163. public function deleteAccess(User $user): bool
  164. {
  165. if($this->userRepository->isActive($user)) {
  166. $user->id_producer = null;
  167. $user->status = User::STATUS_ACTIVE;
  168. return $user->save();
  169. }
  170. return false;
  171. }
  172. public function switchProducer(User $user, Producer $producer)
  173. {
  174. $user->id_producer = $producer->id;
  175. $this->update($user);
  176. }
  177. public function addUserGroup(User $user, UserGroup $userGroup)
  178. {
  179. $this->userUserGroupBuilder->createUserUserGroup($user, $userGroup);
  180. }
  181. }