2012-07-06 92 views
2

我現在瘋了幾天。我在YouTube上使用了文檔和視頻。另外一些教程只針對特定問題。但是有什麼不對,我看不到。我使用cakephp在網絡系統中定義了一個登錄系統。 我的用戶表如下:Cakephp 2.1-使用AuthComponent的登錄系統

CREATE TABLE `usuarios` (
    `Id` INT(50) UNSIGNED NOT NULL AUTO_INCREMENT, 
    `grupo_id` INT(50) UNSIGNED NOT NULL, 
    `Status` VARCHAR(30) NOT NULL COMMENT 'Coordenador/Bolsista/Super', 
    `Nome` VARCHAR(50) NOT NULL, 
    `Login` VARCHAR(50) NOT NULL, 
    `Email` VARCHAR(50) NOT NULL, 
    `Senha` VARCHAR(50) NOT NULL, 
    `created` DATETIME NULL DEFAULT NULL, 
    `modified` DATETIME NULL DEFAULT NULL, 
    PRIMARY KEY (`Id`), 
    UNIQUE INDEX `Login` (`Login`), 
    UNIQUE INDEX `Nome` (`Nome`), 
    INDEX `FK_usuarios_grupos` (`grupo_id`), 
    CONSTRAINT `FK_usuarios_grupos` FOREIGN KEY (`grupo_id`) REFERENCES `grupos` (`Id`) ON UPDATE CASCADE 
) 
COLLATE='latin1_swedish_ci' 
ENGINE=InnoDB 
AUTO_INCREMENT=8; 

如果狀態爲授權的differente部分的電平OS系統。評論中描述了3個選項。

In my AppController, after some time, I coded: 

    class AppController extends Controller { 
    public $components = array('Session', 
           'Auth'=>array(
              'authenticate' => array(
          'Form' => array(
              'fields'=>array(
                  'username'=>'Login', 
                  'password'=>'Senha' 
                  ), 
            'userModel'=> 'Usuario' 
          ), 
          ), 
          'loginAction' =>array(
                'Controller' => 'Usuarios', 
                'action' => 'login' 
              ), 
       'loginRedirect'=>array('Controller'=>'Usuarios', 'action'=>'index'), 
       'logoutRedirect'=>array('Controller'=>'Usuarios', 'action'=>'index'), 
       'authError'=>"You can't access that page", 
       'authorize'=>'Controller', 
       'loginError'=> 'Login errado' 

      ) 
    ); 

    public function isAuthorized($usuario=null) { 
     //return parent::isAuthorized($usuario); 
     return true; 
    } 

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

的isAuthorized方法有兩種選擇,因爲我想的事情...主要的問題是,每次我試圖登錄我去AuthError消息的時間。無論是已註冊的(使用散列密碼)還是無效密碼。我不明白爲什麼會發生這種情況,但我懷疑這是isAutorized方法的問題。

我特異性控制器具有以下培訓相關方法:

public function login() { 
     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       return $this->redirect($this->Auth->redirect()); 
      } else { 
       $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); 
       $this->Session->setFlash('Username or password is incorrect'); 

      } 
     } 
    } 

    public function logout() { 
     $this->redirect($this->Auth->logout()); 
    } 
    public function beforeFilter() { 
     parent::beforeFilter(); 
     $this->Auth->allow('add_bolsista','logout'); 
    } 

    public function isAuthorized($usuario = null) { 
     if (($usuario['Status'] == 'Super') || ($usuario['Status'] == 'Coordenador') || ($usuario['Status'] == 'Bolsista')) { 
      return true; 
     } 
     if (in_array($this->action, array('edit', 'delete'))) { 
      if ($usuario['Id'] != $this->request->params['pass'][0]) { 
       return false; 
      } 
     } 
     return true; 
    } 

    public function isAuthorized($usuario = null) { 
    // Any registered user can access public functions 
    if (empty($this->request->params['Bolsista'])) { 
     return true; 
    } 

    // Only admins can access admin functions 
    if (isset($this->request->params['Super'])) { 
     return (bool)($usuario['Status'] === 'Super'); 
    } 

    // Default deny 
    return false; 
} 

兩個版本的isAuthorized的......沒有一個似乎真正發揮作用。 任何人?

回答

0

如果您只想爲登錄系統使用auth組件,則無需定義isAuthorized方法。嘗試一次沒有這種方法。

您不需要在您的特定控制器中的allow()方法中提供註銷方法,而應該在其中定義'login',因爲此方法可以公開訪問。希望它能爲你工作。