Advertisement
Guest User

Ale 28/01

a guest
Jan 29th, 2020
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.88 KB | None | 0 0
  1. #############################################################################################
  2. file /frontend/config/main  -  UPDATE
  3. #############################################################################################
  4.  
  5. riga 101, alla fine dell'array rules:
  6.  
  7. 'invia-richiesta-recover-password' => 'site/sendrichiestarecoverpassword',
  8. 'rp' => 'site/resetpassword',
  9. 'saverp' => 'site/saveresetpassword'
  10.  
  11.  
  12.  
  13.  
  14. #############################################################################################
  15. file /frontend/controllers/SiteController.php  -  UPDATE
  16. #############################################################################################
  17.  
  18.     /**
  19.      * Logs in a user.
  20.      *
  21.      * @return mixed
  22.      */
  23.     public function actionLogin()
  24.     {
  25.         $msg_reset_password = Yii::$app->session->get("msg_reset_password",NULL);
  26.  
  27.         if (!Yii::$app->user->isGuest) {
  28.             return $this->goHome();
  29.         }
  30.  
  31.         $model = new LoginFormFE();
  32.         if ($model->load(Yii::$app->request->post()) && $model->login()) {
  33.             return $this->redirect("/account");
  34.         } else {
  35.             return $this->render('login', [
  36.                 'model' => $model,
  37.                 'msg_reset_password' => $msg_reset_password
  38.             ]);
  39.         }
  40.     }
  41.    
  42.     /**
  43.      * Logs out the current user.
  44.      *
  45.      * @return mixed
  46.      */
  47.     public function actionSendrichiestarecoverpassword()
  48.     {
  49.         $params = Yii::$app->request;
  50.  
  51.         $codfisc = trim(Html::encode($params->post("codice-fiscale")));
  52.         $email = trim(Html::encode($params->post("email")));
  53.  
  54.         if(empty($codfisc))
  55.             return BaseJson::encode([ 'error' => 1,'field' => 'codice-fiscale','msg' => 'Il codice fiscale è obbligatorio' ]);
  56.  
  57.         if(is_numeric($codfisc) && strlen($codfisc) != 11)
  58.             return BaseJson::encode([ 'error' => 1,'field' => 'codice-fiscale','msg' => 'Il codice fiscale non è valido' ]);
  59.         elseif(! is_numeric($codfisc) && ! StringUtils::is_valid_cf($codfisc))
  60.             return BaseJson::encode([ 'error' => 1,'field' => 'codice-fiscale','msg' => 'Il codice fiscale non è valido' ]);
  61.  
  62.         if(empty($email))
  63.             return BaseJson::encode([ 'error' => 1,'field' => 'email','msg' => 'L\'email è obbligatoria' ]);
  64.  
  65.         if(!StringUtils::is_valid_email($email))
  66.             return BaseJson::encode([ 'error' => 1,'field' => 'email','msg' => 'Email non valida' ]);
  67.  
  68.         $q = <<<SQL
  69. SELECT C.idCliente,A.id_Account ida,C.ClienteCodiceContabilita codcli FROM Cliente C
  70. JOIN _Account A ON A.id_Account=C._Account_id_Account
  71. JOIN _non_cancellare NC ON A._AccountUsername=NC.user
  72. WHERE (C.ClienteCodFiscale=:cf OR C.ClientePIVA=:cf) AND A._AccountEmail=:em AND NC.mail=:em
  73. SQL;
  74.  
  75.         $connection = Yii::$app->getDb();
  76.         $command = $connection->createCommand($q);
  77.         $command->bindValue(':cf', $codfisc);
  78.         $command->bindValue(':em', $email);
  79.         $cliente = $command->queryOne(\PDO::FETCH_ASSOC);
  80.  
  81.         if(empty($cliente)){
  82.             return BaseJson::encode([ 'error' => 1,'field' => '','msg' => 'Siamo spiacenti. Cliente non trovato sul nostro sistema' ]);
  83.         }
  84.  
  85.         $idacc = $cliente['_Account_id_Account'];
  86.         if(! empty($idacc))
  87.             return BaseJson::encode([ 'error' => 1,'field' => '','msg' => 'Possiede già un accesso per la sua Area Personale' ]);
  88.  
  89.         $data = [
  90.             'ida' => $cliente['ida'],
  91.             'codcli' => $cliente['codcli'],
  92.             'codfisc' => $codfisc,
  93.             'email' => $email
  94.         ];
  95.  
  96.         $sub = "NordOvest Energie - Richiesta di recupero password di accesso all'Area Personale";
  97.         $from = [ Yii::$app->params['sistemaautoEmail'] => 'Nordovest Energie' ];
  98.         $to = [ Yii::$app->params['clientiEmail'] => 'Nordovest Energie Area clienti' ];
  99.         //$from = $to = 'simmese@simmese.com';
  100.  
  101.         Yii::$app->mailer->compose(
  102.             [ 'html' => 'send-richiesta-rp-html','text' => 'send-richiesta-rp-text' ],
  103.             $data
  104.         )->setFrom($from)->setTo($to)->setSubject($sub)->setBcc($from)->send();
  105.  
  106.         $msg = <<<HTML
  107. <h3>Grazie per averci contattati</h3>
  108. <div class="standard-no-indent">
  109.     La sua richiesta è stata processata correttamente.<br />
  110.     A breve riceverà una nostra email contenente le istruzioni per reimpostare la password di accesso alla sua Area Personale.
  111. </div>
  112. <div class="standard-no-indent">
  113.     Nel caso in cui non dovesse ricevere nulla verifichi la sua casella Spam.<br />
  114.     Per evitare che eventuali comunicazioni vengano classificate come Spam le consigliamo di aggiungere la nostra email tra i suoi contatti.
  115. </div>
  116. HTML;
  117.  
  118.         return BaseJson::encode([ 'success' => 1,'msg' => $msg ]);
  119.     }
  120.  
  121.     public function actionResetpassword(){
  122.         $params = Yii::$app->request;
  123.  
  124.         $token = trim($params->get('t'));
  125.  
  126.         $q = <<<SQL
  127. SELECT * FROM _Account WHERE token=:t AND token_exp>NOW()
  128. SQL;
  129.  
  130.         $connection = Yii::$app->getDb();
  131.         $command = $connection->createCommand($q);
  132.         $command->bindValue(':t', $token);
  133.         $account = $command->queryOne(\PDO::FETCH_ASSOC);
  134.  
  135.         return $this->render('resetpassword', [
  136.             'account' => $account,
  137.             'token' => $token
  138.         ]);
  139.     }
  140.  
  141.     public function actionSaveresetpassword(){
  142.         $params = Yii::$app->request;
  143.  
  144.         $token = intval($params->post("token"));
  145.         $ida = intval($params->post("ida"));
  146.         $np = trim(Html::encode($params->post("np")));
  147.         $npc = trim(Html::encode($params->post("npc")));
  148.  
  149.         $err = 0;
  150.         if(empty($np)){
  151.             $err = 101;
  152.         }
  153.  
  154.         if(empty($npc)){
  155.             $err = 102;
  156.         }
  157.  
  158.         if($np != $npc) {
  159.             $err = 103;
  160.         }
  161.  
  162.         if(! empty($err)) {
  163.             return $this->redirect("/rp?t=".$token."&err=".$err);
  164.         }
  165.  
  166.         $q = <<<SQL
  167. UPDATE _Account SET _AccountPassword=:p WHERE id_Account=:ida
  168. SQL;
  169.  
  170.         $connection = Yii::$app->getDb();
  171.         $command = $connection->createCommand($q);
  172.         $command->bindValue(':p', md5($np));
  173.         $command->bindValue(':ida', $ida);
  174.         $command->execute();
  175.  
  176.         Yii::$app->session->set("msg_reset_password","Password reimpostata con successo. Prova ad accedere.");
  177.  
  178.         return $this->redirect("/login");
  179.     }
  180.  
  181.  
  182. #############################################################################################
  183. file /frontend/views/site/login.php  -  OVERWRITE FULL
  184. #############################################################################################
  185.  
  186.  
  187. <?php
  188.  
  189. /* @var $this yii\web\View */
  190. /* @var $form yii\bootstrap\ActiveForm */
  191. /* @var $model \common\models\LoginForm */
  192. /* @var $msg_reset_password string|null */
  193.  
  194. use frontend\components\Utilities;
  195. use yii\helpers\Html;
  196. use yii\bootstrap\ActiveForm;
  197.  
  198. $this->title = 'Login';
  199. $this->params['breadcrumbs'][] = $this->title;
  200. ?>
  201. <div class="site-login mar-bot-100">
  202.     <div class="row">
  203.         <div class="col-sm-offset-2 col-sm-8 col-md-offset-3 col-md-6" id="login-cont">
  204.  
  205.             <?php
  206.             if($msg_reset_password){
  207.                 echo '<div class="alert alert-success">'.$msg_reset_password.'</div>';
  208.                 Yii::$app->session->remove('msg_reset_password');
  209.             }
  210.             ?>
  211.  
  212.             <h1><?= Html::encode($this->title) ?></h1>
  213.  
  214.             <div class="mar-top-30" id="login-cont-form">
  215.  
  216.             <?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
  217.  
  218.                 <?php echo $form->field($model, 'username')->textInput(['autofocus' => true]); ?>
  219.                 <?php echo $form->field($model, 'password')->passwordInput(); ?>
  220.  
  221.                 <div class="form-group mar-top-30">
  222.                     <?php echo Html::submitButton('Accedi', ['class' => 'btn btn-primary pull-right', 'name' => 'login-button']); ?>
  223.                 </div>
  224.  
  225.                 <div class="clearfix"></div>
  226.  
  227.             <?php ActiveForm::end(); ?>
  228.  
  229.             </div>
  230.  
  231.             <div class="clearfix"></div>
  232.  
  233.             <div class="center" style="margin-top:20px;">
  234.                 Non hai ancora un accesso alla tua Area Personale?
  235.                 <br><br>
  236.                 <button class="btn btn-primary" data-target="#modal-richiesta-area-personale" data-toggle="modal">
  237.                     <i class="fa fa-user" style="color:white!important;"></i> RICHIEDI ORA
  238.                 </button>
  239.             </div>
  240.  
  241.             <div class="center" style="margin-top:20px;">
  242.                 Hai dimenticato la password di accesso alla tua Area Personale?
  243.                 <br><br>
  244.                 <button class="btn btn-primary" data-target="#modal-richiesta-recover-password" data-toggle="modal">
  245.                     <i class="fa fa-user" style="color:white!important;"></i> RECUPERA ORA
  246.                 </button>
  247.             </div>
  248.  
  249.             <div class="center mar-top-50">
  250.                 Hai un accesso come agenzia? <a href="http://servizi.nordovestenergie.it/login.php">Clicca qui</a>.
  251.             </div>
  252.  
  253.             <?php echo Utilities::getModalContent("modal-richiesta-area-personale","Richiedi accesso alla tua Area Personale",$this->renderFile('@app/views/layouts/partials/modalerichiestaareapersonale.php'),"Richiedi"); ?>
  254.             <?php echo Utilities::getModalContent("modal-richiesta-recover-password","Cambia la password di accesso alla tua Area Personale",$this->renderFile('@app/views/layouts/partials/modalerecuperopassword.php'),"Richiedi",'lg'); ?>
  255.  
  256.         </div>
  257.     </div>
  258. </div>
  259.  
  260.  
  261. #############################################################################################
  262. file /frontend/views/layouts/partials/modalerecuperopassword.php  -  NEW FILE
  263. #############################################################################################
  264.  
  265.  
  266. <form id="modal-richiesta-recover-password-form">
  267.  
  268.     <div class="subtitle center mar-top-10">
  269.         Inserisci qui il tuo codice fiscale e il tuo indirizzo e-mail.<br>
  270.         Ti invieremo al più presto una email con il link per poter reimpostare<br>
  271.         la password per l'accesso alla tua Area Personale.
  272.     </div>
  273.  
  274.     <div class="row mar-top-50">
  275.         <div class="col-sm-4 ralign cont-label">
  276.             <label>Codice Fiscale/Partita IVA *</label>
  277.         </div>
  278.         <div class="col-sm-8">
  279.             <div class="col-sm-8">
  280.                 <input type="text" class="form-control" name="codice-fiscale" placeholder="Inserisci il tuo codice fiscale" />
  281.             </div>
  282.         </div>
  283.     </div>
  284.  
  285.     <div class="row mar-top-50">
  286.         <div class="col-sm-4 ralign cont-label">
  287.             <label>Email *</label>
  288.         </div>
  289.         <div class="col-sm-8">
  290.             <div class="col-sm-8">
  291.                 <input type="text" class="form-control" name="email" placeholder="Inserisci la tua email" />
  292.             </div>
  293.         </div>
  294.     </div>
  295.  
  296.     <div class="small mar-top-20 center">* I campi contrassegnati con un asterisco sono obbligatori</div>
  297.  
  298. </form>
  299.  
  300. <div class="alert alert-danger nascosto mar-top-10" id="err-modal-richiesta-cambio-password"></div>
  301.  
  302.  
  303. #############################################################################################
  304. file /frontend/views/site/resetpassword.php  -  NEW FILE (click email, vista di actionResetpassword)
  305. #############################################################################################
  306.  
  307.  
  308. <?php
  309.  
  310. /* @var $this yii\web\View */
  311.  
  312. use frontend\components\Utilities;
  313. use yii\helpers\Html;
  314. use yii\bootstrap\ActiveForm;
  315.  
  316. if(empty($account))
  317.     die('Richiesta scaduta o non valida');
  318.  
  319. $this->title = 'Reset Password';
  320. $this->params['breadcrumbs'][] = $this->title;
  321. ?>
  322. <div class="site-login mar-bot-100">
  323.     <div class="row">
  324.         <div class="col-sm-offset-1 col-sm-10" id="login-cont">
  325.  
  326.             <h1><?= Html::encode($this->title) ?></h1>
  327.  
  328.             <div class="mar-top-30" id="login-cont-form">
  329.  
  330.                 <form id="modal-richiesta-recover-password-form" action="/saverp" method="post">
  331.  
  332.                     <div class="subtitle center mar-top-10">
  333.                         Inserisci qui la nuova password e confermala per teminare la reimpostazione.<br>
  334.                         Poi prova ad accedere nuovamente.
  335.                     </div>
  336.  
  337.                     <div class="row mar-top-50">
  338.                         <div class="col-sm-4 ralign cont-label">
  339.                             <label>Nuova password *</label>
  340.                         </div>
  341.                         <div class="col-sm-8">
  342.                             <div class="col-sm-8">
  343.                                 <input type="password" class="form-control" name="np" />
  344.                             </div>
  345.                         </div>
  346.                     </div>
  347.  
  348.                     <div class="row mar-top-50">
  349.                         <div class="col-sm-4 ralign cont-label">
  350.                             <label>Ripeti password *</label>
  351.                         </div>
  352.                         <div class="col-sm-8">
  353.                             <div class="col-sm-8">
  354.                                 <input type="password" class="form-control" name="npc" />
  355.                             </div>
  356.                         </div>
  357.                     </div>
  358.  
  359.                     <input type="hidden" name="ida" value="<?= $account['id_Account'] ?>">
  360.                     <input type="hidden" name="token" value="<?= $token ?>">
  361.  
  362.                     <div class="small mar-top-20 center">* I campi contrassegnati con un asterisco sono obbligatori</div>
  363.  
  364.                     <div class="form-group mar-top-30">
  365.                         <?php echo Html::submitButton('Salva', ['class' => 'btn btn-primary pull-right', 'name' => 'reset-password-button']); ?>
  366.                     </div>
  367.  
  368.                 </form>
  369.  
  370.                 <div class="alert alert-danger nascosto mar-top-10" id="err-modal-richiesta-cambio-password"></div>
  371.  
  372.                 <div class="clearfix"></div>
  373.  
  374.             </div>
  375.  
  376.             <div class="clearfix"></div>
  377.  
  378.         </div>
  379.     </div>
  380. </div>
  381.  
  382.  
  383. #############################################################################################
  384. file /frontend/web/js/site.js  -  UPDATE, verso riga 700, dopo blocco
  385. - $doc.on("click","#modal-richiesta-area-personale-btn",function(){
  386. #############################################################################################
  387.  
  388.  
  389.     $('#modal-richiesta-recover-password').on('shown.bs.modal', function (e) {
  390.         $("[name=codice-fiscale]").focus();
  391.     });
  392.  
  393.     $('#modal-richiesta-recover-password').on('hidden.bs.modal', function (e) {
  394.         var $form = $("#modal-richiesta-recover-password-form");
  395.         $form.find("input:text").val("");
  396.     });
  397.  
  398.     $doc.on("click","#modal-richiesta-recover-password input",function(){
  399.         cleanErrors();
  400.     });
  401.  
  402.     $doc.on("keyup","#modal-richiesta-recover-password input",function(){
  403.         cleanErrors();
  404.     });
  405.  
  406.     $doc.on("click","#modal-richiesta-recover-password-btn",function(){
  407.         var $btn = $(this);
  408.         var $form = $("#modal-richiesta-recover-password-form");
  409.  
  410.         $form.find(".alert").html("").hide();
  411.  
  412.         $btn.attr("disabled","disabled");
  413.         $.ajax({
  414.             url : "/invia-richiesta-recover-password",
  415.             data : $form.serialize(),
  416.             dataType : "json",
  417.             type : "post",
  418.             success : function(j){
  419.                 $btn.removeAttr("disabled");
  420.                 if(j) {
  421.                     if(typeof(j.success) != 'undefined') {
  422.                         $form.find("input:text").val("");
  423.                         closeModal("#modal-richiesta-recover-password");
  424.                         $("h1").after('<div class="fade in alert alert-success mar-top-20" id="mail-ok"><a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a><div class="inner-msg">' + j.msg + '</div></div>');
  425.                     } else {
  426.                         if(typeof(j.field) != 'undefined') {
  427.                             if(j.field != '') {
  428.                                 var f = $("#modal-richiesta-recover-password").find("[name=" + j.field + "]");
  429.                                 f.select();
  430.                                 f.addClass("error");
  431.                                 f.closest(".col-sm-8").append('<div class="alert alert-danger mar-top-10">' + j.msg + '</div>');
  432.                             } else {
  433.                                 $form.append('<div class="alert alert-danger mar-top-10">' + j.msg + '</div>');
  434.                             }
  435.                         }
  436.                     }
  437.                 }
  438.             }
  439.         });
  440.     });
  441.  
  442.  
  443.  
  444. #############################################################################################
  445. file /common/mail/send-richiesta-rp-html.php  -  NEW FILE (template mail HTML, richiamato dal controller Site)
  446. Testo da modificare PHP semicorretto
  447. #############################################################################################
  448.  
  449.  
  450. <h3>Richiesta accesso all'Area Personale Nordovest Energie</h3>
  451.  
  452. <?php
  453. /* @var $ida int */
  454. /* @var $codcli int */
  455. /* @var $email string */
  456. /* @var $codfisc string */
  457.  
  458. $token = sha1(uniqid($codcli."|".$email."|".$codfisc."|".time(),TRUE));
  459. // salvo token su tabella _Account
  460. $q = <<<SQL
  461. UPDATE _Account SET token=:t,token_exp=:te WHERE id_Account=:ida
  462. SQL;
  463.  
  464. $connection = Yii::$app->getDb();
  465. $command = $connection->createCommand($q);
  466. $command->bindValue(':t', $token);
  467. $command->bindValue(':te', date("Y-m-d H:i:s",time() + 2*86400));
  468. $command->bindValue(':ida', $ida);
  469. $command->execute();
  470. ?>
  471. https://www.nordovestenergie.it/rp/?t=<?= base64_encode($token) ?>
  472.  
  473. <div style="font-size:15px;">
  474.     <div><b>Codice Cliente: </b><?= str_pad($codcli,5,"0",STR_PAD_LEFT) ?></div>
  475.     <div><b>Codice Fiscale: </b><?= $codfisc ?></div>
  476. </div>
  477.  
  478. <div style="font-size:15px;margin-top:15px;margin-bottom:25px;">
  479.     Inviare una email all'indirizzo comunicatoci dal cliente <?= $email ?> con i dati per effettuare l'accesso.
  480. </div>
  481.  
  482. <?= \common\components\helpers\EmailHelper::footer() ?>
  483.  
  484.  
  485. #############################################################################################
  486. file /common/mail/send-richiesta-rp-text.php  -  NEW FILE (template mail testo, richiamato dal controller Site)
  487. Testo da scrivere NO HTML
  488. #############################################################################################
  489.  
  490. ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement