2013-05-28 89 views
4

我有不同的用户,其中一些是普通用户,一些是管理员。 我需要的是,只需单击用户列表中的按钮,管理员就可以以任何其他用户身份登录。用特定用户登录Laravel 4

我至今是:

index.blade.php(用户列表):

<a href='{{URL::route('users.loginas', array('id' => $user->id))}}'>LoginAs</a> 

routes.php文件:

Route::group(array('before'=>'auth'), function() 
{ 
    Route::get('/', array('as'=>'index', 'uses'=>'[email protected]')); 

    Route::resource('users', 'UsersController'); 
    Route::any('users/loginas/{id}', array('as'=>'users.loginas', 'uses' => '[email protected]')); 

}); 

UsersController.php:

class UsersController extends BaseController { 

... 

public function loginAs($id) 
    { 
     Auth::logout(); 
     Auth::loginUsingId($id); 

     return Redirect::route('provalogin'); 
    } 

} 

当我点击链接与ID 2从用户列表,而用户与ID为1的用户登录,我正确地重定向到mysite.com/users/loginas/2但随后抛出ErrorException:通过

参数1来照亮\ Auth \ Guard :: login()必须实现 接口Illuminate \ Auth \ UserInterface,null给出,在 /var/www/mysite.com/web/vendor/laravel/framework/src/Illuminate/Auth/中调用Guard.php 上线368和定义

然后,如果我改变的URL mysite.com/users我可以看到,我其实是登录的新用户,所以Auth::loginUsingId(2)瓦特orked。

我在做什么错?或者我该怎么做?

+1

你可以尝试它没有'''Auth :: logout()'''?似乎在同一个请求中不能调用'''Auth :: logout()''和'''Auth :: loginUsingId()'''。 – bryantebeek

+0

如果没有'Auth :: logout()'它不会崩溃,但它也不会切换记录的useras我保持seing永恒,就好像我仍然是管理员用户。 :( – Jester

+1

我遇到了同样的问题,我注意到如果你使用Auth :: login而不是Auth :: loginUsingId,我实际上会与新用户重新登录。但这些方法和如何返回到管理员之间有什么区别 - 我没有任何线索 – Victor

回答

0

我所做的是使用会话来设置用户ID等于auth用户ID。然后,如果我想切换用户,我只需更改会话用户标识,但保留auth用户标识为我自己。在我的应用程序中,我总是在查询数据时传递会话用户标识。如果我更新,创建或软删除记录我传递我的身份验证用户ID。这样我就记录了谁实际上更改了数据。我从来没有注销切换用户,只需设置会话userID。

0

听起来像你的用户模型没有正确设置,继承UserInterface。

您的用户模型应该是这个样子:

class User extends Eloquent implements UserInterface 

和你auth.php的配置应该是这个样子:

return array(

    /* 
    |-------------------------------------------------------------------------- 
    | Default Authentication Driver 
    |-------------------------------------------------------------------------- 
    | 
    | This option controls the authentication driver that will be utilized. 
    | This drivers manages the retrieval and authentication of the users 
    | attempting to get access to protected areas of your application. 
    | 
    | Supported: "database", "eloquent" 
    | 
    */ 

    'driver' => 'eloquent', 

    /* 
    |-------------------------------------------------------------------------- 
    | Authentication Model 
    |-------------------------------------------------------------------------- 
    | 
    | When using the "Eloquent" authentication driver, we need to know which 
    | Eloquent model should be used to retrieve your users. Of course, it 
    | is often just the "User" model but you may use whatever you like. 
    | 
    */ 

    'model' => 'User', 

    /* 
    |-------------------------------------------------------------------------- 
    | Authentication Table 
    |-------------------------------------------------------------------------- 
    | 
    | When using the "Database" authentication driver, we need to know which 
    | table should be used to retrieve your users. We have chosen a basic 
    | default value but you may easily change it to any table you like. 
    | 
    */ 

    'table' => '', 

    /* 
    |-------------------------------------------------------------------------- 
    | Password Reminder Settings 
    |-------------------------------------------------------------------------- 
    | 
    | Here you may set the settings for password reminders, including a view 
    | that should be used as your password reminder e-mail. You will also 
    | be able to set the name of the table that holds the reset tokens. 
    | 
    */ 

    'reminder' => array(
     'email' => 'emails.forgot-pass', 'table' => 'forgot_pass' 
    ), 

) 
+0

但这还不是全部,他还必须实现getAuthIdentifier()和getAuthPassword(),以便UserInterface工作。请参阅http://stackoverflow.com/questions/17418085/laravel-4-auth-with-facebook-no-password-authentication – orrd

0

对我来说,与\Session::flush();工作。在会议中似乎还记得一些变数。

0

我发现我的用户是空的,我通过加载一个正确的用户来解决,我不知道它是否也适用于您,但您可以尝试。