2012-05-24 138 views
0

我試圖創建一個簡單的登錄頁面。我希望在該頁面上進行驗證,這樣當用戶單擊登錄時,站點會檢查激活的用戶數據庫中是否設置爲1,如果不是,則無法登錄。我對cakephp仍然很陌生,並且正在努力快速提取,所以我很抱歉如果這是一個簡單的初學者問題。CakePHP 2.0帳戶驗證

這裏是我的用戶模型

public $checkActive = array(
    'activated'=>array(
      'rule'=>array('equalTo', '0'), 
      'message'=>'The account must be activated, please check your email.' 
     )); 

這裏的驗證是在我usersController登錄功能

public function login() { 

    $this->set('title_for_layout', 'Individual Registration'); 
    $this->set('stylesheet_used', 'style'); 
    $this->set('image_used', 'eBOXLogo.jpg'); 


    if ($this->request->is('post')){ 
    if ($this->request->data['User']['password'] == 'qazwsx'){ 
    if ($this->Auth->login()){ 
    if (0 === $this->User->find('count',array('conditions'=>array('enabled'=>1,'login'=> $username)))) { 
     $this->Session->setFlash('Sorry, your account is not validated yet.'); 
    } 

     $this->Auth->user('id'); 
     $this->redirect($this->Auth->redirect('eboxs/home')); 
     } 
    } 
    else { 

     $this->Session->setFlash('Username or password is incorrect'); 
    } 
    }else{ 
    $this->Session->setFlash('Welcome, please login'); 
    } 


} 

這裏是在usersController

public function beforeLogin(){ 

    if(isset($this->data['User']['password'])){ 
     $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']); 
     } 
     return true; 
    } 

App我beforeLogin功能控制器

class AppController extends Controller { 

    public $components = array(
     'DebugKit.Toolbar', 
     'Session', 
     'Auth'=>array(
      'loginRedirect'=>array('controller'=>'users', 'action'=>'login'), 
      'logoutRedirect'=>array('controller'=>'users', 'action'=>'login'), 
      'authError'=>"You can't access this page", 
      'authorize'=>array('Controller') 
     ) 
    ); 

    public function isAuthorized($user){ 
     return true; 
    } 

    public function beforeFilter(){ 
    $this->Auth->allow('index','view'); 
    $this->set('logged_in', $this->Auth->loggedIn()); 
    $this->set('current_user',$this->Auth->user()); 

    } 

我意識到在我的控制器中沒有調用驗證,但與我的其他驗證,如用戶名是唯一的,我沒有必要調用它。

總之現在任何人都可以登錄到我的頁面,我試圖讓它只有那些在users表中的激活字段中有1的人才能登錄。

+1

你爲什麼手動檢查密碼?請發佈您的Auth組件設置(應該在您的AppController中) – Dunhamzzz

+0

發佈應用程序控制器 – user1393064

回答

1

一個選擇會是這樣登錄後立即檢查帳戶驗證:

<?php 
if ($this->request->is('post')){ 
if ($this->request->data['User']['password'] == 'qazwsx'){ 
if ($this->Auth->login()) { 

    // login ok, but check if activated 
    $username = $this->request->data['User']['username']; 
    if (0 === $this->User->find('count',array('conditions'=>array('activated'=>1,'username'=> $username)))) { 
     $this->Session->setFlash('Sorry, your account is not validated yet.'); 
     $this->redirec($this->referer()); 
    } 

    $this->Auth->user('id'); 
    $this->redirect($this->Auth->redirect('eboxs/home')); 
    } 
} 
+0

此代碼阻止所有人登錄,包括那些激活= 1 – user1393064

+0

也沒有setflash出現,它提出了一個數據庫錯誤,說我們錯過了錯誤視圖 – user1393064

+0

pdo.ctp的視圖好,更新了代碼從'真'到'1','激活'字段,並將referer()重定向到位。 –

1

添加scope選項,您的身份驗證設置:

'Auth'=>array(
     'loginRedirect'=>array('controller'=>'users', 'action'=>'login'), 
     'logoutRedirect'=>array('controller'=>'users', 'action'=>'login'), 
     'authError'=>"You can't access this page", 
     'authorize'=>array('Controller'), 
     'scope' => array('User.activated' => 1) 
    ) 

這將防止登錄用戶,如果他們沒有User.activated = 1

另外,看看你的auth setup and re-read the manual page for CakePHP 2.0,你的配置看起來像1.3。應該不需要自己檢查密碼,並且您絕對不需要使用beforeLogin方法進行這樣簡單的設置。

+0

我將其更改爲上述代碼並且沒有更改 – user1393064

+0

這會阻止日誌記錄,但不包含任何線索,因爲它不會記錄此代碼 –