Beispiel #1
0
 public function actionRegister()
 {
     //Creamos la instancia con el model de validación
     $model = new FormRegister();
     //Mostrará un mensaje en la vista cuando el usuario se haya registrado
     $msg = null;
     //Validación mediante ajax
     if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($model);
     }
     //Validación cuando el formulario es enviado vía post
     //Esto sucede cuando la validación ajax se ha llevado a cabo correctamente
     //También previene por si el usuario tiene desactivado javascript y la
     //validación mediante ajax no puede ser llevada a cabo
     if ($model->load(Yii::$app->request->post())) {
         if ($model->validate()) {
             //Preparamos la consulta para guardar el usuario
             $table = new Users();
             $table->username = $model->username;
             $table->email = $model->email;
             //Encriptamos el password
             $table->password = crypt($model->password, Yii::$app->params["salt"]);
             //Creamos una cookie para autenticar al usuario cuando decida recordar la sesión, esta misma
             //clave será utilizada para activar el usuario
             $table->authKey = $this->randKey("abcdef0123456789", 200);
             //Creamos un token de acceso único para el usuario
             $table->accessToken = $this->randKey("abcdef0123456789", 200);
             //Si el registro es guardado correctamente
             if ($table->insert()) {
                 //Nueva consulta para obtener el id del usuario
                 //Para confirmar al usuario se requiere su id y su authKey
                 $user = $table->find()->where(["email" => $model->email])->one();
                 $id = urlencode($user->id);
                 $authKey = urlencode($user->authKey);
                 $subject = "Confirmar registro";
                 $body = "<h1>Haga click en el siguiente enlace para finalizar tu registro</h1>";
                 $body .= "<a href='http://localhost:1234/proyecto/web/index.php?r=site/confirm&id=" . $id . "&authKey=" . $authKey . "'>Confirmar</a>";
                 //Enviamos el correo
                 Yii::$app->mailer->compose()->setTo($user->email)->setFrom([Yii::$app->params["adminEmail"] => Yii::$app->params["title"]])->setSubject($subject)->setHtmlBody($body)->send();
                 $model->username = null;
                 $model->email = null;
                 $model->password = null;
                 $model->password_repeat = null;
                 $msg = "Enhorabuena, ahora sólo falta que confirmes tu registro en tu cuenta de correo";
             } else {
                 $msg = "Ha ocurrido un error al llevar a cabo tu registro";
             }
         } else {
             $model->getErrors();
         }
     }
     return $this->render("register", ["model" => $model, "msg" => $msg]);
 }
 public function actionRegister()
 {
     $model = new FormRegister();
     $msg = null;
     if ($model->load(Yii::$app->request->post()) && Yii::$app->request->isAjax) {
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($model);
     }
     if ($model->load(Yii::$app->request->post())) {
         if ($model->validate()) {
             $table = new Users();
             $table->username = $model->username;
             $table->email = $model->email;
             $table->password = crypt($model->password, Yii::$app->params["salt"]);
             $table->authKey = $this->randKey("abcdef0123456789", 200);
             $table->accessToken = $this->randKey("abcdef0123456789", 200);
             if ($table->insert()) {
                 $user = $table->find()->where(["email" => $model->email])->one();
                 $id = urlencode($user->id);
                 $authKey = urlencode($user->authKey);
                 $subject = "Confirmar registro";
                 $body = "<h1>Haga click en el siguiente enlace para finalizar tu registro</h1>";
                 $body .= "<a href='http://localhost/basic/web/index.php?r=site/confirm&id= (http://localhost/basic/web/index.php?r=site/confirm&id=)" . $id . "&authKey=" . $authKey . "'>Confirmar</a>";
                 Yii::$app->mailer->compose()->setTo($user->email)->setFrom([Yii::$app->params["adminEmail"] => Yii::$app->params["title"]])->setSubject($subject)->setHtmlBody($body)->send();
                 $model->username = null;
                 $model->email = null;
                 $model->password = null;
                 $model->password_repeat = null;
                 $msg = "Enhorabuena, ahora sólo falta que confirmes tu registro en tu cuenta de correo";
             } else {
                 $msg = "Ha ocurrido un error al llevar a cabo tu  registro";
             }
         } else {
             $model->getErrors();
         }
     }
     return $this->render("register", ["model" => $model, "msg" => $msg]);
 }