2012-09-06 2 views
1

자동 로그인 구성 요소와 함께 CakePHP 2x를 사용하고 있습니다. 문제는, 나는 그 물건을 쓸 수 있지만, 그것을 읽고 권한을 부여하는 방법을 잘 모르겠다. 사용자가 페이지에 도착하면 브라우저에 여전히 쿠키가 있지만 권한을 부여하는 방법은 무엇입니까? CakePHP 2x에서 자동 로그인 구성 요소를 사용하여 자동 로그인 쿠키가있는 사용자를 자동 리디렉션하는 방법은 무엇입니까?

내 로그인 스크립트 :

public function login() { 
     if ($this->Auth->user('id')) { 
      $this->redirect(array('action' => 'dashboard')); 
     } 
     if($this->request->data['User']['auto_login']): 
     $this->AutoLogin->write($this->request->data['User']['username'], 
       $this->request->data['User']['password']); 
     endif; 

     if ($this->request->is('post')) { 
      if ($this->Auth->login()) { 
       //$this->redirect(array('controller' => 'users', 'action' => 'dashboard')); 
       return $this->redirect($this->Auth->redirect()); 
      } 
      else 
      { 
       $this->Session->setFlash(__('Username or Password is incorrect'), 'default', array(), 'auth'); 
      } 
     } 

답변

1

이해야 뭔가 같은 :

public function login() 
{  
    if ($this->request->is('post')) 
    { 
     if ($this->Auth->login()) 
     {    
      if ($this->request->data['User']['persist'] == '1') 
      { 
       $cookie = array(); 
       $cookie['username'] = $this->data['User']['USER_LOGINNAME']; 
       $cookie['password'] = $this->data['User']['USER_PASSWORD']; 
       $this->Cookie->write('Auth.User', $cookie, true, '+4 weeks'); 
      } 
      $this->redirect($this->Auth->redirect()); 
     } 
     else 
     { 
      $this->Session->setFlash('Your username or password was incorrect.', 'default/flash-error'); 
     } 
    } 
    else 
    { 
     $user = $this->Auth->user(); 
     if (empty($user)) 
     { 
      $cookie = $this->Cookie->read('Auth.User');        
      if (!is_null($cookie)) 
      { 
       $user = $this->User->find('first', array('conditions' => array('USER_LOGINNAME' => $cookie['username'], 'USER_PASSWORD' => AuthComponent::password($cookie['password'])))); 
       if ($this->Auth->login($user['User'])) 
       { 
        $this->Session->delete('Message.auth'); 
        $this->redirect($this->Auth->redirect()); 
       } 
       else 
       { 
        $this->Cookie->delete('Auth.User'); 
       } 
      } 
     } 
     else 
     { 
      $this->redirect($this->Auth->redirect()); 
     } 
    } 
} 

이 당신에게 동일한 작업을 달성하는 방법의 아이디어를 제공은, 그러나, 나는에 따라 양식 필드를 사용 내 DB 구조.

DB 구조에 따라 양식 필드를 친절하게 변경하십시오.

+0

와아! 그것은 엄청난 변화입니다. 내가 그것을 밖으로 시도하자. 나와 함께 견뎌주십시오. – Karma

+0

코드가 어딘가에 (+1) 나왔습니다. 그러나이 질문을 끝내기 전에 마지막 쿼리가 하나 있는데, 필자는 Facebook 플러그인을 사용하여 FB 연결 만하고 Session_start라는 줄이 있습니다. 내 core.php에서 세션 처리기로 cake를 사용하고 있습니다. autologin이 cakephp.cookie를 저장하고 있고 phpsessid가 있다는 것을 보았습니다. 브라우저가 죽으면 쿠키가 삭제됩니다. 그것에 대한 아이디어가 있습니까? – Karma

+0

이 링크를 사용해보십시오 : http://book.cakephp.org/2.0/ko/core-libraries/components/cookie.html#controller-setup –