登录后注册用户CakePHP中
问题描述:
我做了很多的研究,随后许多不同的例子,但仍无法得到它的正常运行。登录后注册用户CakePHP中
因此,这里是从注册控制器动作的一部分:
if(!empty($this->request->data)){
$this->request->data['Company']['start_date']= date("Y-m-d");
unset($this->Company->User->validate['company_id']);
if($this->Company->saveAssociated($this->request->data)){
$user = $this->request->data['User'];
$data['User']['password'] = $user[0]['password'];
$data['User']['email'] = $user[0]['email'];
if($this->Auth->login($data)){
$this->redirect($this->Auth->redirect(array('controller'=>'customers', 'action'=>'index')));
}...
所以用户保存和用户的电子邮件的一个新的数组并创建密码。然后,它被传递到$这个 - > Auth->登录。登录似传,但下面的错误是重定向到客户控制器:
Notice (8): Undefined index: role [APP\Controller\CustomersController.php, line 32]
Notice (8): Undefined index: role [APP\Controller\CustomersController.php, line 36]
即使角色字段是自动设置为用户创造经理。 这里是CustomerController的样子:
公共职能isAuthorized($用户){
if($user['role'] == 'manager'){
return true;}
if (in_array($this->action, array('add', 'edit', 'index', 'view', 'delete', 'users'))){
if($user['role'] != 'manager'){
return false;
}}return true;}
任何帮助是非常赞赏。
答
检查文档和AuthComponent::login()
- http://api.cakephp.org/2.4/class-AuthComponent.html#_login
- http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#manually-logging-users-in
- http://api.cakephp.org/2.4/source-class-AuthComponent.html#583-606
当传递用户数据AuthComponent::login()
,要登录用户的来源,但没有认证将要发生! “记录在”在这种情况下是指,只要被存储在会话中的数据,并在以下的用户被视为在病例数据登录请求存在于会议(在由组件所使用的特定的键),即你甚至可以通过123456
,用户将被视为登录。
另一方面进行身份验证会导致数据库查找,其中所有的用户数据将被提取并因此被存储在会话中。
所以,因为你没有把它递给AuthComponent::login()
的role
字段不可,你唯一通过email
和password
,因此这些都被后来获得的唯一领域。顺便说一句,做这样的手动登录时不提供密码!你不想在会议中携带这些敏感信息!
要解决这个问题,不是太通role
场,或拨打AuthComponent::login()
,而完全不经过任何数据(请确保您使用的认证形式,使所用的请求传递的数据),所以它会验证用户并从数据库中获取其数据。
另请参见http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html