Cakephp 2.1-使用AuthComponent登录系统

时间:2012-07-06 18:48:46

标签: authentication logging authorization cakephp-2.0

我现在已经疯了几天了。我在youtube上使用了文档和视频。还有一些关于该特定问题的教程。但是我看不到有什么不对劲。我使用cakephp在Web系统中找到了一个登录系统。  我的用户表如下:

    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;

其中Status是系统中不同部分的授权级别。注释中描述了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 ......似乎没有真正起作用。 任何人??

1 个答案:

答案 0 :(得分:0)

如果您只想将auth组件用于登录系统,则无需定义isAuthorized方法。没有这种方法尝试一次。

您不需要在特定控制器中的allow()方法中提供logout方法,而是应该将“login”定义为该方法,因为此方法可以公开访问。希望它能为你效劳。

相关问题