我正在使用Usermgmt插件。对于登录过程,我使用ajax发出请求。这适用于localhost,但不适用于托管。 在托管功能登录工作,除了呼叫“$ this-> UserAuth-> login($ user)”
在Userscontroller.php中登录功能
public function login($type=0,$subtype=0) {
$this->layout = 'plantilla';
if ($this->request -> isPost()) {
if($this->User->LoginValidate()) {
$email = $this->data['User']['email'];
$password = $this->data['User']['password'];
$user = $this->User->findByUsername($email);
if (empty($user)) {
$user = $this->User->findByEmail($email);
if (empty($user)) {
//$this->Session->setFlash(__('Usuario y/o contraseña incorrecto.'));
echo "Usuario y/o contraseña incorrecto";
exit();
}
}
// check for inactive account
if ($user['User']['id'] != 1 and $user['User']['active']==0) {
//$this->Session->setFlash(__('Su registro no ha sido confirmado por favor verifique su correo electrónico o contacte al administrador'));
echo "Su registro no ha sido confirmado por favor verifique su correo electrónico o contacte al administrador";
exit();
}
$hashed = $this->UserAuth->makePassword($password);
if ($user['User']['password'] === $hashed) {
$this->UserAuth->login($user);
$remember = (!empty($this->data['User']['remember']));
if ($remember) {
$this->UserAuth->persist('2 weeks');
}
$OriginAfterLogin=$this->Session->read('Usermgmt.OriginAfterLogin');
$this->Session->delete('Usermgmt.OriginAfterLogin');
$rol = $this->User->find('all',array('conditions' => array('email' => $email)));
if($rol['0']['User']['user_group_id']==1){
$redirect = "/projects";//(!empty($OriginAfterLogin)) ? $OriginAfterLogin : loginRedirectUrl;
//$this->redirect($redirect);
echo 1;
exit();
}
if($this->data['User']['page'] == "projects"){
echo 2;
exit();
}
else {
$redirect = "/";
//$this->redirect($redirect);
echo 0;
exit();
}
} else {
//$this->Session->setFlash(__('Usuario y/o contraseña incorrecto.'));
echo "Usuario y/o contraseña incorrecto.";
exit();
}
}else{
$this->User->set($this->data);
//$this->Session->setFlash(__('Formulario Vacío.'));
echo "Formulario Vacío";
exit();
}
}
$conditions=$subtype!=0?array('Project.project_subtype_id'=>$subtype):array('Project.project_type_id'=>$type);
$this->set('parents_type', $this->ProjectType->find('all', array('conditions' => array('ProjectType.parent_id' => 0))));
$this->set('projects', $this->Project->find('all',array('conditions'=>$conditions)));
}
Peticion con ajax
$(function() {
//Actions to submit login modal
$('#user-login').submit(function() {
var ser = $('#user-login').serialize();
$.ajax({
async: true,
type: 'POST',
url: '<?php echo $this->webroot?>usermgmt/users/login',
data: ser,
success: function(msg){
if(msg == 1){ window.location = '<?php echo $this->webroot?>projects';}
else if(msg == 2){ location.reload(true);}
else if(msg == 0){ window.location ='<?php echo $this->webroot?>';}
else {$("#error-login").html("<div class='alert alert-danger'>"+msg+"</div>");}
//alert(msg);
},
error: function(msg){
alert("Error");
}
});
return false;
});