How do I use a table other than “Users” for CakePHP's AuthComponent?

这一生的挚爱 提交于 2019-12-20 12:51:08

问题


CakePHP's AuthComponent assumes you have a Users table that contains a username and password. I'd like to find a way to override the default tablename from Users to Accounts.

Background Information:

The way I have designed my database is to have a Users table and an Accounts table.

Accounts:

  • id

  • user_id

  • username

  • password

  • authentication service (for example, my site, Facebook, Google, OpenID, etc.)

Users:

  • simply has all the personal information of the user (age, gender, etc.)

The reason for this is so that

  1. each user can have multiple accounts they can login from so they are not locked into one
  2. I can connect the third-party services to an account for more awesomeness

Now back to the problem....

CakePHP has documentation on changing the default field name, but I can't find anything on changing the default table name, but assume it would be similar in nature...

Example of changing the default field name:

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

Is there a way to accomplish this or should I restructure the tables keeping with CakePHP convention and still accomplish the same thing?


回答1:


In app_controller.php:

function beforeFilter() {
    $this->Auth->userModel = 'Account';
}



回答2:


Below code was helpful in my case as accepting username in email field, you can also define password hash in options.

$this->Auth->authenticate = array(
    'Basic' => array('userModel' => 'Account'),
    'Form' => array(
       'fields' => array('username' => 'email'),
       'userModel' => 'Account'
       )
 );


来源:https://stackoverflow.com/questions/437911/how-do-i-use-a-table-other-than-users-for-cakephps-authcomponent

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