2009-06-06 60 views
2

我试图构建登录注册页面,我的网站,我现在面临的问题与他AUTH组件,为什么Auth Component的用户名和密码登录automagic在CakePHP中按预期工作?

function beforeFilter() { 
    $this->Auth->loginAction = array('controller'=>'users','action'=>'login'); 
    $this->Auth->loginRedirect = array('controller'=>'users','action'=>'landing'); 
    $this->Auth->logoutRedirect = array('controller'=>'users','action'=>'home'); 

    // These pages do not require authenication<br/> 
    $this->Auth->allow('home','register','activate','forgot','reset','_sendEmail','reset'); 
} 

我使用$this->Auth->password方法加密并存储在数据库中的密码,现在连当我登录在成功地我不重定向到下用户控制的着陆页,我试着打印出$this->Auth和它说

[loginError] => Login failed. Invalid username or password. 
[authError] => You are not authorized to access that location. 

而且在我的数据库对里面我是用认证的领域是电子邮件和密码,我读的地方, AuthComponent需要字段t o是用户名和密码automagic工作

我无法弄清楚我做错了,也登录后,如果我试图指向我的浏览器http://cake.localhost/users/register它应该会自动重定向到目标页面,但它doesnt因为某些原因。

任何线索,我去哪里错了?

希夫

+0

我可以假设,这是里面app_controller.php?您是否意外地将此控制器添加为授权的acos?那么你可能不得不去确保所有用户组都可以看到这个消息。 – codingbear 2009-06-06 05:33:30

+0

这是里面UsersController – Shiv 2009-06-06 17:05:23

+0

loginAction/loginRedirect/logoutRedirect应该在app_controller的beforeFilter()..至少,这就是我发现作为一个更好的设计 – codingbear 2009-06-07 04:33:20

回答

-1

如果正在使用CakePHP 1.2,则需要更改:

$this->Auth->allow('home','register','activate','forgot','reset','_sendEmail','reset'); 

$this->Auth->allow(array('home','register','activate','forgot','reset','_sendEmail','reset')); 

通知所添加的 “阵列()” 中的允许函数。

+0

哎呀抱歉,但它仍然没有解决我的问题 – Shiv 2009-06-06 05:26:19

+1

你是否包括var $ components = array('Auth')? 此外,我发现这篇文章是一个很好的解决方案,不使用ACL: http://www.studiocanaria.com/articles/cakephp_auth_component_users_groups_permissions_revisited – Jefe 2009-06-06 05:29:23

+0

有趣...我会浏览这篇文章,是的,我已经设置组件变量 – Shiv 2009-06-06 05:31:46

1

尝试$ this-> Auth-> allowedActions = array('*');在控制器的beforeFilter()(不在app_controller)中。

3

从CakePHP的食谱:

默认情况下,AuthComponent希望你有一个名为“用户”与使用所谓的“用户名”和“密码”字段的表。

我想知道如果您使用email作为字段名称,automagic会起作用吗?

尝试添加以下内容到beforefilter:

function beforeFilter() { 
    $this->Auth->fields = array(
     'username' => 'email', 
     'password' => 'password' 
    ); 
} 
+0

我已经把它放在已经 – Shiv 2009-06-11 13:44:35

+0

帮助我。谢谢。 – zgnilec 2012-05-29 13:29:21

0

我解决这个问题的方法,这是一个真正的黑客是我改名为表单域密码2,然后IIN我的控制器方法我设置

 

$this->data['password] = $this->data['password2] 
 

然后我打电话

 

$this->Auth->login($this->data) 
 

,它似乎工作。 我不认为这是最好的方式,但它的工作,我会一起去,直到我找到一个更好的解决方案。

+0

在调用$ this-> Auth-> login()之前,您是否在调用之前手动加密$ this-> data ['password']?如果是这样,那么问题在于因为登录方法也会对密码进行加密,所以期望它以纯文本形式提供。 – 2009-10-16 06:18:08

0

在你的app_controller中。PHP

function beforeFilter() { 
    $this->Auth->fields = array('username' => 'email', 'password' => 'password'); 
    $this->Auth->loginAction = array('controller'=>'users','action'=>'login'); 
    $this->Auth->loginRedirect = array('controller'=>'users','action'=>'landing'); 
    $this->Auth->logoutRedirect = array('controller'=>'users','action'=>'home'); 
} 

在用户控制器:

function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow(list of your allowed actions); 
} 
相关问题