I have the following isAuthorized() function in my students controller:
function isAuthorized() {
$studentId = $this->Auth->user('id');
$studentEmail = $this->Auth->user('email');
if ($this->Auth->user('active') == 1 && $this->Auth->user('level_complete') != 1) {
$this->Auth->loginRedirect = '/classrooms/view';
return true;
} elseif (!$this->Student->hasPayed($studentId)) {
$this->Session->write('Payment.student_id', $studentId);
$this->Session->write('Payment.student_email', $studentEmail);
$this->Session->write('Payment.examScore', $this->Student->getPlacementScore($studentId));
$this->Auth->logout();
$this->redirect(array('controller'=>'payments', 'action'=>'pay'));
} elseif ($this->Auth->user('level_complete') == 1) {
$this->Session->write('Payment.student_id', $studentId);
$this->Session->write('Payment.student_email', $studentEmail);
$this->Auth->logout();
$this->redirect(array('controller' => 'payments', 'action' => 'repay'));
} else {
$this->Auth->logout();
$this->redirect(array('controller' => 'students', 'action' => 'disabled'));
}
return false;
}
Basically, there are four possible states covered in this method:
- The user is active and has not completed a level = Authorized
- The user has not payed = Not Authorized
- The user has completed a level and has to pay again = Not Authorized
- The user account is not active
The problem I am having is that I have a login form in my header, and I can login from any controller. If I login from a controller other than the Students Controller, the isAuthorized() method is not called, and the user can login even though he shouldn't be able to.
Any ideas?
Edit: After checking the API's definition of the isAuthorized() method, I think that the method is only called when actions from the students controller are requested. Where else could I implement this logic then? Thanks
beforeFilter()at all? - Jason McCreary