public function run() { $model = new User(User::SCENARIO_LOGIN); // collect user input data if (isset($_REQUEST['User'])) { $model->attributes = $_REQUEST['User']; // validate user input and redirect to previous page if valid if ($model->validate()) { $errorCode = UserService::login($model); switch ($errorCode) { case UserIdentity::ERROR_NONE: $this->returnSuccess(); break; case UserIdentity::ERROR_USERNAME_INVALID: $model->addError('username', Yii::t('user', 'Username is not registered')); break; case UserIdentity::ERROR_PASSWORD_INVALID: $model->addError('password', Yii::t('user', 'Password is not valid')); break; case UserIdentity::ERROR_UNKNOWN_IDENTITY: $model->addError('username', Yii::t('user', 'Unkown identity')); break; } } } $this->returnOrRenderView($model, 'login'); }
function userLogin($username, $user_password) { // Open a connection to the database try { $pdo = new PDO(DB_PDODRIVER . ':host=' . DB_HOST . ';dbname=' . DB_NAME . '', DB_USER, DB_PASS); } catch (\PDOException $e) { echo "Connection failed: " . $e->getMessage(); exit; } // Create a new instance of UserService $login_service = new UserService($pdo, $username, $password); // Set user_id equal to login() if ($user_id = $login_service->login()) { $user_data = $login_service->getUser(); $pdo = null; header('Location: ../index.php'); } else { $pdo = null; header('Location: ../error.php?error=login'); } }
<?php $app->response->setStatus(200); $app->response()->headers->set('Content-Type', 'application/json'); $app->get("/login", function () use($app) { //Obtengo los parametros que vienen por get //Obtengo los parametros que vienen por todos los verbos http request->parameter('usuario'); $usuario = $app->request->get('usuario'); $password = $app->request->get('clave'); try { $service = new UserService(); $result = array("message" => $service->login($usuario, $password)); $app->response->body(json_encode($result)); } catch (UserNotFoundExeption $ue) { $app->response()->setStatus(200); $result = array("message" => $service->login($usuario, $password)); } catch (Exception $e) { $app->response()->setStatus(500); $app->response->body($e->getMessage()); } }); $app->post("/producto", function () use($app) { $nombreArticulo = $app->request->post('descripcion'); $precio = $app->request->post('precio'); $observacion = $app->request->post('observacion'); try { $connection = getConnection(); $stm = $connection->prepare("INSERT INTO productos() VALUES(?,?,?)"); $stm->bindParam(1, $nombreArticulo); $stm->bindParam(2, $precio); $stm->bindParam(3, $observacion);
<?php include '..' . DIRECTORY_SEPARATOR . 'init.php'; include SROOT . 'userService.class.php'; session_set_cookie_params(60 * 60 * 6); @session_start(); if (v("username") && v("password")) { $service = new UserService(); if ($service->login(v("username"), v("password"))) { header("Location: category_list.php"); } else { render('login'); } } else { render("login"); }
/** * Executa o servico de login de usuario, se conseguir logar seta session * Em caso de sucesso joga o usuario para o index do modulo * Em caso de erro mostra mensagem de erro no login * Redireciona para criacao de pedido caso o usuario tenha produtos no carrinho * @return void */ public function loginAction() { #fazer o login no sistema se for passado os parametros por post $this->response->setTemplate('identificacao'); $user = $this->response->getPost('user_login'); $pass = $this->response->getPost('pass_login'); $this->response->view->action = substr(get_class($this), 0, -4); $this->response->view->error = ""; $user_service = new UserService(); $user_session = $user_service->getLogin(); if (!empty($user) && !empty($pass)) { if (!$user_service->login($user, $pass)) { $this->response->view->error = "Login Inválido."; $this->response->view->user_login = $this->response->getPost('user_login'); } else { #logou, redirecionar para a index da class header('Location:/' . $this->response->view->action); } } }