如何将我的 cakephp2 项目迁移到 cakephp3?

How to migrate my cakephp2 project to cakephp3?

我想将我的 cakephp2 项目迁移到 cakephp3。 我必须保留用户的信息。 如何使它们具有相同的密码生成方式? 下面是我在 cakephp2 中生成密码的方法。

App::uses('AuthComponent', 'Controller/Component');
....
public function beforeSave($options = array()) {
      $this->data['User']['password'] = AuthComponent::password(
      $this->data['User']['password']
    );
    return true;
}

cakephp3文档是这样生成密码的:

namespace App\Model\Entity;

use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

/**
 * User Entity.
 */
class User extends Entity
{

    /**
     * Fields that can be mass assigned using newEntity() or patchEntity().
     *
     * @var array
     */
    protected $_accessible = [
        'email' => true,
        'password' => true,
        'bookmarks' => true,
    ];

    protected function _setPassword($value)
    {
        $hasher = new DefaultPasswordHasher();
        return $hasher->hash($value);
    }

}

它们不是相同的明文生成相同的密文。 所以,我无法保留cakephp2的用户信息。 您能告诉我如何设置成功的迁移项目吗?

请参阅 Migration Guide,它解释了您需要做的一切,特别是在 Auth 组件部分下提到了这一点:

Default is now the default password hasher used by authentication classes. It uses exclusively the bcrypt hashing algorithm. If you want to continue using SHA1 hashing used in 2.x use 'passwordHasher' => 'Weak' in your authenticator configuration.

请参阅:http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#authcomponent 了解更多信息。

来自CakePHP 3 migration guide:

  • Default is now the default password hasher used by authentication classes. It uses exclusively the bcrypt hashing algorithm. If you want to continue using SHA1 hashing used in 2.x use 'passwordHasher' => 'Weak' in your authenticator configuration.
  • A new FallbackPasswordHasher was added to help users migrate old passwords from one algorithm to another. Check AuthComponent’s documentation for more info.

阅读 AuthComponent documentation 显示了一个与此类似的示例:

$this->loadComponent('Auth', [
    'authenticate' => [
        'Form' => [
            'passwordHasher' => [
                'className' => 'Fallback',
                'hashers' => ['Default', 'Weak']
            ]
        ]
    ]
]);

当用户登录 AuthComponent 时将使用 Fallback 密码散列器 class,这将首先尝试 Default 散列方法(在上面的代码中使用)然后Weak 哈希器。

文档还继续向您展示如何在登录时更新用户密码以使用更安全的 Default 哈希器。