2011-11-21 66 views
0

我如何使用以下內容。 我有列在我的表wf_users:wf_username, wf_password現在在我的形式我不想要任何人瑟我的表結構,因此我在表單中使用CakePHP 2.0 AuthComponent使用自己的列名稱

$this->Form->input("username")­;
而不是
$this->Form->input("wf_username")­;
我的帖子是這樣

User=>array('username', 'password');

現在我需要他們改變了我的指定的列或想要做的伎倆在允許的地區登陸,因爲它try's與錯列不取(用)。

我的AppController:

class AppController extends Controller { 

    public $viewClass = 'Theme'; 

    public $theme; 

    public $components = array(
          'Auth'=> array(
           'loginRedirect'=>array('controller'=>'users', 'action'=>'dashboard'), 
           'logoutRedirect'=>array('controller'=>'users', 'action'=>'dashboard'), 
           'authError'=>'yout can´t access the page!', 
           'authorise'=>array('Controller'), 
           #'authenticate' => array(
           # 'Form' => array(
           #  'fields' => array(
           #   'wf_auth_user_username' => 'username', 
           #   'wf_auth_user_password' => 'password' 
           #  ) 
           # ) 
           #) 
          ), 
          'Session' 
         ); 

    //Before mainlayout 
    public function beforeFilter() { 
     parent::beforeFilter(); 
     $this->theme = 'SM'; 
     $this->Auth->fields = array('username'=>'wf_auth_user_username','password'=>'wf_auth_user_password'); 
     //Configure AuthComponent 
     $this->Auth->allow('*'); 
     //Logged in user 
     $this->set('isauth', 'loggedout'); //$this->userpanelAuth()); 
    } 

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

    protected function userpanelAuth() { 
     if($this->isAuthorized()) { 
      return 'loggedin'; 
     }else{ 
      return 'loggedout'; 
     } 
    } 

} 

,這是我的方式:

      <div id="display-panel"> 
           <? 
echo $this->Session->flash(); 

             echo $this->Form->create('User', array('action' => 'login')); 
             echo $this->Form->input(
              'username', 
               array('label'=>false, 'div'=>false,'placeholder'=>'Benutzername','')); 
             echo $this->Form->input(
              'password', 
               array('label'=>false, 'div'=>false,'placeholder'=>'Passwort')); 

             echo $this->Form->end(array('label'=>'login','div'=>false)); 
           ?> 
          </div> 

回答

0

當您設置的所有身份驗證設置(通常在beforeFilter回調)剛剛成立的fields屬性

$this->Auth->fields = array('username' => 'wf_username', 'password' => 'wf_password'); 
+0

it's不工作,每次我得到這個,當我試圖 錯誤:SQLSTATE [42S22]:列未發現:在1054未知列'User.username「where子句」 ,因爲它再次嘗試它取用發佈到控制器的用戶名,而不是用我的wf_username ... 我在AppController的方法「beforeFilter」下設置它你如何解釋 – webfacer

+0

在用戶控制器中,如果你有一個beforeFilter方法,你需要確保在那裏有'parent :: __ beforeFilter()',否則AppController方法將不會運行 – Stoosh

+0

道歉,看起來像Cake已經改變了2.0版的Auth語法,請參閱下面的brism的回答 – Stoosh

3

Auth組件允許您在setti時指定不同的字段增加配置。在這種情況下,您使用Form身份驗證處理程序,該處理程序具有fields密鑰。

<?php 
// Pass settings in $components array 
public $components = array(
    'Auth'=> array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'email') 
      ) 
     ) 
    ) 
); 

2.0 Authentication docs

+0

我也試過這個問題。 可以我用這個這樣 公共$組分=陣列( '驗證'=>數組( '認證'=>數組( '窗體'=>數組( '字段'=>數組('wf_username '=>'用戶名') ) ) ) ); – webfacer

+0

應該是'array('fields'=> array('username'=>'wf_username'))' – Stoosh

+0

是的,它們與上面貼出的代碼相反,就像Stoosh的評論。 '用戶名'和'密碼'是指向你想要的自定義名稱的鍵。您可以在這裏看到它在FormAuthenticate類中的使用方式:http://api20.cakephp.org/view_source/form-authenticate#line-48 – brism

0

您不需要使用那些列名稱wf_。堅持常規,不要浪費時間。

+0

這是一個不顯示錶結構的例子,如果表有一列「name」而不是「username」 – webfacer

相關問題