Cake 2.2以降、contain
認証オプションにキーを追加して、関連データをプルできます。contain
キーはキーを受け入れるためfields
、フィールドを制限できます。
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'contain' => array(
'Profile' => array(
'fields' => array('name', 'birthdate')
)
)
)
)
)
);
ユーザーモデルが検索するフィールドを変更する場合は、使用している認証オブジェクトを拡張できます。通常、usersテーブルには最小限の情報が含まれているため、通常、これは必要ありません。
ただし、とにかく例を示します。ここではFormAuthenticateオブジェクトを使用_findUser
し、BaseAuthenticateクラスのほとんどのメソッドコードを使用します。これは、Cakeの認証システムがユーザーを識別するために使用する機能です。
App::uses('FormAuthenticate', 'Controller/Component/Auth');
class MyFormAuthenticate extends FormAuthenticate {
// overrides BaseAuthenticate::_findUser()
protected function _findUser($username, $password) {
$userModel = $this->settings['userModel'];
list($plugin, $model) = pluginSplit($userModel);
$fields = $this->settings['fields'];
$conditions = array(
$model . '.' . $fields['username'] => $username,
$model . '.' . $fields['password'] => $this->_password($password),
);
if (!empty($this->settings['scope'])) {
$conditions = array_merge($conditions, $this->settings['scope']);
}
$result = ClassRegistry::init($userModel)->find('first', array(
// below is the only line added
'fields' => $this->settings['findFields'],
'conditions' => $conditions,
'recursive' => (int)$this->settings['recursive']
));
if (empty($result) || empty($result[$model])) {
return false;
}
unset($result[$model][$fields['password']]);
return $result[$model];
}
}
次に、その認証を使用して、新しい設定を渡します。
public $components = array(
'Auth' => array(
'authenticate' => array(
'MyForm' => array(
'findFields' => array('username', 'email'),
'contain' => array(
'Profile' => array(
'fields' => array('name', 'birthdate')
)
)
)
)
)
);