CakePHP - authentication component, using a different table name and column name

时光总嘲笑我的痴心妄想 提交于 2019-12-04 02:21:55

问题


Just for organization sake, I wanted to use a different table for the authentication component to check, but it doesn't quite work. While I can initially state:

$this->Auth->userModel = "CoreUsers" plus set the loginAction to my proper MVC works to look at that table just to confirm it's there, but the login doesn't work, it only keeps returning an incorrect password. Something happens in the authentication component; I can't tell what makes it fail. When I rename my table to "Users", it works.

The other part is I'd prefer to actually use the column name of 'email' rather than 'username' since that's really what I'm using anyway.

I am just not having luck finding a complete tutorial and reference sets to do both these successfully with CakePHP 2.x. What is the way forward?

References:

  1. Stack Overflow question How do I use a table other than "Users" for CakePHP's AuthComponent?

  2. Stack Overflow question CakePHP - 'AuthComponent' with a different model name (not 'User')

(I had a look for answers, but I never quite got the whole answer.)


回答1:


Make sure your database table "core_users" and model "CoreUser" exists.

When you setup component you can put login/logout redirect here.

var $components = array(
        "Auth" => array(
             'loginRedirect' => array('controller' => 'dashboard', 'action' => 'index'),
             'logoutRedirect' => array('controller' => 'core_users', 'action' => 'login')
              ),
       "Session");

Now in beforeFilter medhod you can put the following

function beforeFilter(){
      $this->Auth->authenticate = array(
         AuthComponent::ALL => array('userModel' => 'CoreUser', 'scope' => array("CoreUser.status" => 1), "fields" => array("username" => "email", "password" => "your_password_field"), 'Form', 'Basic'
                                        );
}

Above example you can ignore status, if you need to pass any other info on the login verification u can use that. Now about you can remove 'Basic' if you only need form validation. I hope this would work .




回答2:


First, model names are generally singular. Are you sure you didn't mean CoreUser which would look in the core_users table?

Secondly, you can use the email field instead of username by setting the fields key on your auth setup.

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'CoreUser',
                'fields' => array('username' => 'email')
            )
        )
    )
);

See the book for more information.



来源:https://stackoverflow.com/questions/9929129/cakephp-authentication-component-using-a-different-table-name-and-column-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!