【问题标题】:Laravel 5.5 Permissions - User does not have the right rolesLaravel 5.5 权限 - 用户没有正确的角色
【发布时间】:2018-08-14 08:19:27
【问题描述】:

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

Spatie \ Permission \ Exceptions \ UnauthorizedExceptionuser does not have the right roles

app/Http/Kernel.php

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

路由/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');
   }
);

查看/dashboard.blade.php

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

我已成功生成默认角色和权限

命令/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]);
   }

它可能会出什么问题?谢谢你的时间。

【问题讨论】:

    标签: php laravel permissions laravel-5.5


    【解决方案1】:

    您应该覆盖渲染方法以重定向(或任何您想做的事情)。转到 Expections/Handler.php 并像这样覆盖渲染函数:

    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-and-permission-failures

    【讨论】:

    • 感谢您的回复。也许我没有很好地描述这个问题。问题是管理员用户没有访问管理员域的特权。非常感谢您让我知道如何捕获异常,但这并不能解决问题。
    • @sniafas 对不起,如果我误解了。我会一步一步调试它。 1) 使用 tinker 确保您的用户具有该角色 2) 确保您的 User.php 正在使用特征 HasRoles 3) 将您的 @hasrole('admin') 更改为 @if(auth()->user()->hasRole ('admin')) 看看这是否有所作为。 4)在您的路线中,您还可以执行 dd(auth()->user());看看你是否有正确的用户。附带说明,您可以在路线组中将“仪表板”设置为前缀,这样您就不会在每条路线中重复它
    • 不用担心。我真的很感谢你的时间。 1)它工作正常。 2,3,4 给出false。所以我再次设法migrate:fresh & db:seed。突然间一切都正常了。真的很奇怪,但它有效!
    猜你喜欢
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    相关资源
    最近更新 更多