0

I have a website which had initially a login system for only users of the website. And I am using CakePHP's form based Auth.

Now I have Supplier and Distributer accounts and want to allow both types of user to Login to the website. But their username and password are stored in corresponding tables.

in my beforeFilter I have Auth code like

$this->Auth->authenticate = array(
    'Form' => array(
        'userModel' => 'User',
        'fields' => array(
            'username' => 'username',
            'password' => 'password'
        ),
        'scope' => array(
            'User.active' => 1,
        )
    )
);

if(isset($this->request->params['admin']) && ($this->request->params['prefix'] == 'admin')) {
     if($this->Session->check('Auth.User')) {
         $this->set('authUser', $this->Auth->user());
         $loggedin = $this->Session->read('Auth.User');
         $this->set(compact('loggedin'));
         $this->layout = 'admin';
     }
} 

How may I extend my code that it accomodates both above mentioned types too.

Holt
  • 36,600
  • 7
  • 92
  • 139
user3733648
  • 1,323
  • 1
  • 10
  • 25
  • Rather than have multiple login tables it would be much simpler to have a single table containing the login details which your `Supplier` and `Distributor` models have a `belongsTo` association with. I'm not sure it is possible to have multiple login tables used for the same authentication process. To be honest multiple login tables would be difficult to manage as you'd need to ensure usernames remained unique across all models. – drmonkeyninja Jul 28 '15 at 11:49
  • adding to @drmonkeyninja, if you continue down this solution, I would recommend you create a SQL view UNIONing the two tables' username and password columns. Then use this SQL view as the basis of Cakephp UserModel. You will also need to bring the ID column and a 4th column, indicating what the source table is. – AgRizzo Jul 28 '15 at 12:24
  • possible duplicate of [CakePHP Auth with two tables (models)](http://stackoverflow.com/questions/24636263/cakephp-auth-with-two-tables-models) – Holt Jul 28 '15 at 14:29

0 Answers0