Laravel 5新的auth:获取当前用户和如何实现角色?

时间:2022-11-27 14:28:06

I am currently experimenting with the new Laravel 5 and got the authentication to work (register/login).

我目前正在试验新的Laravel 5,并让认证工作(注册/登录)。

To get the authenticated user in my controller I currently inject Guard into the controller action:

为了在我的控制器中获得经过身份验证的用户,我现在向控制器操作注入了保护:

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;

class ClientController extends Controller {

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(Guard $auth)
    {
        return view('client.index', ['user' => $auth->user()]);
    }
...

First Question: Is this the recommended way?

第一个问题:这是推荐的方法吗?

Second Question: How would I go about implementing some kind of roles/permissions? Something like client.edit, client.add, ... Does Larval 5 offer some kind of convenience here? How would I set the necessary role/permission for a route/controller action?

第二个问题:如何实现某种角色/权限?类似的客户端。编辑客户端。添加、…Larval 5在这里提供了一些便利吗?如何设置路由/控制器操作的必要角色/权限?

I am thinking that I might need to write my own middleware for that. Any suggestions on how to approach the problem?

我在想,我可能需要为此编写自己的中间件。对如何解决这个问题有什么建议吗?

2 个解决方案

#1


50  

After spending some more time on Laravel 5 I can an answer my own question:

花了更多的时间在Laravel 5上,我可以回答我自己的问题:

Is injecting Guard the recommended way? No: If you need to access Auth in your view, you can do so already like this:

注射防护是推荐的方法吗?不:如果你需要在你的视图中访问Auth,你可以这样做:

@if( Auth::check() )
    Current user: {{ Auth::user()->name }}
@endif

This uses the Auth facade. A list of all available facades is in config/app.php under aliases:

这使用了Auth外观。所有可用的facades列表都在config/app中。php在别名:

What if I need Auth in my controller? Injecting an instance of Guard like shown in the question works, but you don't need to. You can use the Auth facade like we did in the template:

如果我在控制器中需要Auth怎么办?在这个问题中注入一个类似于“警卫”的实例是可行的,但您不需要这样做。你可以像我们在模板中那样使用Auth facade:

    public function index()
    {
        if(\Auth::check() && \Auth::user()->name === 'Don') {
            // Do something
        }
        return view('client.index');
    }

Be aware that the \ is needed before the facade name since L5 is using namespaces.

请注意,由于L5使用名称空间,所以在facade名称之前需要\。

I want to have permissions/roles using the new auth mechanism in L5: I implemented a lightweight permission module using the new middleware, it is called Laraguard. Check it out on Github and let me know what you think: https://github.com/cgrossde/Laraguard

我希望在L5中使用新的auth机制来获得权限/角色:我使用新的中间件实现了一个轻量级权限模块,它被称为Laraguard。看看Github上的内容,让我知道你的想法:https://github.com/cgrossde/Laraguard。

UPDATE: For the sake of completeness I want to mention two more projects. They provide everything you need to save roles and permissions in the DB and work perfectly together with Laraguard or on their own:

更新:为了完整起见,我想再提两个项目。它们提供了在DB中保存角色和权限所需的所有内容,并与Laraguard或它们自己完美地结合在一起:

#2


0  

If you want make your own custom authentification, you need to keep the User model from Laravel 5 with all the dependency. After you will be able to login your user in your controller. Dont forget to put (use Auth;) after the namespace of your controller.

如果您想要定制自定义,那么您需要将用户模型从Laravel 5保留到所有依赖项。在您将能够登录您的用户在您的控制器。不要忘记在您的控制器的名称空间后面加上(使用Auth)。

#1


50  

After spending some more time on Laravel 5 I can an answer my own question:

花了更多的时间在Laravel 5上,我可以回答我自己的问题:

Is injecting Guard the recommended way? No: If you need to access Auth in your view, you can do so already like this:

注射防护是推荐的方法吗?不:如果你需要在你的视图中访问Auth,你可以这样做:

@if( Auth::check() )
    Current user: {{ Auth::user()->name }}
@endif

This uses the Auth facade. A list of all available facades is in config/app.php under aliases:

这使用了Auth外观。所有可用的facades列表都在config/app中。php在别名:

What if I need Auth in my controller? Injecting an instance of Guard like shown in the question works, but you don't need to. You can use the Auth facade like we did in the template:

如果我在控制器中需要Auth怎么办?在这个问题中注入一个类似于“警卫”的实例是可行的,但您不需要这样做。你可以像我们在模板中那样使用Auth facade:

    public function index()
    {
        if(\Auth::check() && \Auth::user()->name === 'Don') {
            // Do something
        }
        return view('client.index');
    }

Be aware that the \ is needed before the facade name since L5 is using namespaces.

请注意,由于L5使用名称空间,所以在facade名称之前需要\。

I want to have permissions/roles using the new auth mechanism in L5: I implemented a lightweight permission module using the new middleware, it is called Laraguard. Check it out on Github and let me know what you think: https://github.com/cgrossde/Laraguard

我希望在L5中使用新的auth机制来获得权限/角色:我使用新的中间件实现了一个轻量级权限模块,它被称为Laraguard。看看Github上的内容,让我知道你的想法:https://github.com/cgrossde/Laraguard。

UPDATE: For the sake of completeness I want to mention two more projects. They provide everything you need to save roles and permissions in the DB and work perfectly together with Laraguard or on their own:

更新:为了完整起见,我想再提两个项目。它们提供了在DB中保存角色和权限所需的所有内容,并与Laraguard或它们自己完美地结合在一起:

#2


0  

If you want make your own custom authentification, you need to keep the User model from Laravel 5 with all the dependency. After you will be able to login your user in your controller. Dont forget to put (use Auth;) after the namespace of your controller.

如果您想要定制自定义,那么您需要将用户模型从Laravel 5保留到所有依赖项。在您将能够登录您的用户在您的控制器。不要忘记在您的控制器的名称空间后面加上(使用Auth)。