public function TestDataBase() { // add $User = new Users(); $User->name = "test"; $User->email = '*****@*****.**'; $User->insert(); // find $userToDelete = $User::findOne(['name' => 'test']); $curUserId = $userToDelete->getAttributes(['id']); // var_dump($curUserId); // update $userToDelete->name = (int) $curUserId + 1005; $userToDelete->save(); // delete if ($userToDelete) { $userToDelete->delete(); } // show all // var_dump(Users::find()->asArray()->all()); }
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]); }
public function actionSignUp() { $model = new FormAccountSignUp(); $msg = NULL; $this->View->title = 'Registro de usuairo'; if (Gbox::getRequest()->isPost() && $model->load(Gbox::getRequest()->post())) { if ($model->password != $model->password_confirm) { $model->addError('password_confirm', 'Las contraseñas no coinciden.'); } if ($model->validate()) { $table = new Users(); $table->username = $model->username; $table->email = $model->email; $table->password = crypt($model->password, '$2y$10$' . Gbox::getConfig()->params['salt']); $table->firstname = $model->firstname; $table->lastname = $model->lastname; $table->auth_key = $this->randKey('abcdefghijklmnopqrstuvxyz0123456789', 128); $table->access_token = $this->randKey('abcdefghijklmnopqrstuvxyz0123456789', 128); $table->active = 1; if ($id_user = $table->insert()) { $msg = 'Se ha creado el usuario ' . $model->firstname . ' ' . $model->lastname . ' (' . $id_user . ').'; $model->username = null; $model->email = null; $model->password = null; $model->firstname = null; $model->lastname = null; } else { $msg = 'Ha ocurrido un error al guardar al nuevo usuario.'; } } else { $msg = 'Ocurrió un error, revise los campos y vuelva a intentarlo.'; } } return $this->render('sign-up', ['model' => $model, 'msg' => $msg]); }