ASK CakePHP - 使用密码字段

时间:2014-06-27 03:05:26

标签: php mysql cakephp

我在这里搜索了很多话题,但我无法解决我的问题。请为我检查一下。

我创建了注册页面,当我创建密码字段时......

我有 users_controller.php ,如:

class UsersController extends AppController
{

    var $name = "Users";
    var $helpers = array('Paginator','Html');
    var $paginate = array();

    //Doi tuong component de thuc thi thao tac login
    public $components = array
    (
        'Auth' => array
        (
            'authorize' => 'controller',
            'loginRedirect' => array
            (
                'admin' => FALSE,
                'controller' => 'users',
                'action' => 'dashboard' 
            ),
            'loginError' => 'Invalid account',
            'authError' => 'You don\'t have permission'
        ),
    'Session'   
    );

    //Ham loc cac user truoc khi truy cap trang
    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('add');
        $this->Auth->allow('viewuserall');
    }

    //Ham them moi user
    public function add()
    {
        $this->layout = 'TDCake';

        $this->User->set($this->data);
        if($this->User->valid_user() == TRUE)
        {
            if(!empty($this->data))
            {
                $this->User->create();
                if($this->User->save($this->data))
                    {
                        $this->Session->setFlash('User has been created!');
                        $this->redirect(array('action'=>'login'));
                    }
                    else
                    {
                        $this->Session->setFlash('Please correct the errors');
                    }
            };
        }
        else
        {
            $this->Session->setFlash("Your data is NOT available");
        }
    }

    //Ham login cho user
    public function login()
    {
        $this->layout = 'TDCake';
        if
        (
            !empty($this->data) &&
            !empty($this->Auth->data['User']['username'])&&
            !empty($this->Auth->data['User']['password'])
        )
        {
            $user = $this->User->find
            (
                'first',array
                (
                    'conditions'=>array
                        (
                            'User.email'=>$this->Auth->data['User']['username'],
                            'User.password'=>$this->Auth->data['User']['password']
                        ),
                    'recursive' => -1
                )
            );
            if(!empty($user) && $this->Auth->login($user))
            {
                if($this->Auth->autoRedirect)
                {
                    $this->redirect($this->Auth->redirect());
                }
            }
            else
            {
                $this->Session->setFlash
                (
                    $this->Auth->loginError,
                    $this->Auth->flashElement,
                    array(),'auth'
                );
            }
        }
    }

    //Ham logout cho user
    public function logout()
    {
        $this->redirect($this->Auth->logout());
    }   

    //Ham gi cha biet, de do tinh sau =))
    public function dashboard()
    {
        $this->layout = 'TDCake';

    }

    //Ham view cac user khong dieu kien trong table users
    function viewuserall()
    {
        $this->layout = 'TDCake';
        $this->paginate=array
        (
            'limit' => 10,
            'order' => array('id' => 'asc'),
        );
        $data = $this->paginate("User");
        $this->set("data",$data);
    }

}
模型中的

User.php 是:

class User extends AppModel
{
    var $name = "User";
    var $validate = array();

    function validate_passwords()
    {
        if($this->data[$this->alias]['pass'] == $this->data[$this->alias]['rpass'])
        { 
            return $this->data[$this->alias]['pass'] = $this->data['User']['password'];
        }
        else return FALSE;
    }

    function valid_user()
    {
        $this->validate = array
        (
            //Kiem tra username truoc khi add
            'username' => array
            (
                'rule01_notEmpty' => array
                (
                    'rule' => 'notEmpty',
                    'message' => 'You must enter your Username !'
                ),
                'rule02_max16' => array
                (
                    'rule' => array('maxLength', 20), 
                    'message' => 'Your Username must be less than 20 chars !'
                ),
                'rule03_exists' => array
                (
                    'rule' => 'isUnique', 
                    'message' => 'Your Username have already existed !'
                )
            ),
            //Kiem tra email truoc khi add
            'email' => array
            (
                'rule01_notEmpty' => array
                (
                    'rule' => 'notEmpty',
                    'message' => 'You must enter your Email !'
                ),
                'rule02_exists' => array
                (
                    'rule' => 'isUnique', 
                    'message' => 'Your Email have already existed !'
                ),
                'rule03_emailtype' => array
                (
                    'rule' => 'email', 
                    'message' => 'You didn\'t type a email !'
                )                   
            ),
            //Kiem tra password truoc khi add
            'pass' => array
            (
                'length' => array
                (
                    'rule'      => array('between', 6, 20),
                    'message'   => 'Your password must be between 8 and 40 characters.',
                ),
            ),
            'rpass' => array
            (
                'length' => array
                (
                    'rule'      => array('between', 6, 20),
                    'message'   => 'Your password must be between 8 and 40 characters.',
                ),
                'compare' => array
                (
                    'rule'    => 'validate_passwords',
                    'message' => 'The passwords you entered do not match.',
                )
            )
        );//End this->validate=array


        if($this->validates($this->validate==TRUE))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }//End function valid_user

}

add.ctp

    echo $this->Session->flash('auth');
echo $this->Form->create();

echo $this->Form->input('username', array('label' => ('Username')));
echo $this->Form->input('email', array('label' => ('Email')));
echo $this->Form->input('pass', array('label' => ('Password'),'type' => 'password', 'value' => ''));
echo $this->Form->input('rpass', array('label' => ('Repeat Password'), 'type' => 'password', 'value' => ''));
echo $this->Form->input('firstname', array('label' =>('Firstname')));
echo $this->Form->input('lastname', array('label' =>('Lastname')));
echo $this->Form->input('dob', array('label' =>('DOB'),'type' => 'date'));


echo $this->Form->end('Register');

说明: 因此,在这种情况下,我可以验证2个密码字段(空,不等于......),但它不能插入数据库。这意味着它将当前数据插入到DB中但DB中的密码列为EMPTY。在数据库中,我的密码列名称"密码"还

在另一种情况下,我更改名称"传递"进入"密码"对于

echo $this->Form->input('pass', array(

当然,我改变了与...相关的任何地方。

在这种情况下,可以插入密码但无法验证任何内容。

我对此感到困惑......我不知道自己错了是什么......有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

我不确定你为什么要在验证函数中进行分配:

   return $this->data[$this->alias]['pass'] = $this->data['User']['password'];

即使你正在做作业,也应该是:

   return $this->data['User']['password'] = $this->data[$this->alias]['pass'];

意识到字段“密码”是从$this->data获取具有该信息的值,而不是相反。

另外。最好(在清晰度方面),将这些代码分成两行。

$this->data['User']['password'] = $this->data[$this->alias]['pass'];
return $this->data['User']['password'];

如果你没有明确指定,那么你应该在字段中为你的字段命名为“密码”,如果它是在数据库 AND 中调用它。

您的添加功能并未执行上述操作,此外,作为最佳做法,您应该对密码进行哈希处理。

请参阅CakePHP关于教程和示例的书。

花一些时间来完成所有的片段和建议。不要忘记标准。 :)