2013-05-28 4 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가 1 인 사용자와 로그인 한 상태에서 사용자 목록의 ID가 2 인 사용자의 경우 mysite.com/users/loginas/2로 올바르게 리디렉션되었지만 Error가 throw됩니다.

인수 1이 통과되었습니다. \ Auth \ Guard :: login()은 인터페이스를 구현해야합니다. \ Auth \ UserInterface, 주어진 null, /var/www/mysite.com/web/vendor/laravel/framework/src/Illuminate/Auth/에서 호출됩니다. 라인 368에 Guard.php 와 내가 mysite.com/users에 URL을 변경하면 나는 새 사용자로 로그인 한 사실에있어 볼, 그래서 Auth::loginUsingId(2)w 수

그런

정의 orked.

내가 뭘 잘못하고 있니? 또는 어떻게해야합니까?

+1

'''Auth :: logout()'''없이 사용해 볼 수 있습니까? Auth :: logout()''과''Auth :: loginUsingId()'''는 같은 요청으로 호출 될 수 없다. – bryantebeek

+0

'Auth :: logout()'이 없으면 충돌이 발생하지 않지만 관리자 인 것처럼 계속 로그인 한 사용자를 전환하지 않습니다. : – Jester

+1

동일한 문제가 발생했습니다. Auth :: loginUsingId 대신 Auth :: login을 사용하면 실제로 새 사용자와 다시 로그인하게됩니다.하지만이 방법들과 어떻게 관리자에게 돌아갈 지에 대한 차이점은 무엇입니까? – Victor

답변

0

사용자 ID를 인증 사용자 ID와 동일하게 설정하려면 세션을 사용하십시오. 그런 다음 사용자를 전환하려면 세션 userID를 변경하지만 인증 사용자 아이디를 그대로 유지해야합니다. 내 응용 프로그램에서는 항상 데이터를 쿼리 할 때 세션 userID를 전달합니다. 레코드를 업데이트, 생성 또는 소프트 삭제하면 인증 사용자 아이디가 전달됩니다. 그렇게하면 누가 실제로 데이터를 변경했는지에 대한 기록이 있습니다. 사용자를 전환하기 위해 로그 아웃 할 필요가 없습니다. 세션 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

하지만 그게 전부는 아니지만 UserInterface가 작동하려면 getAuthIdentifier()와 getAuthPassword()도 구현해야합니다. http://stackoverflow.com/questions/17418085/laravel-4-auth-with-facebook-no-password-authentication을 참조하십시오. – orrd

0

은 나에게는 \Session::flush();로했다. 일부 변수는 여전히 세션에서 기억 된 것으로 보입니다.

0

나는 내 사용자가 비어있는 것을 알았고 올바른 사용자를로드하여 해결했습니다. 나는 당신도 잘 할 수 있을지 모르지만 시도해 볼 수는 있습니다.