Laravel 5.5权限-用户没有正确的角色 [英] Laravel 5.5 Permissions - User does not have the right roles

查看:312
本文介绍了Laravel 5.5权限-用户没有正确的角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在管理员用户没有分配角色的用例中找到解决方案. 在仪表板视图中,它不会为用户呈现url,而如果直接访问仪表板/用户,则会得到:

I'm trying to find a solution in a use case where the admin user does not have the assigned role. In dashboard view it doesn't render the url for users, while if I directly access the dashboard/users, I get:

Spatie \ Permission \ Exceptions \ UnauthorizedException user does not have the right roles

app/Http/Kernel.php

app/Http/Kernel.php

    protected $routeMiddleware = [
     ....
    'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
    'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
];

routes/web.php

routes/web.php

Route::group(
   ['middleware' => ['role:admin']],
    function () {
      Route::get('/dashboard/users', 'UsersController@index');
      Route::get('/dashboard/users/{id}', 'UsersController@edit');
      Route::patch('/dashboard/users/{id}', 'UsersController@update');
   }
);

view/dashboard.blade.php

view/dashboard.blade.php

<div class="panel-body">
  @hasrole('admin')
     <li><a href="/dashboard/users">Manage Users</a></li>
  @endhasrole
</div>

我已经成功生成了默认角色&

I have successfully generated the default roles & permission with

Commands/GenerateRoles.php

Commands/GenerateRoles.php

    public function handle()
    {
    $this->info('Generating default roles and permissions...');
    $admin = User::create(
      [
        'name'     => 'administrator',
        'email'    => 'adm@mail.com',
        'password' => bcrypt('12345'),
      ]
    );

    // Create roles.
    $adminRole   = Role::create(['name' => 'admin']);
    $supportRole = Role::create(['name' => 'support']);

    $admin->assignRole('admin');

    // Create permissions.
    $userManagement = Permission::create(['name' => 'users management']);
    $deleteImages  = Permission::create(['name' => 'delete images']);
    $datasetStatus   = Permission::create(
      ['name' => 'change dataset building status']
    );

    $adminRole->givePermissionTo($userManagement);
    $deleteImages->syncRoles([$adminRole, $supportRole]);
    $datasetStatus->syncRoles([$adminRole, $supportRole]);
   }

这可能会出错吗?谢谢您的时间.

What it could possibly goes wrong? Thanks for you time.

推荐答案

您应该覆盖render方法进行重定向(或执行任何操作).转到Expectations/Handler.php并覆盖渲染函数,如下所示:

You should overwrite the render method to redirect (or whatever you want to do). Go to Expections/Handler.php and overwrite the render function like this:

public function render($request, Exception $exception)
{
    if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
          return redirect('/');
    }

    return parent::render($request, $exception);
}

来源: https://github.com/spatie/laravel-permission#catching-role-permission-failures

这篇关于Laravel 5.5权限-用户没有正确的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆