7

我似乎无法让哨兵工作。我不断收到此错误:A hasher has not been provided for the user.有谁知道会发生什么?

错误日志

我在 OS X 10.9 上运行 MAMP。我正在使用 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('AuthController@login')->with('message', 'Thanks for registering!');
                } else {
                // validation has failed, display error messages   
                    return Redirect::action('AuthController@register')->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('OfferController@offer');
                        }
                }
                catch(\Exception $e)
                {
                        return Redirect::action('AuthController@login')->withErrors(array('login' => $e->getMessage()));
                }
        }

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

                return Redirect::action('AuthController@login')->with('message','You have been logged out');
        }

}

?>
4

4 回答 4

13

问题是当您将 Sentry 配置为使用 User.php 作为模型时,它会丢失 Sentry 哈希。解决方案是在用户注册时设置哈希

$user->setHasher(new Cartalyst\Sentry\Hashing\NativeHasher);
于 2013-12-14T19:31:08.280 回答
4

@Dylan Pierce 建议的更好替代方法是直接在用户模型的构造函数中设置哈希。

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

请注意,Sentry 还提供了其他不同的 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);
     }

}
于 2014-12-15T19:51:31.123 回答
2

如果要对整个模型使用 setHasher 方法;为了保留 Eloquent 类的构造函数,请使用静态方法“boot”,该方法将从 Eloquent 构造函数类运行:

public static function boot()
{
    static::setHasher(new \Cartalyst\Sentry\Hashing\NativeHasher);
}
于 2015-01-05T16:04:55.333 回答
1

最好的方法是使用 Eloquent 引导功能(如 @browno 建议的那样),但也使用父类中的哈希器,因此仍将使用哈希器的 Sentry 配置设置。另外,记得调用父引导函数,否则它可能会破坏事情。示例代码:

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

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

        static::setHasher(parent::getHasher());
    }
}
于 2015-06-23T03:55:45.583 回答