/** * Displays the login page, * If there is an error in validation or parameters it returns the form code with errors * if everything is ok, it returns JSON with result=>1 and realname=>"..." parameters * ATTENTION: This function is also used by mobile clients */ public function actionLogin() { $model = new LoginForm(); $processOutput = true; // collect user input data if (isset($_POST['LoginForm'])) { $model->attributes = $_POST['LoginForm']; // validate user input and if ok return json data and end application. if ($model->validate() && $model->login()) { echo CJSON::encode(array("result" => "1", "id" => Yii::app()->user->id, "realname" => $model->getName(), "minDataSentInterval" => Yii::app()->params->minDataSentInterval, "minDistanceInterval" => Yii::app()->params->minDistanceInterval)); Yii::app()->end(); } if (Yii::app()->request->isAjaxRequest) { $processOutput = false; } } if (isset($_REQUEST['client']) && $_REQUEST['client'] == 'mobile') { if ($model->getError('password') != null) { $result = $model->getError('password'); } else { if ($model->getError('email') != null) { $result = $model->getError('email'); } else { if ($model->getError('rememberMe') != null) { $result = $model->getError('rememberMe'); } } } echo CJSON::encode(array("result" => $result)); Yii::app()->end(); } else { Yii::app()->clientScript->scriptMap['jquery.js'] = false; Yii::app()->clientScript->scriptMap['jquery-ui.min.js'] = false; $this->renderPartial('login', array('model' => $model), false, $processOutput); } }
/** * Login process */ public function executeLogin(sfWebRequest $r) { // If we've posted the form if ($r->isMethod('post')) { // Launching form $login = new LoginForm(); // Binding fields to validators $login->bind($r->getParameter($login->getName())); // Doing a little sleep to prevent automatic bruteforce sleep(1); // If form is valid if ($login->isValid()) { // Fetching account for this user $q = Doctrine::getTable("Users")->findOneByUsername($login->getValue("username")); // Setting cookies for auto-login $this->getResponse()->setCookie("uid", $q->getId(), time() + 365 * 3600 * 24); $this->getResponse()->setCookie("pwd", $q->getCookiesHash(), time() + 365 * 3600 * 24); // Informing user $this->getUser()->setFlash("notice", $this->getContext()->getI18N()->__("Happy to see you %s% !", array("%s%" => $q->getUsername()))); if ($r->isXmlHttpRequest()) { $this->getResponse()->setStatusCode(202); return $this->renderText(""); } } else { $c = (string) $login->getErrorSchema(); preg_match_all('#(.+) \\[(.+)\\]#U', $c, $m); $m[1] = array_map('trim', $m[1]); die(json_encode($m, JSON_FORCE_OBJECT)); } } // Redirect to homepage $this->redirect("@homepage"); }