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.

UserController.php 25KB

8 years ago
3 months ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. <?php
  2. /**
  3. * Copyright Guillaume Bourgeois (2018)
  4. *
  5. * contact@souke.fr
  6. *
  7. * Ce logiciel est un programme informatique servant à aider les producteurs
  8. * à distribuer leur production en circuits courts.
  9. *
  10. * Ce logiciel est régi par la licence CeCILL soumise au droit français et
  11. * respectant les principes de diffusion des logiciels libres. Vous pouvez
  12. * utiliser, modifier et/ou redistribuer ce programme sous les conditions
  13. * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  14. * sur le site "http://www.cecill.info".
  15. *
  16. * En contrepartie de l'accessibilité au code source et des droits de copie,
  17. * de modification et de redistribution accordés par cette licence, il n'est
  18. * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  19. * seule une responsabilité restreinte pèse sur l'auteur du programme, le
  20. * titulaire des droits patrimoniaux et les concédants successifs.
  21. *
  22. * A cet égard l'attention de l'utilisateur est attirée sur les risques
  23. * associés au chargement, à l'utilisation, à la modification et/ou au
  24. * développement et à la reproduction du logiciel par l'utilisateur étant
  25. * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  26. * manipuler et qui le réserve donc à des développeurs et des professionnels
  27. * avertis possédant des connaissances informatiques approfondies. Les
  28. * utilisateurs sont donc invités à charger et tester l'adéquation du
  29. * logiciel à leurs besoins dans des conditions permettant d'assurer la
  30. * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  31. * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  32. *
  33. * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  34. * pris connaissance de la licence CeCILL, et que vous en avez accepté les
  35. * termes.
  36. */
  37. namespace backend\controllers;
  38. use backend\models\CreditForm;
  39. use common\helpers\GlobalParam;
  40. use common\helpers\Password;
  41. use domain\Order\Order\OrderSearch;
  42. use domain\PointSale\PointSale\PointSale;
  43. use domain\PointSale\UserPointSale\UserPointSale;
  44. use domain\User\User\User;
  45. use domain\User\User\UserSearch;
  46. use domain\User\UserProducer\UserProducer;
  47. use domain\User\UserUserGroup\UserUserGroup;
  48. use Yii;
  49. use yii\base\UserException;
  50. use yii\filters\AccessControl;
  51. use yii\helpers\Html;
  52. use yii\web\NotFoundHttpException;
  53. /**
  54. * UserController implements the CRUD actions for User model.
  55. */
  56. class UserController extends BackendController
  57. {
  58. public function behaviors()
  59. {
  60. return [
  61. 'access' => [
  62. 'class' => AccessControl::class,
  63. 'rules' => [
  64. [
  65. 'allow' => true,
  66. 'roles' => ['@'],
  67. 'matchCallback' => function ($rule, $action) {
  68. $userCurrent = $this->getUserCurrent();
  69. $authorizationChecker = $this->getUserModule()->getAuthorizationChecker();
  70. if(in_array($action->id, ['messages', 'message-delete'])) {
  71. return $authorizationChecker->isGrantedAsAdministrator($userCurrent);
  72. }
  73. else {
  74. return $authorizationChecker->isGrantedAsProducer($userCurrent);
  75. }
  76. }
  77. ],
  78. ],
  79. ],
  80. ];
  81. }
  82. /**
  83. * Liste les utilisateurs.
  84. */
  85. public function actionIndex(
  86. int $idPointSale = 0,
  87. bool $sectionSubscribers = false,
  88. bool $sectionInactiveUsers = false)
  89. {
  90. $pointSaleModule = $this->getPointSaleModule();
  91. $producer = $this->getProducerCurrent();
  92. $searchModel = new UserSearch();
  93. $dataProvider = $searchModel->search([
  94. 'UserSearch' => array_merge(
  95. [
  96. 'id_point_sale' => $idPointSale,
  97. 'inactive' => (int)$sectionInactiveUsers,
  98. 'subscribers' => (int)$sectionSubscribers
  99. ],
  100. isset(\Yii::$app->request->queryParams['UserSearch']) ?
  101. Yii::$app->request->queryParams['UserSearch'] :
  102. []
  103. )
  104. ], $producer);
  105. $producer = $this->getProducerCurrent();
  106. $pointsSaleArray = $pointSaleModule->findPointSales();
  107. return $this->render('index', [
  108. 'searchModel' => $searchModel,
  109. 'dataProvider' => $dataProvider,
  110. 'producer' => $producer,
  111. 'idPointSaleActive' => $idPointSale,
  112. 'pointsSaleArray' => $pointsSaleArray,
  113. 'sectionInactiveUsers' => $sectionInactiveUsers,
  114. 'sectionSubscribers' => $sectionSubscribers,
  115. ]);
  116. }
  117. public function actionCreate(string $redirectUrl = null)
  118. {
  119. $userModule = $this->getUserModule();
  120. $producerModule = $this->getProducerModule();
  121. $producerCurrent = $this->getProducerCurrent();
  122. $model = $userModule->instanciateUser();
  123. $model->redirect_url = $redirectUrl;
  124. $posts = Yii::$app->request->post();
  125. $userExist = false;
  126. if ($posts && isset($posts['User']['email']) && strlen($posts['User']['email']) > 0) {
  127. $userExist = $userModule->findOneUserByEmail($posts['User']['email']);
  128. }
  129. if ($userExist) {
  130. $producerModule->addUser($userExist, $producerCurrent);
  131. $this->processLinkPointSale($userExist);
  132. $this->processLinkUserGroup($userExist);
  133. $this->setFlash('success', "L'utilisateur que vous souhaitez créer possède déjà un compte sur la plateforme. Il vient d'être lié à votre établissement.");
  134. return $this->redirectAfterSave('user', $userExist->id);
  135. } else {
  136. if ($model->load(\Yii::$app->request->post()) && $model->validate() && YII_ENV != 'demo') {
  137. $userCreate = $userModule->getBuilder()->createUser(
  138. $model->type,
  139. $model->email,
  140. $model->name,
  141. $model->lastname,
  142. $model->name_legal_person,
  143. $model->phone,
  144. $model->address,
  145. $model->newsletter,
  146. Password::generate(),
  147. (bool)$model->send_mail_welcome
  148. );
  149. $userCreate->points_sale = $model->points_sale;
  150. $userCreate->user_groups = $model->user_groups;
  151. $userCreate->exclusive_access_selected_points_sale = $model->exclusive_access_selected_points_sale;
  152. $this->processLinkPointSale($userCreate);
  153. $this->processLinkUserGroup($userCreate);
  154. //$this->processProductPricePercent($model);
  155. $this->setFlash('success', 'Utilisateur créé.');
  156. return $this->redirectAfterSave('user', $userCreate->id, $model->redirect_url);
  157. }
  158. }
  159. return $this->render('create', array_merge($this->initForm($model), [
  160. 'model' => $model,
  161. ]));
  162. }
  163. public function actionView($id)
  164. {
  165. $userModule = $this->getUserModule();
  166. $pointSaleModule = $this->getPointSaleModule();
  167. $model = $this->findModel($id);
  168. // Email de bienvenue
  169. $mailWelcome = Yii::$app->request->post('submit_mail_welcome');
  170. if ($mailWelcome) {
  171. $this->getUserModule()->getManager()->welcome($model);
  172. $this->setFlash('success', 'Email de bienvenue envoyé à <strong>' . Html::encode($userModule->getSolver()->getUsername($model)) . '</strong>.');
  173. return $this->redirect(['view', 'id' => $model->id]);
  174. }
  175. // Mot de passe oublié
  176. $newPassword = Yii::$app->request->post('submit_new_password');
  177. if ($newPassword) {
  178. $this->getUserModule()->getManager()->newPassword($model);
  179. $this->setFlash('success', 'Nouveau mot de passe envoyé à <strong>' . Html::encode($userModule->getSolver()->getUsername($model)) . '</strong>.');
  180. return $this->redirect(['view', 'id' => $model->id]);
  181. }
  182. return $this->render('view', [
  183. 'model' => $model,
  184. 'pointSaleBillingArray' => $pointSaleModule->findByBillingUser($model),
  185. 'subscriptionsArray' => $this->getSubscriptionModule()->getRepository()->findSubscriptionsByUser($model)
  186. ]);
  187. }
  188. public function actionUpdate($id)
  189. {
  190. if($this->getProducerCurrent()->isDemoAccount()) {
  191. $this->addFlash('error', "Fonctionnalité non disponible sur le compte de démo.");
  192. return $this->redirect(['view', 'id' => $id]);
  193. }
  194. $userModule = $this->getUserModule();
  195. $pointSaleModule = $this->getPointSaleModule();
  196. $model = $this->findModel($id);
  197. $previousMail = $model->email;
  198. $userBelongToProducer = UserProducer::findOne(['id_user' => $id, 'id_producer' => GlobalParam::getCurrentProducerId()]);
  199. if ($userBelongToProducer) {
  200. $model->newsletter = $userBelongToProducer->newsletter;
  201. $model->newsletter_order_taking = $userBelongToProducer->newsletter_order_taking;
  202. $model->trust_alert = $userBelongToProducer->trust_alert;
  203. $model->trust_alert_comment = $userBelongToProducer->trust_alert_comment;
  204. $model->exclusive_access_selected_points_sale = $userBelongToProducer->exclusive_access_selected_points_sale;
  205. if ($model->load(\Yii::$app->request->post()) && $model->save()) {
  206. // on envoie le mail de bienvenue si le mail vient d'être défini
  207. if (!strlen($previousMail) && strlen($model->email) && $model->send_mail_welcome) {
  208. $password = Password::generate();
  209. $userModule->setPassword($model, $password);
  210. $model->username = $model->email;
  211. $userModule->sendMailWelcome($model, $password);
  212. }
  213. $this->processLinkPointSale($model);
  214. $this->processLinkUserGroup($model);
  215. //$this->processProductPricePercent($model);
  216. // Newsletter
  217. $userModule->getNewsletterManager()->manageUserNewsletter(
  218. $model,
  219. $model->newsletter,
  220. $model->newsletter_order_taking
  221. );
  222. // Alerte confiance
  223. $userBelongToProducer->trust_alert = $model->trust_alert;
  224. $userBelongToProducer->trust_alert_comment = $model->trust_alert_comment;
  225. $userBelongToProducer->save();
  226. // Mot de passe
  227. if($this->getUserModule()->getAuthorizationChecker()->isGrantedAsAdministrator($this->getUserCurrent())
  228. && $model->password_new
  229. && strlen($model->password_new)) {
  230. $this->getUserModule()->getBuilder()->initPassword($model, $model->password_new);
  231. $model->save();
  232. }
  233. $this->setFlash('success', 'Utilisateur <strong>' . Html::encode($userModule->getUsername($model)) . '</strong> modifié.');
  234. return $this->redirect(['update', 'id' => $model->id]);
  235. }
  236. } else {
  237. throw new UserException("Vous ne pouvez pas modifier cet utilisateur.");
  238. }
  239. return $this->render('update', array_merge($this->initForm($model), [
  240. 'model' => $model,
  241. 'pointSaleBillingArray' => $pointSaleModule->findByBillingUser($model)
  242. ]));
  243. }
  244. public function actionMessages(int $id, int $idUserMessage = null)
  245. {
  246. $userMessageModule = $this->getUserMessageModule();
  247. $user = $this->findModel($id);
  248. $isUpdate = false;
  249. if($idUserMessage) {
  250. $isUpdate = true;
  251. $userMessageModel = $this->findUserMessage($idUserMessage);
  252. }
  253. else {
  254. $userMessageModel = $userMessageModule->getBuilder()->instanciateUserMessage(
  255. $user,
  256. $this->getUserCurrent()
  257. );
  258. }
  259. if($userMessageModel->load(\Yii::$app->request->post()) && $userMessageModel->validate()) {
  260. if($isUpdate) {
  261. $userMessageModel->save();
  262. $this->setFlash('success', "Le message a bien été modifié.");
  263. }
  264. else {
  265. $userMessageModule->getManager()->createUserMessage(
  266. $user,
  267. $userMessageModel->getMessage(),
  268. $this->getUserCurrent()
  269. );
  270. $this->setFlash('success', "Le message a bien été envoyé à l'utilisateur.");
  271. }
  272. return $this->redirect(['messages', 'id' => $id]);
  273. }
  274. return $this->render('messages', [
  275. 'user' => $user,
  276. 'userMessageModel' => $userMessageModel,
  277. 'userMessagesDataProvider' => $userMessageModule->getRepository()
  278. ->queryUserMessagesByUser($user)
  279. ->getDataProvider(20)
  280. ]);
  281. }
  282. public function actionMessageDelete(int $idUser, int $idUserMessage)
  283. {
  284. $userMessageModule = $this->getUserMessageModule();
  285. $userMessage = $this->findUserMessage($idUserMessage);
  286. if($userMessageModule->getManager()->deleteUserMessage($userMessage)) {
  287. $this->setFlash('success', "Le message a bien été supprimé.");
  288. }
  289. else {
  290. $this->setFlash('error', "Une erreur est survenue pendant la suppression du message.");
  291. }
  292. return $this->redirect(['messages', 'id' => $idUser]);
  293. }
  294. public function findUserMessage(int $idUserMessage)
  295. {
  296. $userMessage = $this->getUserMessageModule()->getRepository()->findOneUserMessageById($idUserMessage);
  297. if(!$userMessage) {
  298. throw new NotFoundHttpException("Le message utilisateur n'a pas été trouvé.");
  299. }
  300. return $userMessage;
  301. }
  302. public function actionReportProblemReceivingEmails(int $id)
  303. {
  304. $user = $this->findModel($id);
  305. if($this->getUserModule()->getManager()->reportProblemReceivingEmails($user)) {
  306. $this->setFlash('success', "L'utilisateur <strong>".$this->getUserModule()->getSolver()->getUsername($user)."</strong> a bien été signalé comme ayant des problèmes dans la réception de vos emails. L'administrateur débloquera la situation dès que possible et préviendra l'utilisateur.");
  307. }
  308. else {
  309. $this->setFlash('error', "Une erreur est survenue.");
  310. }
  311. return $this->redirect('index');
  312. }
  313. public function initForm($model)
  314. {
  315. $userPointSaleModule = $this->getUserPointSaleModule();
  316. $userUserGroupModule = $this->getUserUserGroupModule();
  317. $userProducerModule = $this->getUserProducerModule();
  318. $userGroupModule = $this->getUserGroupModule();
  319. if ($model->id) {
  320. $userPointSaleArray = $userPointSaleModule->findUserPointSalesByUser($model);
  321. if ($userPointSaleArray && count($userPointSaleArray) > 0) {
  322. foreach ($userPointSaleArray as $userPointSale) {
  323. $model->points_sale[] = $userPointSale->id_point_sale;
  324. }
  325. }
  326. $userUserGroupsArray = $userUserGroupModule->findUserUserGroupsByUser($model);
  327. if ($userUserGroupsArray && count($userUserGroupsArray) > 0) {
  328. foreach ($userUserGroupsArray as $userUserGroup) {
  329. $model->user_groups[] = $userUserGroup->id_user_group;
  330. }
  331. }
  332. $userProducer = $userProducerModule->findOneUserProducer($model);
  333. $model->product_price_percent = $userProducer->product_price_percent;
  334. }
  335. $pointsSaleArray = PointSale::find()
  336. ->where([
  337. 'id_producer' => GlobalParam::getCurrentProducerId(),
  338. 'status' => 1
  339. ])
  340. ->joinWith(['userPointSale' => function ($query) use ($model) {
  341. if ($model->id) {
  342. $query->andOnCondition('user_point_sale.id_user = ' . $model->id);
  343. }
  344. }])
  345. ->all();
  346. $userGroupsArray = $userGroupModule->findUserGroups();
  347. return [
  348. 'pointsSaleArray' => $pointsSaleArray,
  349. 'userGroupsArray' => $userGroupsArray,
  350. ];
  351. }
  352. /**
  353. * Lie un utilisateur aux points de vente sélectionnés.
  354. */
  355. public function processLinkPointSale(User $modelUser)
  356. {
  357. $posts = Yii::$app->request->post();
  358. $userPointSaleModule = $this->getUserPointSaleModule();
  359. $pointSaleModule = $this->getPointSaleModule();
  360. UserPointSale::deleteAll([
  361. 'id_user' => $modelUser->id
  362. ]);
  363. if (is_array($modelUser->points_sale) && count($modelUser->points_sale) > 0) {
  364. foreach ($modelUser->points_sale as $pointSaleId) {
  365. $pointSale = $pointSaleModule->findOnePointSaleById($pointSaleId);
  366. $userPointSale = $userPointSaleModule->findOneUserPointSale($modelUser, $pointSale);
  367. if (!$userPointSale) {
  368. $userPointSale = new UserPointSale;
  369. $userPointSale->id_user = $modelUser->id;
  370. $userPointSale->id_point_sale = $pointSaleId;
  371. $userPointSale->comment = isset($posts['User']['comment_point_sale_' . $pointSaleId]) ? $posts['User']['comment_point_sale_' . $pointSaleId] : '';
  372. $userPointSale->save();
  373. }
  374. }
  375. }
  376. // Accès exclusif aux points de vente sélectionnés
  377. $userProducer = UserProducer::findOne(['id_user' => $modelUser->id, 'id_producer' => GlobalParam::getCurrentProducerId()]);
  378. $userProducer->setExclusiveAccessSelectedPointsSale($modelUser->exclusive_access_selected_points_sale);
  379. $userProducer->save();
  380. }
  381. /**
  382. * Lie un utilisateur aux groupes d'utilisateurs sélectionnés.
  383. */
  384. public function processLinkUserGroup($modelUser)
  385. {
  386. UserUserGroup::deleteAll([
  387. 'id_user' => $modelUser->id
  388. ]);
  389. if (is_array($modelUser->user_groups) && count($modelUser->user_groups) > 0) {
  390. foreach ($modelUser->user_groups as $userGroupId) {
  391. $userUserGroup = UserUserGroup::searchOne([
  392. 'id_user' => $modelUser->id,
  393. 'id_user_group' => $userGroupId
  394. ]);
  395. if (!$userUserGroup) {
  396. $userUserGroup = new UserUserGroup();
  397. $userUserGroup->id_user = $modelUser->id;
  398. $userUserGroup->id_user_group = $userGroupId;
  399. $userUserGroup->save();
  400. }
  401. }
  402. }
  403. }
  404. public function processProductPricePercent($model)
  405. {
  406. $userProducer = UserProducer::searchOne([
  407. 'id_producer' => GlobalParam::getCurrentProducerId(),
  408. 'id_user' => $model->id
  409. ]);
  410. $userProducer->product_price_percent = $model->product_price_percent;
  411. $userProducer->save();
  412. }
  413. /**
  414. * Désactive l'utilisateur de l'établissement.
  415. */
  416. public function actionDelete(int $id)
  417. {
  418. $userModule = $this->getUserModule();
  419. $userProducerModule = $this->getUserProducerModule();
  420. $user = $userModule->findOneUserById($id);
  421. $userProducer = $userProducerModule->findOneUserProducer($user);
  422. if ($userProducer) {
  423. if ($userProducerModule->hasOutstandingCredit($userProducer)) {
  424. $this->setFlash('error', "Vous ne pouvez pas supprimer cet utilisateur car il a toujours de l'argent dans sa cagnotte.");
  425. } else {
  426. $userProducerModule->unlinkUserProducer($userProducer);
  427. $this->setFlash('success', 'L\'utilisateur a bien été supprimé de votre établissement.');
  428. }
  429. } else {
  430. throw new \yii\web\NotFoundHttpException('L\'enregistrement UserProducer est introuvable', 404);
  431. }
  432. $params = Yii::$app->getRequest()->getQueryParams();
  433. unset($params['id']);
  434. $this->redirect(array_merge(['index'], $params));
  435. }
  436. /**
  437. * Affiche les données liées à la cagnotte d'un utilisateur (formulaire, historique).
  438. */
  439. public function actionCredit(int $id)
  440. {
  441. $producerModule = $this->getProducerModule();
  442. $userModule = $this->getUserModule();
  443. $paymentModule = $this->getPaymentModule();
  444. $userProducerModule = $this->getUserProducerModule();
  445. $user = $userModule->findOneUserById($id);
  446. $userProducer = $userProducerModule->getRepository()->findOneUserProducer($user);
  447. if ($userProducer) {
  448. $creditForm = new CreditForm();
  449. $creditForm->send_mail = $producerModule->getSolver()->getConfig('option_check_by_default_prevent_user_credit');
  450. if ($creditForm->load(\Yii::$app->request->post()) && $creditForm->validate()) {
  451. $dateTransaction = null;
  452. if($creditForm->date_transaction) {
  453. $dateTransaction = date('Y-m-d', strtotime(str_replace('/', '-', $creditForm->date_transaction)));
  454. }
  455. $paymentModule->getManager()
  456. ->creditOrDebitUser(
  457. $creditForm->type,
  458. $user,
  459. $creditForm->amount,
  460. $creditForm->mean_payment,
  461. $this->getUserCurrent(),
  462. $creditForm->comment,
  463. $dateTransaction
  464. );
  465. if ($creditForm->send_mail && !$this->getProducerCurrent()->isDemoAccount()) {
  466. $paymentModule->getNotifier()
  467. ->notifyUserCreditMovement(
  468. $user,
  469. $creditForm->type,
  470. $creditForm->amount
  471. );
  472. if (!$user->email) {
  473. $this->addFlash('error', "L'utilisateur n'a pas pu être prévenu car son adresse email n'est pas définie.");
  474. }
  475. }
  476. $this->addFlash('success', 'Cagnotte mise à jour.');
  477. return $this->refresh();
  478. }
  479. return $this->render('credit', [
  480. 'user' => $user,
  481. 'userProducer' => $userProducer,
  482. 'creditForm' => $creditForm,
  483. 'dataProvider' => $paymentModule->getRepository()
  484. ->queryPaymentsCreditHistoryByUser($user)->getDataProvider(20),
  485. ]);
  486. } else {
  487. throw new UserException("Utilisateur introuvable.");
  488. }
  489. }
  490. /**
  491. * Modifie l'option "credit_active" d'un utilisateur pour le producteur courant.
  492. * Redirige vers la page de crédit de l'utilisateur.
  493. */
  494. public function actionStateCredit($idUser, $state)
  495. {
  496. $userModule = $this->getUserModule();
  497. $userProducerModule = $this->getUserProducerModule();
  498. $user = $userModule->findOneUserById($idUser);
  499. $userProducer = $userProducerModule->findOneUserProducer($user);
  500. if ($userProducer) {
  501. $userProducer->credit_active = $state;
  502. $userProducer->save();
  503. }
  504. return $this->redirect(['user/credit', 'id' => $idUser]);
  505. }
  506. /**
  507. * Affiche les commandes d'un utilisateur.
  508. *
  509. * @param integer $id
  510. * @return mixed
  511. */
  512. public function actionOrders($id)
  513. {
  514. $userModule = $this->getUserModule();
  515. $user = $userModule->findOneUserById($id);
  516. $searchModel = new OrderSearch();
  517. $dataProvider = $searchModel->search(array_merge(\Yii::$app->request->queryParams, ['id_user' => $id]));
  518. return $this->render('orders', [
  519. 'user' => $user,
  520. 'searchModel' => $searchModel,
  521. 'dataProvider' => $dataProvider,
  522. ]);
  523. }
  524. /**
  525. * Finds the User model based on its primary key value.
  526. * If the model is not found, a 404 HTTP exception will be thrown.
  527. */
  528. protected function findModel($id)
  529. {
  530. $userModule = $this->getUserModule();
  531. if (($user = $userModule->findOneUserById($id)) !== null) {
  532. return $user;
  533. } else {
  534. throw new NotFoundHttpException("Utilisateur introuvable");
  535. }
  536. }
  537. public function actionSwitchIdentity(int $id)
  538. {
  539. $userModule = $this->getUserModule();
  540. if ($this->isUserCurrentAdmin()) {
  541. $initialId = Yii::$app->user->getId();
  542. if ($initialId != $id) {
  543. $user = $userModule->findOneUserById($id);
  544. $duration = 0;
  545. Yii::$app->user->switchIdentity($user, $duration);
  546. Yii::$app->session->set('user.idbeforeswitch', $initialId);
  547. return $this->redirect($this->getUrlManagerFrontend()->createUrl(['site/index']));
  548. }
  549. } else {
  550. throw new NotFoundHttpException('Page introuvable');
  551. }
  552. }
  553. }