2

我正在为我的应用程序使用 Laravel 4。在这个应用程序中,我有两个身份验证模型:买家和用户。我不会使用 User->type 字段,因为这些模型具有完全不同的逻辑。

这是我的登录控制器:

public function postIndex()
{

    if (Auth::attempt(array_only(Input::get(), array('email', 'password')), array_only(Input::get(), array('save')))) {
        Login::create(array('user_id' => Auth::user()->id, 'session_id' => session_id())); // Создаем новую запись логина вместе с session_id.
        return Redirect::to('/');
    }

    return $this->userauth();
}

public function userauth() {

    Config::set('auth.model', 'User');
    Config::set('auth.table', 'users');
    $test = Config::get('auth.model');

    if (Auth::attempt(array_only(Input::get(), array('email', 'password')), array_only(Input::get(), array('save')))) {
        Login::create(array('user_id' => Auth::user()->id, 'session_id' => session_id())); // Создаем новую запись логина вместе с session_id.
        return Redirect::to('/');
    }

    Session::flash('error', 'Auth not excepted. '. implode(' ', array_only(Input::get(), array('email', 'password'))));

    return Redirect::to('logins')->withInput(Input::except('password'));

}

我已经更改了 auth.php 中的设置以与买家合作。当我为买家输入登录名和密码时,一切正常。看来,在 Auth::attempted() 之后它不会更改设置。看来我必须重新加载 Auth 对象。有人可以帮助我吗?

买的方式,如果我这样写:

    public function postIndex()
{


    Config::set('auth.model', 'User');
    Config::set('auth.table', 'users');
    $test = Config::get('auth.model');

    if (Auth::attempt(array_only(Input::get(), array('email', 'password')), array_only(Input::get(), array('save')))) {
        Login::create(array('user_id' => Auth::user()->id, 'session_id' => session_id())); // Создаем новую запись логина вместе с session_id.
        return Redirect::to('/');
    }

    Session::flash('error', 'Auth not excepted. '. implode(' ', array_only(Input::get(), array('email', 'password'))));

    return Redirect::to('logins')->withInput(Input::except('password'));
}

一切都很好。

4

1 回答 1

4

简短的回答: 你是对的。第一次调用后Auth::attempt()对配置的更改没有任何效果。在运行时,您必须使用Auth::setProvider()来设置将要使用的模型。

长答案: 我不知道这在您的特定设置中效果如何,但是我在尝试做类似的事情时发现了您的问题,所以我将向您展示我最终是如何做到的。

就我而言,要求不仅仅是拥有两种不同类型的用户。我在来自同一个代码库的不同域上运行两个网站,一个用于学生,一个用于寄宿家庭。我有一个Student课程将在一个站点上替换User,而在另一个站点上则Host用于相同的目的。都实现UserInterfaceand RemindableInterface,所以我们很好。

在 app/filters.php 中,我创建了两个特殊的过滤器:

Route::filter('prep.student', function() {
  Auth::setProvider(new Illuminate\Auth\EloquentUserProvider(App::make('hash'), 'Student'));
});

Route::filter('prep.host', function() {
  Auth::setProvider(new Illuminate\Auth\EloquentUserProvider(App::make('hash'), 'Host'));
});

这里的“Host”和“Student”是你希望 Eloquent 认证驱动使用的类名。

在 app/routes.php 中,我将路由分为两组,在每组上我使用了上面的正确过滤器:

/**
 * Routes for Students
 */
Route::group(array('domain' => '[student domain]', 'before' => 'prep.student'), function() {

  Route::get('test', function() {
    return View::make('student/test');
  });
  ...
});

/**
 * Routes for Hosts
 */
Route::group(array('domain' => '[host domain'], 'before' => 'prep.host'), function() {

  Route::get('test', function() {
    return View::make('host/test');
  });
  ...
});

结果是用户提供者为每组路由设置了正确的模型。您可能还可以将提供程序设置为app/start/global.php,但我决定将其保留在路由中,以便很清楚两组路由用于两个不同的域并具有两种不同的身份验证模型。

如果我构建它是新的,我会做一些不同的事情,但是这两个网站是一个大型遗留代码库的前端,我无法对其数据库进行重大更改,所以我坦率地承认这有点像 hack。如果您不是这种情况,更好的解决方案可能是拥有一个User用于身份验证的类,然后将其附加到您需要的另外两个模型上。

于 2013-12-07T04:05:36.327 回答