当前位置: 首页 > 知识库问答 >
问题:

在Laravel 4上与特定用户登录

杜元明
2023-03-14

我有不同的用户,其中一些是普通用户,一些是管理员。我需要的是管理员可以通过单击用户列表中的按钮作为任何其他用户登录。

到目前为止,我有:

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'=>'HomeController@showWelcome'));

    Route::resource('users', 'UsersController');
    Route::any('users/loginas/{id}', array('as'=>'users.loginas', 'uses' => 'UsersController@loginAs'));

});

用户控制器。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,但随后它会抛出一个ErrorException:

传递给Illumb\Auth\Guard::login()的参数1必须实现接口Illumb\Auth\UserInterface,给定null,在/var/www/mysite中调用。com/web/vendor/laravel/framework/src/light/Auth/Guard。第368行的php,已定义

然后,如果我将URL更改为mysite。com/users我可以看到我实际上是以新用户的身份登录的,因此Auth::loginUsingId(2)有效。

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

共有3个答案

龚永新
2023-03-14

对我来说,它适用于\会话::flush();。会议似乎还记得一些变量。

司空福
2023-03-14

听起来你的用户模型没有正确设置,继承了用户界面。

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

class User extends Eloquent implements UserInterface

还有你的作者。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'
    ),

)
万俟宜修
2023-03-14

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

 类似资料:
  • 我正在尝试构建一个使用数据库的应用程序,允许用户以管理员或员工身份登录 管理员几乎没有选项: 添加新员工, 列出所有员工和 为选定员工添加待办事项 受雇者 登录时显示待办事项 我遇到的第一个问题是: 我应该为所有记录(员工)只创建一个表吗 第二个问题是,我不知道如何为成功登录的用户显示项目列表。如何访问登录用户的数据?

  • 我试图添加Google SignIn到我的Firebase Webapp,根据文档,这是通过

  • 我目前有一个WP站点设置,安装了一个独特的“multisite”插件,专门用于WooCommerce产品的一个管理区域,但有两个不同的前端,基于两个不同的域,具有不同的主题。 其中一个站点是“批发”站点,而另一个是“零售”站点。批发网站只允许交易客户进行购买。问题在于这两个站点共享一个域,因此用户帐户。 问题:我需要确保,如果没有用户角色“trade_customer”的用户尝试登录批发网站,则会

  • 我遵循本指南的Spring引导安全JWT,以了解如何使用Spring引导安全和jwt保护应用程序,我调用 /authenticateapi来测试登录功能。 我正在使用postman调用api,通过json传递用户名和密码: AuthenticationManager。正在验证用户是否已锁定 我的UserDetails直接在用户实体上实现,这不是最佳实践,但我现在对如何实现它没有更好的想法,也许我应

  • 问题内容: 如何仅从服务器向特定用户发送websocket消息? 我的webapp具有spring安全设置,并使用websocket。我在尝试仅从服务器向特定用户发送消息时遇到棘手的问题。 通过阅读手册,我的理解是来自我们可以做的服务器 在客户端: 但是我永远无法调用订阅回调。我尝试了许多不同的方法,但是没有运气。 如果我将其发送到/ topic / reply,它可以工作,但所有其他已连接用户也

  • 如何仅将websocket消息从服务器发送给特定用户? 我的webapp具有Spring Security设置,并使用websocket。我在尝试只从服务器向特定用户发送消息时遇到了一个棘手的问题。 从阅读手册中我的理解是从服务器上我们可以做到的 在客户端: 但是我永远无法调用订阅回调。我尝试了许多不同的道路,但没有运气。 如果我将它发送到/topic/reply,它会工作,但所有其他连接的用户也