2009-02-21 26 views
6

Tôi có một cơ sở dữ liệu hiện có Tôi đang cố gắng để đặt một ứng dụng bánh trên đầu trang của. Ứng dụng cũ đã sử dụng crypt() trong Perl để băm mật khẩu. Tôi cần phải làm tương tự trong ứng dụng PHP.Làm cách nào để thay thế thuật toán băm mật khẩu cakephp?

Địa điểm chính xác để thực hiện thay đổi đó trong ứng dụng bánh tiêu chuẩn ở đâu? Và một thay đổi như thế sẽ như thế nào?

Trả lời

8

tôi nhận nó làm việc ...

đây là AppController tôi:

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

Sau đó, ở đây là người dùng:

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

Mọi thứ khác là bình thường, tôi nghĩ.

Dưới đây là một nguồn lực tốt: http://teknoid.wordpress.com/2008/10/08/demystifying-auth-features-in-cakephp-12/

2

Trên thực tế phương pháp mô tả ở trên bởi danb không làm việc cho tôi trong CakePHP 2.x Thay vào đó tôi đã kết thúc việc tạo ra một thành phần tùy chỉnh auth để vượt qua các thuật toán băm tiêu chuẩn:

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

... và sau đó sử dụng trong điều khiển của tôi ...

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

Khối cuối cùng này cũng có thể được đặt bên trong phương thức trướcFilter của AppController. Trong trường hợp của tôi, tôi chỉ chọn đặt nó một cách cụ thể trong một bộ điều khiển nơi tôi sẽ sử dụng xác thực tùy chỉnh với một mô hình người dùng khác.

+1

Các tên tập tin cần phải được CustomFormAuthenticate.php - không CustomFormAuthentication .php –

+0

Cảm ơn, tốt bắt – jesal

+0

@ jesal Tôi biết đây là một chủ đề cũ 5 tháng nhưng bạn sẽ biết cách cấu trúc lớp CustomFormAuthenticate nếu tôi cần sử dụng cryp với email là muối.Rõ ràng, email không thể truy cập tại thời điểm này? Hãy giúp tôi! – rizalp1

1

Chỉ cần làm theo điều này trong CakePHP 2.4.1, tôi đã xây dựng giao diện người dùng cho cơ sở dữ liệu cũ có mật khẩu người dùng hiện có được lưu trữ dưới dạng md5 (accountnumber: statictext: password) và cho phép người dùng đăng nhập để sử dụng hệ thống băm đó.

Giải pháp là:

Tạo một ứng dụng tập tin/Controller/Component/Auth/CustomAuthenticate.php với:

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

} 

Các "kéo dài FormAuthenticate" có nghĩa là tập tin này tiếp quản chức năng _findUser nhưng defers để FormAuthenticate cho tất cả các chức năng khác như bình thường. Điều này sau đó được kích hoạt bằng cách chỉnh sửa AppController.php và thêm vào lớp AppController như sau:

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

Đặc biệt lưu ý việc sử dụng khóa mảng liên kết 'Tùy chỉnh'.

Cuối cùng nó là cần thiết để băm mật khẩu khi tạo một người dùng mới, vì vậy với mô hình tập tin (trong trường hợp của tôi Account.php) Tôi nói thêm:

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; 
} 
+0

Cảm ơn bạn rất nhiều @sverreg! Nhờ phản hồi của bạn, tôi đã có thể thực hiện quy trình mật khẩu của mình! – azerto00

+0

Hơn nữa giải pháp này cho phép chúng tôi chọn mô hình sẽ xử lý quá trình Auth. – azerto00

+0

Vui mừng khi đã giúp :-) – sverreg

Các vấn đề liên quan