2013-12-14 10 views
7

Non riesco a far funzionare la sentinella. Continuo a ricevere questo errore: A hasher has not been provided for the user. Qualcuno sa cosa potrebbe accadere?Non è stato fornito un hasher per l'utente

The error log

Sono in esecuzione MAMP su OS X 10.9. Sto usando php 5.4.4 MCrypt è installato e abilitato. Questo errore si verifica quando si tenta di cancellare la password durante la creazione di un nuovo utente. Il nostro progetto utilizza il plugin sentinella laravel. Ecco il controllore:

<?php 

use Auth, BaseController, Form, Input, Redirect, Sentry, View; 

class AuthController extends BaseController { 

     public function register() 
     { 
      return View::make('Auth.register'); 
     } 

     public function handleRegister() 
     { 
       $validator = Validator::make(Input::all(), User::$rules); 

       if ($validator->passes()) { 
        //The registration has passed, create a user 

      $user = new User; 
        $user->first_name = Input::get('first_name'); 
        $user->last_name = Input::get('last_name'); 
        $user->email = Input::get('email'); 
        $user->password = Hash::make(Input::get('password')); 
        $user->activated = 1; 
        $user->save(); 

      //grabbing the Sentry model of the user so we can save it to the appropriate group 
      $sentryUser = Sentry::findUserByLogin($user->email); 

      if (Input::get('userType') == 'Promoter') 
      { 
       $group = 'Promoters'; 
      } 
      else 
      { 
       $group = 'Agents'; 
      } 

      // Find the group using the group id 
      $group = Sentry::findGroupByName($group); 

      // Assign the group to the user 
      $sentryUser->addGroup($group); 

        return Redirect::action('[email protected]')->with('message', 'Thanks for registering!'); 
       } else { 
       // validation has failed, display error messages 
        return Redirect::action('[email protected]')->with('message', 'The following errors occurred')->withErrors($validator)->withInput(); 

       } 
     } 

     /** 
     * Display the login page 
     * @return View 
     */ 
     public function login() 
     { 
       return View::make('Auth.login'); 
     } 

     /** 
     * Login action 
     * @return Redirect 
     */ 
     public function handleLogin() 
     { 
       $credentials = array(
         'email' => Input::get('email'), 
         'password' => Input::get('password') 
       ); 

       try 
       { 
         $user = Sentry::authenticate($credentials, false); 

         if ($user) 
         { 
           return Redirect::action('[email protected]'); 
         } 
       } 
       catch(\Exception $e) 
       { 
         return Redirect::action('[email protected]')->withErrors(array('login' => $e->getMessage())); 
       } 
     } 

     /** 
     * Logout action 
     * @return Redirect 
     */ 
     public function logout() 
     { 
       Sentry::logout(); 

       return Redirect::action('[email protected]')->with('message','You have been logged out'); 
     } 

} 

?> 
+0

possibile duplicato del [estensione modello utente Sentry2] (http://stackoverflow.com/questions/16655070/sentry2-user-model-extension) – Ifnot

risposta

13

Il problema è quando si è configurato per usare Sentry User.php come modello perde il hasher Sentry. La soluzione è quella di impostare il hasher quando un utente si sta registrando

$user->setHasher(new Cartalyst\Sentry\Hashing\NativeHasher); 
+2

Wow, sei un professionista. Perché grazie per questa bellissima spiegazione. Ti segnerò appena possibile (2 minuti);) – jamespick

4

Una migliore alternativa a @Dylan Pierce suggerimento è quello di impostare il hasher direttamente nel costruttore del modello utente.

public function __construct() 
{ 
    $this->setHasher(new \Cartalyst\Sentry\Hashing\NativeHasher); 
} 

nota ci sono altri Hashers diversi forniti da Sentry e si possono trovare in questa directory: vendor/cartalyst/sentry/src/Cartalyst/Sentry/Hashing/

completa Esempio:

use Cartalyst\Sentry\Users\Eloquent\User; 
use Cartalyst\Sentry\Hashing\NativeHasher as SentryNativeHasher; 

class Subscriber extends User 
{ 

    public function __construct() 
    { 
      $this->setHasher(new SentryNativeHasher); 
    } 

} 
+2

Dopo aver raccolto più esperienza di programmazione, devo convenire che questa è una risposta migliore. –

2

Se si desidera utilizzare il metodo setHasher per l'intero modello; al fine di preservare la funzione costruttore di classe Eloquente, utilizzare il metodo statico 'stivale' che sarà eseguito dal costruttore di classe Eloquent:

public static function boot() 
{ 
    static::setHasher(new \Cartalyst\Sentry\Hashing\NativeHasher); 
} 
+0

Dovrai chiamare parent :: boot() all'interno della funzione – developerbmw

1

Il metodo migliore è quello di utilizzare la funzione di avvio Eloquente (come suggerito da @browno), ma usa anche l'hasher della classe genitore, quindi l'impostazione di Sentry per l'hasher verrà comunque utilizzata. Inoltre, ricorda di chiamare la funzione di avvio genitore, altrimenti probabilmente si romperà le cose. Esempio di codice:

use Cartalyst\Sentry\Users\Eloquent\User as SentryUser; 

class User extends SentryUser { 
    protected static function boot() { 
     parent::boot(); 

     static::setHasher(parent::getHasher()); 
    } 
}