简体   繁体   中英

CakePHP: using a different users table

After setting up the simple Cakephp login concept I would like to let CakePHP use a different table to check the users and the login. I can't figure out how to change the table name within the Auth-component.

Below my basic Controller. How can I let Cakephp know it has to look into a different database table?

class AppController extends Controller {
  public $components = array(
      'Session',
      'Auth'=>array(
          'loginRedirect'=>array('controller'=>'users', 'action'=>'index'),
          'logoutRedirect'=>array('controller'=>'users', 'action'=>'index'),
          'authError'=>"You can't access that page",
          'authorize'=>array('Controller')
      )
  );

  public function isAuthorized($user) {
      return true;
  }

  public function beforeFilter() {
      //$this->Auth->allow('index', 'view');

      $this->set('siteCategory', 'home');   
      $this->set('logged_in', $this->Auth->loggedIn());
      $this->set('current_user', $this->Auth->user());
  }
}

Best Solution:

This is something that's done in the User model with the useTable property.

Ie in app/Model/User.php you should have something like this:

class User extends AppModel {

    public $useTable = 'table_name';

    //... rest of Model stuff here

}

Alternative:

Alternatively you can specify a different model to be used for the user, although I don't think that's what you're asking for. If I'm wrong there though, just set the userModel value like this:

public $components = array(
      'Auth'=>array(
          'authenticate'=>array(
              'Form' => array('userModel' => 'ADifferentUserModel')
)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM