2014-02-08 37 views
0

我想这是一个愚蠢的问题,但我无法登录到我在Cake中构建的网站的受限制部分。无法使用AuthComponent在蛋糕中登录php

对于开始,我发现$this->request->data['Usuario']['clave']中的密码字符串与模型中的beforeSave函数中使用SimplePasswordHasher的散列字符串不同。我还要说,该模型是不是默认用户模式,因为我写了西班牙语的应用程序,我不想使用默认模式,所以我对部件的构成是:

class AppController extends Controller { 
    /*function beforeFilter() { 
     date_default_timezone_set('America/Mexico_City'); 
    }*/ 
    public $components = array(
     'Session', 
     'Auth' => array(
      'Form'=>array(
       'userModel' => 'Usuario', 
       'unauthorizedRedirect' => false, 
       'loginRedirect' => array(
        'controller' => 'ComplejosResidenciales', 
        'action' => 'index' 
       ), 
       'logOutRedirect' => array(
        'controller' => 'Usuarios', 
        'action' => 'index' 
       ), 
       'fields' => array(
        'username' => 'usuario', 
        'password' => 'clave' 
       ), 
       'authorize' => 'Controller' 
      ) 
     ) 
    ); 
} 

所以我决定不散列密码字段,但仍然无济于事。 我希望任何人都可以借我一手,因为我是CakePHP的新手,不知道如何解决它。

我认为它必须是与Auth->login()方法的东西,因为我没有遵循约定在这里,但我不知道如何配置上述方法。目前就像如下:

public function login() { 
    if($this->request->is('post')) { 
     if($this->Auth->login()) { 
      return $this->Auth->redirectUrl($this->Auth->redirectUrl());  
     } 
     else { 
      $this->Session->setFlash(__('Las credenciales proporcionadas no son correctas'), 'default', array(), 'auth'); 
     } 

由于RRD指出,我的$ components数组是错误的,所以我把它改为:根据cakephp.org

public $components = array(
'Session', 
'Auth'=>array('loginRedirect'=>array('controller'=>'ComplejosResidenciales',  'action'=>'index'), 'logOutRedirect'=>array('controller'=>'Usuarios', 'action'=>'index'),  'loginAction'=>array('controller'=>'Usuarios', 'action'=>'login'),  'authenticate'=>array('Form'=>array('userModel'=>'Usuario',  'fields'=>array('username'=>'usuario', 'password'=>'clave'))))); 

哪个更好,

不要将其他Auth配置密钥(如authError,loginAction等)放在authenticate或Form元素中。它们应该与认证密钥处于同一级别。

但它不工作。

一直在努力,我无法得到它的窍门,我希望有人会指出我做错了什么。在我的AppController我已经宣布了组件和beforeFilter函数是这样的:

public $components =  array('Auth'=>array('loginRedirect'=>array('controller'=>'ComplejosResidenciales',  'action'=>'index'), 
'logoutRedirect'=>array('controller'=>'Usuarios', 'action'=>'login'), 
), 'Session'); 

public function beforeFilter(){ 
     $this->Auth->authenticate = array(
     AuthComponent::ALL => array('userModel' => 'Usuario', "fields" => array("username" => "usuario", "password" => "clave"), 'Form')); 
} 

然后,我有登录功能肚里(很明显,我猜)在UsuariosController,就像这样:

public function login() { 
    if($this->request->is('post')) { 
    if($this->Auth->login()) { 
    return $this->Auth->redirectUrl($this->Auth->loginRedirect);  
    } 
    else { 
    $this->Session->setFlash(__('Las credenciales proporcionadas no son correctas'),  'default', array(), 'auth'); 
    } 
    } 
    } 

但我只是继续看到“Las credenciales proporcionadas no son correctas”的消息。我不知道我是否在部分$this->Auth->login()中正确调用了Auth组件的方法,因为显然我在没有参数的情况下调用它时没有结果,但我尝试使用参数$this->request->data调用它,结果它没有无论我在用户名和密码字段中写什么都不重要,任何事情都会通过,这当然是不好的。

现在我明白为什么编码$this->Auth->login($this->request->data)导致给予不受限制的访问:

在2.X $这个 - > Auth->登录($这个 - >请求 - >数据)会记录用户与无论发布什么数据,在1.3 $ this-> Auth-> login($ this-> data)中都会尝试首先识别用户,并且只有在成功登录时才会登录。

根据蛋糕的手册。我似乎无法正确读取关于此的任何文件。无论如何,我恳求有人会帮助我,因为我在这里很匆忙。阅读一些其他文件后,我想这验证组件要正确处理好一切,只要我提供正确的配置,所以我已经结束了在AppController中做一个beforeFilter()调用,就像这样:

var $components = array('Auth', 'Session'); 

public function beforeFilter() { 
$this->Auth->loginAction = array('controller'=>'Usuarios', 'action'=>'login'); 
$this->Auth->redirectLogin = array('controller'=>'ComplejosResidenciales', 'action'=>'add'); 
$this->Auth->authenticate = array('Form'=>array('userModel'=>'Usuario',  'fields'=>array('username'=>'usuario', 'password'=>'clave'))); 
} 

然后,在我的“UsuariosController”我做的:

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow('index', 'view', 'add', 'edit', 'delete'); 
    } 

我有我的登录和注销功能,很简单,但它不工作,它不重定向我在登录时也不让我访问任何其他控制器,它似乎什么都不做。请帮忙!

/** 
* login and logout functions 
* 
*/ 

    public function login() { 
    } 
    public function logout() { 
    $this->redirect($this->Auth->logout()); 
    } 
+0

对此,您应该清楚Auth的主要原则。阅读手册或https://leanpub.com/CakePHPUserAuthentication 这样的书你的验证数组声明是不正确的。你使用的是什么版本的CakePHP? – rrd

+0

@rrd我使用的是蛋糕2.4.5,但我没有得到我的组件声明有什么问题,我真的没有开发太多的授权部分,但现在我只是试图限制所有控制器并允许它们在每个的beforeFilter部分中根据需要 –

回答

0

你应该改变更多的东西。

$组件应该是这样的:

public $components = array(
      'Session', 
      'Auth' => array(
       'authenticate' => array(
        'Form' => array(
         'fields' => array('username'=>'usuario', 'password'=>'clave') 
        ) 
       ) 
      ) 
    ); 

我不知道有关的usermodel,检查手册。

比你应该实现isAuthorized

function isAuthorized($user){ 
if(in_array($this->action, array('view', 'edit'))) 
    return true; 
return false; 
} 

beforeFilter不需要你的情况。

您的登录方法是这样的。

public function login() { 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       return $this->redirect($this->Auth->redirectUrl()); 
      } 
     else { 
      $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); 
      } 
     } 
    } 

此外,我建议您阅读上述手册或书籍。它有一个免费的样本,我想你会得到这个主要想法。