2013-12-14 60 views
7

我似乎無法使哨兵工作。我不斷收到此錯誤:A hasher has not been provided for the user.有誰知道會發生什麼?尚未爲用戶提供散列器

The error log

我運行在OS X 10.9甲基苯丙胺。 我正在使用php 5.4.4 MCrypt已安裝並啓用。 創建新用戶時嘗試散列密碼時發生此錯誤。 我們的項目使用laravel Sentry插件。 這裏是控制器:

<?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

可能重複(http://stackoverflow.com/questions/16655070/sentry2-user-model-extension) – Ifnot

回答

13

問題是,當你配置哨兵使用user.php的爲你的模型失去哨兵散列器。解決的辦法是設置散列器,當用戶註冊

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

哇,你是一個親。爲什麼感謝你這個美麗的解釋。我會盡快爲您勾選(2分鐘);) – jamespick

4

到@Dylan皮爾斯建議一個更好的選擇是直接在你的用戶模型的構造函數中設置散列器。

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

注有由哨兵提供不同Hashers,你可以在此目錄中找到它們: vendor/cartalyst/sentry/src/Cartalyst/Sentry/Hashing/

完整的例子:

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

在收集了更多體驗編程之後,我必須同意這是一個更好的答案。 –

2

如果你想使用的setHasher方法整個模型;爲了保持雄辯類的構造函數,使用靜態方法「開機」,其將從雄辯構造類中運行:

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

你會想在函數內調用parent :: boot() – developerbmw

1

最好的方法是用雄辯的開機功能(通過@browno的建議),但也可以使用父類中的hasher,因此仍然會使用hasher的Sentry配置設置。另外,請記住調用父啓動函數,否則它可能會破壞事情。實施例的代碼:[Sentry2用戶模型擴展]的

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

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

     static::setHasher(parent::getHasher()); 
    } 
} 
相關問題