ケーキアプリを上に置こうとしている既存のデータベースがあります。古いアプリでは、Perl の crypt() を使用してパスワードをハッシュしていました。PHPアプリでも同じことをする必要があります。
標準のcakephpアプリでその変更を行う正しい場所はどこですか? そして、そのような変化はどのように見えるでしょうか?
ケーキアプリを上に置こうとしている既存のデータベースがあります。古いアプリでは、Perl の crypt() を使用してパスワードをハッシュしていました。PHPアプリでも同じことをする必要があります。
標準のcakephpアプリでその変更を行う正しい場所はどこですか? そして、そのような変化はどのように見えるでしょうか?
私はそれを動作させました...
ここに私のAppControllerがあります:
class AppController extends Controller {
var $components = array('Auth');
function beforeFilter() {
// this is part of cake that serves up static pages, it should be authorized by default
$this->Auth->allow('display');
// tell cake to look on the user model itself for the password hashing function
$this->Auth->authenticate = ClassRegistry::init('User');
// tell cake where our credentials are on the User entity
$this->Auth->fields = array(
'username' => 'user',
'password' => 'pass',
);
// this is where we want to go after a login... we'll want to make this dynamic at some point
$this->Auth->loginRedirect = array('controller'=>'users', 'action'=>'index');
}
}
次に、ユーザーは次のとおりです。
<?php
class User extends AppModel {
var $name = 'User';
// this is used by the auth component to turn the password into its hash before comparing with the DB
function hashPasswords($data) {
$data['User']['pass'] = crypt($data['User']['pass'], substr($data['User']['user'], 0, 2));
return $data;
}
}
?>
それ以外は普通だと思います。
ここに良いリソースがあります: http://teknoid.wordpress.com/2008/10/08/demystifying-auth-features-in-cakephp-12/
実際、上記の danb による方法は、CakePHP 2.x では機能しませんでした。代わりに、標準のハッシュ アルゴリズムをバイパスするカスタム認証コンポーネントを作成することになりました。
/app/Controller/Component/Auth/CustomFormAuthenticate.php
<?php
App::uses('FormAuthenticate', 'Controller/Component/Auth');
class CustomFormAuthenticate extends FormAuthenticate {
protected function _password($password) {
return self::hash($password);
}
public static function hash($password) {
// Manipulate $password, hash, custom hash, whatever
return $password;
}
}
...そして、それをコントローラーで使用します...
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'CustomForm' => array(
'userModel' => 'Admin'
)
)
)
);
この最後のブロックは、 AppControllerのbeforeFilterメソッド内に配置することもできます。私の場合、別のユーザー モデルでカスタム認証を使用する 1 つのコントローラーに具体的に配置することを選択しました。
これを CakePHP 2.4.1 でフォローアップするために、既存のユーザーパスワードが md5(accountnumber:statictext:password) として保存されているレガシーデータベースのフロントエンドを構築していました。ユーザーがログインできるようにするには、そのハッシュを使用する必要がありました。システムも。
解決策は次のとおりです。
次のファイル app/Controller/Component/Auth/CustomAuthenticate.php を作成します。
<?php
App::uses('FormAuthenticate', 'Controller/Component/Auth');
class CustomAuthenticate extends FormAuthenticate {
protected function _findUser($username, $password = null) {
$userModel = $this->settings['userModel'];
list(, $model) = pluginSplit($userModel);
$fields = $this->settings['fields'];
if (is_array($username)) {
$conditions = $username;
} else {
$conditions = array(
$model . '.' . $fields['username'] => $username
);
}
if (!empty($this->settings['scope'])) {
$conditions = array_merge($conditions, $this->settings['scope']);
}
$result = ClassRegistry::init($userModel)->find('first', array(
'conditions' => $conditions,
'recursive' => $this->settings['recursive'],
'contain' => $this->settings['contain'],
));
if (empty($result[$model])) {
return false;
}
$user = $result[$model];
if ($password) {
if (!(md5($username.":statictext:".$password) === $user[$fields['password']])) {
return false;
}
unset($user[$fields['password']]);
}
unset($result[$model]);
return array_merge($user, $result);
}
}
「FormAuthenticate を拡張する」とは、このファイルが _findUser 関数を引き継ぐが、他のすべての関数については通常どおり FormAuthenticate に従うことを意味します。これは、AppController.php を編集し、AppController クラスに次のように追加することでアクティブ化されます。
public $components = array(
'Session',
'Auth' => array(
'loginAction' => array('controller' => 'accounts', 'action' => 'login'),
'loginRedirect' => array('controller' => 'accounts', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'authenticate' => array (
'Custom' => array(
'userModel' => 'Account',
'fields' => array('username' => 'number'),
)
),
)
);
特に、連想配列キー「Custom」の使用に注意してください。
最後に、新しいユーザーを作成するときにパスワードをハッシュする必要があるため、モデル ファイル (私の場合は Account.php) に次を追加しました。
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = md5($this->data[$this->alias]['number'].":statictext:".$this->data[$this->alias]['password']);
}
return true;
}