Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何为客人分配角色&;权限_Php_Laravel_Laravel 5.2_Entrust - Fatal编程技术网

Php 如何为客人分配角色&;权限

Php 如何为客人分配角色&;权限,php,laravel,laravel-5.2,entrust,Php,Laravel,Laravel 5.2,Entrust,我目前正在尝试将角色“guest”分配给任何来宾,以便他们可以拥有权限。我目前有以下代码,它是一些中间件的一部分,这似乎是一个错误的地方有它,我会假设有一个更好的地方,我已经尝试使用服务提供商,但我不能附加组 if($this->auth->guest()) { $user = new User(); $user->username = 'Guest'; $role = Role::where('name', '=',

我目前正在尝试将角色“guest”分配给任何来宾,以便他们可以拥有权限。我目前有以下代码,它是一些中间件的一部分,这似乎是一个错误的地方有它,我会假设有一个更好的地方,我已经尝试使用服务提供商,但我不能附加组

 if($this->auth->guest())
    {
        $user = new User();
        $user->username = 'Guest';

        $role = Role::where('name', '=', 'guest')
                ->with('perms')
                ->first();


        $user->perms = new Collection();
        $user->perms->add($role);

        $perms = explode('|', $permissions);

        foreach($user->perms as $p) {
            foreach($p->perms as $pp) {
                foreach($perms as $perm) {
                    if($perm === $pp->name)
                        return $next($request);
                }
            }
        }
    }

正如您所看到的,这对于中间件是非常特定的,理想情况下,我希望在第一个可能的实例中攻击角色,以便它可以用于应用程序的任何部分

  session::put('role',$role);
在我看来

 @if(Session::has('role') == 'whatever') 
 // show this content
权限也是如此
在会话中添加一个perm数组,然后在数组中使用
,在这种情况下只需使用会话即可

  session::put('role',$role);
在我看来

 @if(Session::has('role') == 'whatever') 
 // show this content
权限也是如此
在会话中添加一个perm数组,然后在数组中使用

我认为位置是对的,但是请记住,您正在对每个请求执行此验证,它看起来很繁重。那么,为什么不使用会话或缓存呢

使用会话:

if($this->auth->guest()){
    $userPerms = [];

    if (\Session::has('permissions')){
        $userPerms = \Session::get('permissions');
    }
    else{
        $user = new User();
        $user->username = 'Guest';
        // the rest of your code here...
        $userPerms = $user->perms;

        // update the session the first time
        \Session::put('permissions', userPerms)
    }

    // comparison here
}

您可以使用
Cache
provider应用相同的逻辑。

我认为位置是正确的,但请记住,您正在对每个请求执行此验证,它看起来很繁重。那么,为什么不使用会话或缓存呢

使用会话:

if($this->auth->guest()){
    $userPerms = [];

    if (\Session::has('permissions')){
        $userPerms = \Session::get('permissions');
    }
    else{
        $user = new User();
        $user->username = 'Guest';
        // the rest of your code here...
        $userPerms = $user->perms;

        // update the session the first time
        \Session::put('permissions', userPerms)
    }

    // comparison here
}
您可以使用
缓存
提供程序应用相同的逻辑