2012-07-06 54 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',因为此方法可以公开访问。希望它能为你工作。