Reputation: 89
I am using CakePHP 2.3.6. In a project, I have 2 type of users : Students
and Admin
. So, I created 2 Controllers
for 2 type of users, namely, StudentsController
and AdminsController
. I have different Authentication
configuration for these 2 controllers, so I configured AuthComponent
individually in 2 controllers. And I want a common login()
function implementation for 2 kind of users, so that I don't have to write same code twice.
Here is my code :
AppController.php :
public $components=array('Session','RequestHandler','Acl','Auth'=>array('authorize'=>array('Actions'=>array('actionPath'=>'controllers'))));
StudentsController.php :
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->loginRedirect=array('controller'=>'students','action'=>'editProfile');
$this->Auth->logoutRedirect=array('controller'=>'students','action'=>'index');
$this->Auth->authenticate=array('Form'=>array('scope'=>array('User.group_id'=>2),'userModel'=>'User','fields'=>array('username'=>'username','password'=>'password')));
$this->Auth->unauthorizedRedirect=array('controller'=>'users','action'=>'login');
$this->Auth->loginAction=array('controller'=>'users','action'=>'login');
$this->Auth->allow('login','index','createProfile');
$this->layout='student_layout';
}
AdminsController.php :
public function beforeFilter(){
parent::beforeFilter();
$this->Auth->loginRedirect=array('controller'=>'admins','action'=>'myJobs');
$this->Auth->logoutRedirect=array('controller'=>'admins','action'=>'index');
$this->Auth->authenticate=array('Form'=>array('scope'=>array('User.group_id'=>1),'userModel'=>'User','fields'=>array('username'=>'username','password'=>'password')));
$this->Auth->authError='Did you really think you are allowed to see that ?';
$this->Auth->unauthorizedRedirect=array('controller'=>'admin','action'=>'index');
$this->Auth->loginAction=array('controller'=>'users','action'=>'login');
$this->Auth->allow('index');
$this->layout='admin_layout';
}
UsersController.php :
public function login(){
if($this->request->is('post'))
if($this->Auth->login()){
$welcome=($this->Auth->user('group_id')==2)?'Welcome '.$this->Student->field('name',array('Student.id'=>$this->Auth->user('id'))):(($this->Auth->user('group_id')==1)?"<p style='margin-left:20px;color:#366;'><strong>Welcome Admin, You have successfully entered to your Admin Panel!</strong></p>":"");
$this->Session->setFlash($welcome);
return $this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Invalid username or password, please try again');
$this->set('title_for_layout','Error - Login');
}
}
So, I want that login
will be handled in users/login
, for both of users. My code is a little complex, I know. Actually, my AdminsController
's index
page contains the login form
, which submits to users/login
.
I mean, login
logic should be handled in users/login
, but login page(login form)
can be different for both of users, only important is that those forms
should submit to users/login
.
Now, with these configurations, Students
cant access editProfile
, and Admins
cant access anything
in the Admin Panel
.
I think my problem is with redirecting
after successful login. That's why I used return
before $this->redirect($this->Auth->redirect())
in the login function.
So, where is the problem ? What should I do ?
Please help me.
Thanks.
Upvotes: 0
Views: 555
Reputation: 481
Modify this line in core.php :
Configure::write('Routing.prefixes', array('admin','student'));
Add following line in beforeFilter function in app controller :
if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
AuthComponent::$sessionKey = 'Auth.Admin';
$this->Auth->loginAction = array('plugin' => false, 'controller' => 'users', 'action' => 'login','admin'=>true);
$this->Auth->logoutRedirect = array('plugin' => false, 'controller' => 'admin', 'action' => 'dashboard');
} else {
AuthComponent::$sessionKey = 'Auth.Front';
$this->Auth->loginAction = array('plugin' => false, 'controller' => 'users', 'action' => 'login',$this->request->prefix=>false);
$this->Auth->logoutRedirect = array('plugin' => false, 'controller' => 'users', 'action' => 'dashboard');
}
To get session for admin you use like that
$this->Session->read('Auth.Admin');
And get front (student) session as
$this->Session->read('Auth.Front');
Upvotes: 1