3
votes

My site has a public section for employees and back end for admin. It uses 2 different models, Employee and Admin.

I want to use Auth component for employee login and admin login. I know how to setup Auth component to use a Model other than default User model. But can i have auth component use 2 models, one for Employee authentication and other for Admin authentication? I am using admin_ prefix routing.

Is this possible? I searched but all i could found was tutorials on howto make Auth component use models other than User model.

Please advise!

EDIT

I use separate login forms for admin login and employee login. Both use the employee controller, but separate actions.

2
How you are checking whether user want to login as an Employee or Admin? Are you using any kind of radio buttons?Arun Jain
@goose , I also face with this problem, If you have been a solution, You should post your solution? I am waitting...Do Nhu Vy

2 Answers

0
votes

http://api.cakephp.org/class/auth-component

check the property authenticate, your answer is there!

and more : http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html. Look at authentication handlers!

Here is an example directly from cake page

<?php
// Basic setup
$this->Auth->authenticate = array('Form');

// Pass settings in
$this->Auth->authenticate = array(
'Form' => array('userModel' => 'Member'),
'Basic' => array('userModel' => 'Member')
);

Just put something else instead of Form and Basic and associate the good Model

0
votes

Considering you are using two radio buttons for Employee and Admin. Then you can use the following code into the login method.

 function login()
{       
    if ($this->request->is('post'))
    {
        $logged_in = false;
        $login_type = $this->request->data['User']['login_type']
        if ($login_type == 'Admin')
        {
             $this->Auth->authenticate = array('Form' => array('userModel' => 'Admin' ));       
        }
        else //if ($login_type == 'Employee')
        {
             $this->Auth->authenticate = array('Form' => array('userModel' => 'Employee' ));             
        }
        $this->Auth->constructAuthenticate();
        if ($this->Auth->login())
        {  
             $logged_in = true;                 
                     /*.... Do what you want............*/
        }
    }
}