2010-03-30 8 views

risposta

12

Viene utilizzato quando un utente seleziona la casella "Ricordami" sul tuo sito. Un token viene generato per l'utente e memorizzato nella tabella user_tokens.

Se guardate la classe Kohana_Auth_ORM nella funzione _login, si può vedere come viene creato:

if ($remember === TRUE) 
    { 
     // Create a new autologin token 
     $token = ORM::factory('user_token'); 

     // Set token data 
     $token->user_id = $user->id; 
     $token->expires = time() + $this->config['lifetime']; 
     $token->save(); 

     // Set the autologin cookie 
     cookie::set('authautologin', $token->token, $this->config['lifetime']); 
    } 

E 'utilizzato dal AUTO_LOGIN() funzione anche nella classe Kohana_Auth_ORM:

/** 
* Logs a user in, based on the authautologin cookie. 
* 
* @return boolean 
*/ 
public function auto_login() 
{ 
    if ($token = cookie::get('authautologin')) 
    { 
     // Load the token and user 
     $token = ORM::factory('user_token', array('token' => $token)); 

     if ($token->loaded() AND $token->user->loaded()) 
     { 
      if ($token->user_agent === sha1(Request::$user_agent)) 
      { 
       // Save the token to create a new unique token 
       $token->save(); 

       // Set the new token 
       cookie::set('authautologin', $token->token, $token->expires - time()); 

       // Complete the login with the found data 
       $this->complete_login($token->user); 

       // Automatic login was successful 
       return TRUE; 
      } 

      // Token is invalid 
      $token->delete(); 
     } 
    } 

    return FALSE; 
} 

Spetta a te utilizzare correttamente questa funzionalità all'interno del controller di autorizzazione. Sono relativamente nuovo per Kohana, ma effettuo un semplice controllo per reindirizzare un utente se vanno al form di login e sono già registrati nel oppure possono effettuare il login automatico:

if (Auth::instance()->logged_in() || Auth::instance()->auto_login()) 
    Request::instance()->redirect('auth/'); 

Il codice per il modulo Auth isn' t troppo difficile da capire. Se sei nuovo a Kohana, è un buon punto di partenza per vedere come funziona il modulo ORM.

+0

Ciao Brian Riehman, Grazie per la domanda. E sì, sono un principiante per il framework Kohana. – Asif