我有一个现有的数据库,我试图把蛋糕应用之上。旧的应用程序使用在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'
            )
        )
    )
);

此最后的块,也可以把 beforeFilter内 AppController的的方法。在我来说,我只选择把它特别是在一个控制器我要去哪里使用自定义身份验证使用不同的用户模型。

只是为了CakePHP中2.4.1跟进,我是建设一个前端为具有存储为MD5现有用户的口令旧的数据库(ACCOUNTNUMBER:静态文本:密码),并允许用户登录,我们需要使用该散列系统,以及

的解决方案是:

创建一个文件的应用程序/控制器/组件/认证/ 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'),
            )
        ),
    )
);

在特别需要注意使用关联数组键“自定义”。的

最后,有必要创建一个新用户时,所以模型文件(在我的情况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;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top