在cakephp 1.3中使用除用户之外的表进行身份验证

时间:2012-06-22 14:44:19

标签: cakephp

我正在尝试使用名为“accounts”的表代替users表进行身份验证。我尝试使用$this->Auth->userModel = 'Account';,但是当我尝试登录时,我显示了authError,但它确实阻止我访问其他任何内容。然后我尝试了这里建议的内容:cakephp-auth-component-using-different-table 但是这样做我在第21行的../app_controller.php中收到错误'致命错误:未定义的类常量'ALL''不知道还有什么可以尝试。

App_controller:

<?php
class AppController extends Controller { 
  var $components = array(
          'Auth'=> array(
                  'loginRedirect' => array('controller' => 'drugs', 'action' => 'index'),
                  'logoutRedirect' => array('controller' => 'accounts','action' => 'login')
                ), 
                'Session','Security');
  var $helpers = array('Form', 'Html', 'Session', 'Javascript');

  function beforeFilter() {
    $this->Auth->userModel = 'Account';
//    $this->Auth->allow('index','view');
    $this->Auth->authError = 'Please login to view that page.';
    $this->Auth->loginError = 'Incorrect Username/Password combination.';
    //$this->Auth->loginRedirect = array('controller'=>'drugs', 'action'=>'index');
    //$this->Auth->logoutRedirect = array('controller'=>'users', 'action'=>'login');
    $this->Auth->authenticate = array(
        //AuthComponent::ALL => array('userModel' => 'Account', 'scope' => array("Account.status" => 1), "fields" => array("username" => "email", "password" => "your_password_field"), 'Form', 'Basic'
    //); 
        AuthComponent::ALL => array('userModel' => 'Account', 'scope' => array("Account.status" => 1), 'Form', 'Basic'
    )); 


    $this->set('admin', $this->_isAdmin());
    $this->set('logged_in', $this->_loggedIn());
    $this->set('users_username', $this->_usersUsername());
    $this->set('user', $this->Auth->user('id'));
    $this->set('language', $this->Auth->user('language'));
  }

帐户管理员:

<?php
class AccountsController extends AppController {

    var $name = 'Accounts';

    function beforeFilter() {
      parent::beforeFilter();
      //$this->Auth->allow('add');

      if ($this->action == 'add' || $this->action == 'edit') {
       $this->Auth->authenticate = $this->Account; 
      }

    }

    function login() {

    }

    function logout() {
      $this->redirect($this->Auth->logout());
    }

    function index() {
      $this->Account->recursive = 0;
      $this->set('users', $this->paginate());
    }

    function add() {
      if (!empty($this->data)) {
        $this->Account->create();
        if ($this->Account->save($this->data)) {
          $this->Session->setFlash(__('The user has been saved', true));
          $this->redirect(array('action' => 'index'));
        }
        else {
          $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
        }
      }
    }

    function edit($id = null) {
      $this->Account->id = $id;
      $this->set('title_for_layout', 'Current Users');

      if (!is_numeric($id) && empty($this->data)) {
         $this->Session->setFlash('Invalid User');
         $this->redirect(array('action'=> 'index'));
      }

      if (!empty($this->data)) {
         if ($this->Account->save($this->data)) {
            $this->Session->setFlash('The user has been updated.');
            $this->redirect(array('action'=>'index'));
         }
         else {
            $this->Session->setFlash('The user could not be saved. Please, try again.');
         }
      }

      if (empty($this->data)) {
        $this->data = $this->Account->read(null, $id);
      }
    }

    function delete($id = null) {
      if (!is_numeric($id)) {
        $this->Session->setFlash(__('Invalid id for user.'));
      }
      if ($this->Account->delete($id)) {
        $this->Session->setFlash(__('User deleted.', true));
        $this->redirect(array('action'=>'index'));
      }
      $this->Session->setFlash(__('User was not deleted.',true));
      $this->redirect(array('action'=>'index'));
    }
}
?>

帐户模型:

<?php
class Account extends AppModel {
    var $name = 'Account';
    var $displayField = 'name';
    var $order = "Account.name ASC"; 
    var $hasMany = array(
      'FrenchTranslation'=> array(
        'className'=>'FrenchTranslation',
        'foreignKey' => 'user_id',
        'dependent' => false,
      ),
    );

    var $validate = array(
      'name'=>array(
        'Please enter the user\'s name.'=>array(
          'rule'=>'notEmpty',
          'message'=>'Please enter the user\'s name.'
        )
      ),
      'username'=>array(
        'Please enter the user\'s username.'=>array(
          'rule'=>'notEmpty',
          'message'=>'Please enter the username.'
        ),
        'The username must be between 3 and 15 characters.'=>array(
          'rule'=>array('between', 3, 15),
          'message'=>'The username must be between 3 and 15 characters.'
        ),
        'That username has already been taken.'=>array(
          'rule'=>'isUnique',
          'message'=>'That username has already been taken.'
        )
      ),
      'password'=>array(
        'The password must be between 5 and 15 characters.' =>array(
          'rule'=>array('between', 5, 15),
          'message'=>'The password must be between 5 and 15 characters.'
        ),
        'The passwords do not match'=>array(
          'rule'=>'matchPasswords',
          'message'=>'The passwords do not match.'
        )
      ),
      'password_confirmation'=>array(
        'You must confirm the password'=>array(
          'rule'=>'notEmpty',
          'message'=>'You must confirm the password.'
        )
      )
   );
?>

1 个答案:

答案 0 :(得分:0)

AuthComponent :: ALL来自哪里?

检查变量(例如,其userScope不是范围,并根据manual分配,

为什么不在AppController中全部分配它们?