2012-12-03 132 views
1

默認登錄過程需要電子郵件,而不是用戶名。CakeDC用戶以用戶名登錄

我已經添加了這AppController.phpbeforeFilter(),這是在CakePHP's Docs Authentication還提到:

$this->Auth->fields = array(
    'username' => 'username', 
    'password' => 'password' 
); 

但不知何故,它不允許用戶與他們的用戶名登錄。關於如何改變這種情況的任何想法?

AppController的

App::uses('Controller', 'Controller'); 
class AppController extends Controller { 
var $components = array(
    'Session', 
    'RequestHandler', 
    'Security' 
); 
var $helpers = array('Form', 'Html', 'Session', 'Js'); 

public function beforeFilter() { 
    $this->Auth->authorize = 'Controller'; 
    $this->Auth->fields = array('username' => 'username', 'password' => 'password'); 
    $this->Auth->loginAction = array('plugin' => 'users', 'controller' => 'users', 'action' => 'login', 'admin' => false); 
    $this->Auth->loginRedirect = '/'; 
    $this->Auth->logoutRedirect = '/'; 
    $this->Auth->authError = __('Sorry, but you need to login to access this location.', true); 
    $this->Auth->loginError = __('Invalid e-mail/password combination. Please try again', true); 
    $this->Auth->autoRedirect = true; 
    $this->Auth->userModel = 'User'; 
    $this->Auth->userScope = array('User.active' => 1); 
    if ($this->Auth->user()) { 
     $this->set('userData', $this->Auth->user()); 
     $this->set('isAuthorized', ($this->Auth->user('id') != '')); 
    } 
} 

/View/Users/login.ctp

這是一樣的,在插件的文件夾中找到默認login.ctp。我只是將字段電子郵件更改爲用戶名。

現在,這裏有一些有趣的事情。無論我將哪些代碼放入此文件,CakePHP都會從插件登錄視圖中提取內容,我創建的視圖將被忽略。

調試

當調用調試器:

Debugger::dump($this->Auth); 

這說明一切,我設置的值。但它仍然不接受用戶名。我仍然可以通過電子郵件登錄,所以這不是我插入錯誤的憑據。它只是在等待電子郵件/密碼,而不是用戶名/密碼。

+0

未經測試/不知道這是否是正確的方法,但蛋糕DC插件將在'用戶\控制器\ UsersController.php'圍繞線144的價值觀 - 你可以嘗試調整它們那裏。 – Ross

+0

事情是,我不想改變核心,我不認爲這是覆蓋設置的正確方法。但如果沒有其他選擇,我必須遵循這條路線。 – rlcabral

+1

需要注意的是,CakeDC用戶插件使用該電子郵件進行註冊,忘記密碼等等等。 – Dave

回答

3

嘗試在AppController中配置它在$組件:

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'username') 
      ) 
     )    
    ) 
} 

我對逆方式問題,我想,以驗證用戶使用他們的郵件,而不是用戶名,並使用上述配置與「 username'=>'email'爲我工作。

編輯:

public $components = array(
    'Acl', 
    'Auth' => array(
     'authorize' => array(
      'Actions' => array('actionPath' => 'controllers') 
     ), 
     'authenticate' => array(
      'Form' => array(
       'fields' => array('username' => 'email') 
      ) 
     )    
    ), 
    'Session' 
); 

public function beforeFilter() { 
    //User settings 
    $this->activeUserId = $this->Auth->user; 
    $this->set('activeuserphoto', $this->Auth->user('photo')); 
    $this->set('activeusername', $this->Auth->user('username')); 

    //Configure AuthComponent 
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); 
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login'); 
    $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'dashboard'); 
} 
+0

Nop,沒有工作。看起來插件的默認設置是優先的,無論我設置了什麼,它都會隨着插件的需要而變化。 – rlcabral

+1

您可以添加您的登錄視圖代碼和完整的AppController代碼嗎?它應該工作! –

+0

已更新問題 – rlcabral

相關問題