【问题标题】:Using table other than users for authentication in cakephp 1.3在 cakephp 1.3 中使用用户以外的表进行身份验证
【发布时间】:2012-06-22 14:44:19
【问题描述】:

我正在尝试使用名为“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.'
        )
      )
   );
?>

【问题讨论】:

  • AuthComponent::ALL 是在 cake 2.0 中添加的,所以这对你不起作用。 userModel 应该可以工作:book.cakephp.org/1.3/en/view/1265/…
  • 所以一旦你删除 ::ALL 并且避免了致命的发生了什么?是否在登录尝试时查询帐户表?
  • @Leo 我受够了,决定只引用 database.php 配置中的另一个数据库,并告诉模型使用 var $useDbConfig = 'databasename';我想把它全部放在同一个数据库中,但这很好用。感谢您的帮助!

标签: cakephp


【解决方案1】:

AuthComponent::ALL 来自哪里?

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

为什么不直接在 AppController 中分配它们?

【讨论】:

  • 那来自另一篇帖子,我尝试过它是否能解决我的问题,但没有。我没有尝试任何工作。我认为只需在我的 app_controller 过滤器中使用 $this->Auth->userModel 就可以让我使用另一个表,但事实并非如此。我是否必须将 $this->Auth->user 的每个实例更改为我想要使用的表名?喜欢 $t​​his->Auth->account?
  • 仍然没有太多运气。使用不同的表应该不难。 ://
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多