奇怪的CakePHP 2.0身份验证登录问题

时间:2012-01-21 00:22:13

标签: login authentication cakephp-2.0

好的,所以我在CakePHP 1.3中遇到了一个非常有趣的问题,因为即使我使用了正确的信息来尝试登录,它也行不通。我现在将相同的应用程序升级到Cakephp 2.0,我的问题非常不同。基本上现在,无论我在登录时输入什么信息,它都会登录。即使数据库是空的。不知道为什么会这样......

这是我的代码:

查看:

<div id="login">
<p>Please log in! <a id="register" href="register" alt="Register">Register</a></p>
<hr class="login"/>
<?php    
    echo $this->Session->flash('auth');    
    echo $this->Form->create('User');    
    echo $this->Form->input('username');
    echo $this->Form->input('password');
    echo "<hr class=\"login\"/>";
    echo $this->Form->end('Login');
    echo $this->Session->flash('flash_registration');
    echo "<pre>"; print_r($this->request->data); echo "</pre>";
    echo $this->Html->link('Log-Out', 'logout');
?>

</div>

型号:

<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {
var $name = 'User';
var $validate = array(
    'name' => array(
        'custom_rule' => array(
            'rule' => '/^[A-Za-z\s]*$/i',
            'message' => 'Please enter an acceptable name'
            ),
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'This field is required'
        )
    ),
    'dob' => array(
        'rule' => array('date', 'ymd'),
        'message' => 'Enter a valid date',
    ),
    'phone' => array(
        'numbers' => array(
            'rule' => 'numeric',
            'message' => 'Numbers only, no dashes or spaces'
        ),
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'This field is required'
        )
    ),
    'username' => array(
        'alphaNumeric' => array(
            'rule' => 'alphaNumeric',
            'message' => 'Letters and numbers only'
        ),
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'This field is required'
        )
    ),
    'e-mail' => array(
        'email' => array(
            'rule' => 'email',
            'message' => 'Please enter a valid e-mail address'
        ),
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'This field is required'
        )
    ),
    'password_enter' => array(
        'length' => array(
            'rule' => array('between', 8, 16),
            'message' => 'Password must be between 8 and 16 characters'
        ),
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'This field is required'
        )
    ),
    'password_confirm' => array( 
        'identicalFieldValues' => array( 
            'rule' => array('identicalFieldValues', 'password_enter'), 
            'message' => 'Passwords do not match'
        ),
        'length' => array(
            'rule' => array('between', 8, 16),
            'message' => 'Password must be between 8 and 16 characters'
        ),
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'This field is required'
        )
    )
);

function identicalFieldValues( $field=array(), $compare_field=null ){
    foreach( $field as $key => $value ){ 
        $v1 = $value; 
        $v2 = $this->data[$this->name][ $compare_field ];
        if($v1 !== $v2) { 
            return FALSE; 
        } else { 
            return TRUE;
        }
    } 
}
function beforeValidate(){ 

    $this->data['User']['dob'] = $this->data['User']['dob'];

    return true;
}
function beforeSave(){

    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password_enter']);
    $this->data['User']['activated'] = FALSE;

    return TRUE;
}

}

?>

控制器:

<?php

class UsersController extends AppController {

var $name = 'Users';
var $uses = array("User");
var $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
        )
    );

var $helpers = array('Form', 'Session', 'Html');

function beforeFilter(){
    // Basic setup
    $this->Auth->authenticate = array('Form');
    $this->Auth->allow('register', 'activate');

}

function index() {



}

function login() {
    $this->Auth->login($this->request->data);
    $this->set('title_for_layout', "Welcome to Sound-On.com!");
    $this->layout = 'user_functions';
    if ($this->Auth->user()) {
        echo "Logged in!";
    } else {
        echo "Not logged in!";
    }
}

function logout() {

    $this->redirect($this->Auth->logout());

}

function register(){

    $this->set('title_for_layout', "Register Here!");
    $this->layout = 'user_functions';
    $date = date('Y');

    if (!empty($this->data)) {
        $user_check = $this->User->find('first', array('conditions' => array('username' => $this->data['User']['username'])));
        $email_check = $this->User->find('first', array('conditions' => array('e-mail' => $this->data['User']['e-mail'])));
        if (empty($user_check)) {
            if(empty($email_check)){
                if ($this->User->save($this->data)) {
                    $uuid_string = $this->data['User']['activation_hash'];
                    $email = <<<EOT
                    <html>
                        <head>
                            <title>Welcome to Sound-On.com!</title>
                        </head>
                        <body>
                            <p>
                                <h1>Welcome to Sound-on.com!</h1>

                                <p>You have successfully registered! To activate your account and start sounding on, please click <a href="http://www.sound-on.com/activate?uid=$uuid_string">Here</a>! <br/>If the link is not clickable, please copy and paste the link below into your browser address bar.</p>

                                http://www.sound-on.com/activate?uid=$uuid_string

                                <p style="">Thank you for registering!</p>
                                                <p>Your friendly Sound-On registration robot</p>

                                <p>If you did not register or wish to remove your account, please click <a href="http://www.sound-on.com/not-me?uid=$uuid_string">here</a>.</p>

                                <p style="font-size:8pt;color:#707070">&copy; Copyright $date Sound-on.com. All rights Reserved.</p>
                            </p>
                        </body
                    </html>
EOT;
                    $to = $this->data['User']['e-mail'];
                    $subject = 'Welcome to Sound-On.com!';
                    $headers = "MIME-Version: 1.0" . "\r\n";
                    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
                    $headers .= 'From: registration@Sound-On.com';

                    if (mail($to, $subject, $email, $headers)) {
                        $this->redirect('/');
                    }
                } else {
                    //$this->Session->setFlash('<p class="register_flash">Something went wrong. Please try again.</p>', 'flash_registration');
                    //$this->flash('', '/');
                }
            } else {

                //email exists

            }
        } else {

            //username exists

        }
    }   
}

function activate(){

    $this->set('title_for_layout', "Register Here!");
    $this->layout = 'user_functions';
    if (!empty($_GET)) {
        $activate = $this->User->updateAll(array('activated' => 1), array('activation_hash' => $_GET['uuid']));
        if ($activate) {
            $this->set('message', '<p id="activation_message">Your account has been successfully activated! Please click <a href="/">here</a> to proceed to login!</p>');
        }
    }

}
}





?>

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果您将数据发送到Auth->login()功能,它将使用数据登录。

http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in

你需要使用这样的东西。

public function login() {
    if ($this->request->is('post')) {
        if (!$this->Auth->login()) {
            $this->Session->setFlash('Your username or password was incorrect.');
        } else {
            $this->Session->setFlash('You are now logged in.');
            //redirect
        }
    }
}
相关问题