Allow admin users to see what other user type can see/do?

淺唱寂寞╮ 提交于 2019-12-04 16:48:13

You could try what I did in one of my projects - implementation is pretty simple, maybe you can make use of that as well.

There is additional action in our AuthController that allows a user to switch to other users and remembers current user ID in session:

public function switchUser($userId)
{
    // disallow switched users from switching again
    if (Session::get('previous_user')) App::abort(403);

    $user = User::findOrFail($userId);

    Session::set('previous_user', Auth::id());

    Auth::login($user);

    return redirect('some path');
}

Second part is customized logout function, that for switched users switches them back to their original user account instead of logging out:

public function getLogout()
{
    if ($previousUser = Session::get('previous_user')) {
        Session::remove('previous_user');
        Auth::loginUsingId($previousUser);

        return redirect('some path');
    }

    Auth::logout();

    return redirect('some path');
}

With that logic you'll be able to switch to other users and back. You might need to add permission checking, so that only admins can do that etc., link the customers in the list to the switch URL, anyway the core of the functionality is there in the code above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!