我有以下isAuthorized()在我的學生控制器功能:CakePHP的驗證組件 - isAuthorized()函數問題
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;
}
基本上有涵蓋在這個方法中四種狀態:
- 用戶處於活動狀態並且尚未完成某個級別=已授權
- 用戶未付款=未授權
- 用戶已完成級別並且必須再次付款=不是授權
- 用戶帳戶是不活躍
我遇到的問題是,我有我的頭一個登錄表單,我可以從任何控制器登錄。如果我從學生控制器以外的控制器登錄,則不會調用isAuthorized()方法,即使用戶不應該也可以登錄。
任何想法?
編輯:檢查isAuthorized()方法的API的定義之後,我想,當被要求從學生控制器操作的方法只調用。那麼我還能在哪裏實現這個邏輯呢?謝謝
你是否在使用'beforeFilter()'? –
耶是在beforeFilter()中設置Auth組件,但不是這個邏輯。我儘管當用戶嘗試登錄時調用了isAuthorized()方法。 – AlexBrand
您在每個控制器中都有登錄操作? –