简体   繁体   English

Laravel 5-角色权限

[英]Laravel 5 - Role permissions

I try to make my own permission system. 我尝试建立自己的权限系统。 but got a problem with the code when if the user doesn't have a specific role they can't access the page. 但如果用户没有特定角色,则他们无法访问页面,则代码出现问题。

Here is my code: 这是我的代码:

public function compose($view)
{
    $roleid = Session::get('role_id');
    $userid = Session::get('user_id');
    $additional_role = Session::get('additional_role');

    $roles = explode(';', $additional_role);

    $segment = Request::segment('1');

    $joins = [];
    foreach ($roles as $role) 
    {
        $query = DB::table('roles')->where('id', $role)->first();
        $joins[] = $query->link;

    }


    foreach($joins as $join) {
        var_dump($join); var_dump($segment);

        if ($join == $segment) 
        {
              return redirect($segment);
        } 
        else
        {
            return redirect('/');
        }   
    }         



}

i make the code within a composer so i can keep booting it up. 我在作曲家内编写代码,这样我就可以继续启动它。 But i can't make it work, even the user doesn't have the role they not redirected to '/'. 但是我无法使其工作,即使用户没有他们未重定向到“ /”的角色。

Is there any way to make it work? 有什么办法可以使其工作? So they won't get into the page and they will get into the page if they have the role. 因此,他们将不会进入该页面,并且如果他们具有该角色,也将进入该页面。

where do you have this logic? 您在哪里有这种逻辑?

i think in l5 best place to permissions and auth logic is "Http/Middleware", there you can add your file with handle method and logic within, and then you must edit kernel.php 我认为在l5权限和auth逻辑的最佳位置是“ Http / Middleware”,您可以在其中添加带有handle方法和逻辑的文件,然后必须编辑kernel.php

protected $routeMiddleware = [
        'auth'          => 'App\Http\Middleware\Authenticate',
        'auth.basic'    => 'Illuminate\Auth\Middleware\AuthenticateWithBasicAuth',
        'guest'         => 'App\Http\Middleware\RedirectIfAuthenticated',
        'test'          => 'App\Http\Middleware\yourFile',
    ];

you can view great solution for laravel acl here : https://gist.github.com/amochohan/8cb599ee5dc0af5f4246 您可以在此处查看适用于laravel acl的绝佳解决方案: https ://gist.github.com/amochohan/8cb599ee5dc0af5f4246

and then you can easy make your role permissions 然后您可以轻松地设置角色权限

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM