Frage

Ich habe eine vorhandene Datenbank Ich versuche, einen Kuchen app oben auf setzen. Die alte App verwendet crypt () in Perl, um die Passwörter zu Hash. Ich brauche das gleiche in der PHP-Anwendung zu tun.

Wo ist der richtige Ort, dass die Änderung in einem Standard CakePHP App zu machen? Und was wäre eine solche Änderung aussehen?

War es hilfreich?

Lösung

Ich habe es funktioniert ...

hier ist mein 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');
    }
}

Dann ist hier der Benutzer:

<?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;
    }
}
?>

Alles andere ist normal, denke ich.

Hier ist eine gute Ressource: http : //teknoid.wordpress.com/2008/10/08/demystifying-auth-features-in-cakephp-12/

Andere Tipps

Eigentlich das oben beschriebene Verfahren durch DanB arbeitet in CakePHP 2.x für mich nicht Stattdessen landete ich eine benutzerdefinierte Auth Komponente zu schaffen, um den Standard-Hash-Algorithmus zu umgehen:

/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;
    }
}

... und verwenden Sie dann, dass in meinem Controller ...

public $components = array(
    'Session',
    'Auth' => array(
        'authenticate' => array(
            'CustomForm' => array(
                'userModel' => 'Admin'
            )
        )
    )
);

Dieser letzte Block kann auch innerhalb der before gesetzt werden Methode der AppController . In meinem Fall wähle ich es einfach zu setzen und zwar in einem Controller, wo ich benutzerdefinierte Authentifizierung mit einem anderen Benutzermodell verwenden würde.

Genau dies in CakePHP verfolgen 2.4.1, ich war der Bau ein Front-End für eine Legacy-Datenbank, die als md5 gespeichert vorhandene Benutzer-Passwörter hatte (Kontonummer: StaticText-: Passwort) und damit Benutzer anmelden mussten wir verwenden Sie diese Hashing-System.

Die Lösung war:

Erstellen Sie eine Datei app / Controller / Component / Auth / CustomAuthenticate.php mit:

<?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);
    }

}

Die „erstreckt FormAuthenticate“ bedeutet, dass diese Datei über die _findUser Funktion nimmt aber aufschiebt für alle anderen Funktionen wie gewohnt FormAuthenticate. Dies wird dann aktiviert, indem AppController.php Bearbeitung und zur Ergänzung der AppController Klasse etwas wie folgt aus:

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'),
            )
        ),
    )
);

Besonders hervorzuheben ist die Verwendung der assoziativen Arrays Taste ‚Benutzerdefiniert‘.

Schließlich ist es notwendig, das Passwort Hash, wenn Sie einen neuen Benutzer erstellen, so zum Modell-Datei (in meinem Fall Account.php) fügte ich hinzu:

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;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top