duanbiyi7319 2014-10-14 09:55 采纳率: 100%
浏览 25
已采纳

Laravel有两种不同的用户类型

With my laravel 5 app, I want to have both admin users, and clients. In the past I have used the user table for both, and had a flag which which type of user it was - with a join table for more client information.

I have found this approach quite messey... and think it'd be better to have both a users table and model and clients table and model.

This leads me to my problem though, things like the auth password reset, auth attempt are for the users table. Is it possible to make this also work on the client table.

Is that even possible? (I've tagged laravel 4 as solution might also effect this).

  • 写回答

1条回答 默认 最新

  • doubo1871 2014-10-14 10:01
    关注

    I've had a similar issue with dealing with multiple users in Laravel.

    I found the multiauth Laravel Package to be very helpful, check this out https://github.com/ollieread/multiauth

    The documentation is pretty straight forward. The plugin ultimately expands the native Laravel Auth to allow multiple users.

    Let me paste a snippet from the website:

    Usage

    Everything is done the exact same way as the original library, the one exception being that all >method calls are prefixed with the key (account or user in the above examples) as a method itself.

    Auth::account()->attempt(array(
        'email'     => $attributes['email'],
        'password'  => $attributes['password'],
    ));
    Auth::user()->attempt(array(
        'email'     => $attributes['email'],
        'password'  => $attributes['password'],
    ));
    Auth::account()->check();
    Auth::user()->check();
    

    I found that have to call the user() method on a user type called user() looked messy, so I have added in a nice get method to wrap around it.

    Auth::user()->get();
    

    In the instance where you have a user type that can impersonate another user type, example being an admin impersonating a user to recreate or check something, I added in an impersonate() method which simply wraps loginUsingId() on the request user type.

    Auth::admin()->impersonate('user', 1, true);
    

    The first argument is the user type, the second is the id of said user, and the third is whether or not to remember the user, which will default to false, so can be left out more often than not.

    And so on and so forth.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?