问题
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
- each user can have multiple accounts they can login from so they are not locked into one
- 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