регистрация и авторизация в cakephp2.0 - PullRequest
0 голосов
/ 07 марта 2012

Добрый день!Сейчас я переношу свои коды с 1.3 на 2.0 CakePHP.И я просто хочу спросить, как я могу сделать этот код (от 1.3) до 2.0?Вот код:

function register() {
 if(!empty($this->data)) {
        // unset unrequired validation rules
        unset($this->User->validate['username']['check_user']);

        // validate & save data
        if($this->User->save($this->data)) {
            $this->data['User']['Password'] = md5($this->data['User']['Password']);         
            $this->User->save($this->data);
            // set Flash & redirect
            $this->Session->setFlash('You have successfully registered.','default',array('class'=>'flash_good'));
            $this->redirect(array('action'=>'login'));
        }
        else{
            //$this->Session->setFlash(__('The user could not be saved.' , true));
            //$this->redirect(array('action' => 'register'));
        }
    }
}

, а вот код моей попытки, который я попытался разрешить:

public function register() {
    if($this->request->is('post')) {
    //unset($this->User->validate['username']['check_user']);

        // validate & save data

        //$this->data['User']['Password'] = md5($this->data['User']['Password']);     
         $this->request->data('User.Password', $this->request->data('User.Password'));    
        // $this->User->save($this->data);
        // set Flash & redirect
        if($this->User->save($this->request->data)) {
            $this->Session->setFlash('You have successfully registered.','default',array('class'=>'flash_good'));
            $this->redirect(array('action'=>'login'));
        }
        else{
            //$this->Session->setFlash(__('The user could not be saved.' , true));
            //$this->redirect(array('action' => 'register'));
        }
    }
}

Этот код предназначен для входа в систему, сделанного в 1,3

function login() {
    //echo $_SESSION['User']['auth'];
    if(!isset($_SESSION['User']['id'])){
        if(!empty($this->data)) {
            if(($user = $this->User->validateLogin($this->data['User'])) == true) 
            { 
            //print_r(md5($this->data['User']['password']));
                $user = $this->User->find('first',array('conditions'=>array('Username'=>$this->data['User']['Username'],'Password'=>md5($this->data['User']['Password']))));
                //print_r ($user);
                if(!empty($user)){
                    $_SESSION['User']['id'] = $user['User']['id'];
                    $_SESSION['User']['name'] = $user['User']['Name'];
                    $_SESSION['User']['auth'] = $user['User']['auth'];
                    $this->redirect(array('controller'=>'ads','action'=>'index'));
                }else{
                    $this->Session->setFlash('Username/Password not match');
                    $this->redirect(array('action'=>'login'));
                }
            } 
        }
    }
        else{
        $this->Session->setFlash('Login First.');
        $this->redirect(array('controller'=>'ads','action'=>'index'));

    }
}

а вот мой код в 2.0, и он все еще не работает.

public function login() {

    if(!($this->Session->read('user_id'))){
        if($this->request->is('post')) {        
            //$user = $this->User->find('first',array('conditions'=>array('Username'=>$this->data['User']['Username'],'Password'=>md5($this->data['User']['Password']))));

                if(!empty($user)){
                    $this->Session->write('user_id',$user['User']['id']);
                    $this->Session->write('name',$user['User']['Name']);
                    //$this->Session->write('name',$user['User']['Name']);
                    $this->redirect(array('controller'=>'ads','action'=>'index'));
                }else{
                    $this->Session->setFlash('Username/Password not match');
                    $this->redirect(array('action'=>'login'));
                }

        } 
    }else{
        $this->redirect(array('controller'=>'ads','action'=>'index'));
    }
}//end login

Я надеюсь, что кто-то ответит на мой вопрос.Заранее спасибо.

1 Ответ

2 голосов
/ 07 марта 2012

Сначала я попробую запустить миграцию для существующего кода, и вы можете быть удивлены.вот ссылка на оболочку обновления:

http://book.cakephp.org/2.0/en/console-and-shells/upgrade-shell.html#upgrade-shell

Попробуйте сначала.

...