2013-12-14 14 views
7

Tôi dường như không thể làm việc sentry. Tôi vẫn gặp lỗi này: A hasher has not been provided for the user. Có ai biết điều gì sẽ xảy ra không?A hasher chưa được cung cấp cho người dùng

The error log

Tôi đang chạy MAMP trên OS X 10.9. Tôi đang sử dụng php 5.4.4 MCrypt được cài đặt và kích hoạt. Lỗi này xảy ra khi cố gắng băm mật khẩu khi tạo người dùng mới. Dự án của chúng tôi sử dụng plugin ghép Sentry. Đây là bộ điều khiển:

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

thể trùng lặp của [mở rộng sử dụng mô hình Sentry2] (http://stackoverflow.com/questions/16655070/sentry2-user-model-extension) – Ifnot

Trả lời

13

Vấn đề là khi bạn định cấu hình Sentry để sử dụng User.php làm mô hình của bạn mất dấu Sentry. Giải pháp là để thiết lập hasher khi người dùng đăng ký

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

Wow, bạn là một người chuyên nghiệp. Tại sao cảm ơn bạn vì lời giải thích tuyệt vời này. Tôi sẽ đánh dấu bạn càng sớm càng tốt (2 phút);) – jamespick

4

Một lựa chọn tốt hơn để @Dylan Pierce gợi ý là để thiết lập hasher trực tiếp trong các nhà xây dựng mô hình sử dụng của bạn.

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

lưu ý có Hashers khác nhau khác được cung cấp bởi Sentry và bạn có thể tìm thấy chúng ở thư mục này: vendor/cartalyst/sentry/src/Cartalyst/Sentry/Hashing/

Full Ví dụ:

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

Sau khi thu thập thêm lập trình kinh nghiệm, tôi phải đồng ý đây là câu trả lời hay hơn. –

2

Nếu bạn muốn sử dụng phương pháp setHasher cho toàn bộ mô hình; để duy trì chức năng constructor lớp hùng hồn, sử dụng phương thức tĩnh 'khởi động' mà sẽ được chạy từ lớp constructor hùng biện:

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

Bạn sẽ muốn gọi parent :: boot() bên trong hàm – developerbmw

1

Phương pháp tốt nhất là sử dụng các chức năng khởi động hùng biện (theo đề nghị của @browno), nhưng cũng sử dụng hasher từ lớp cha, do đó, thiết lập cấu hình Sentry cho hasher sẽ vẫn được sử dụng. Ngoài ra, hãy nhớ gọi hàm khởi động gốc, nếu không nó có thể sẽ làm hỏng mọi thứ. mã ví dụ:

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

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

     static::setHasher(parent::getHasher()); 
    } 
} 
Các vấn đề liên quan