如何将会话变量引入到此Laravel 8应用程序的AuthServiceProvider中? [英] How can I bring a session variable into the AuthServiceProvider in this Laravel 8 application?

查看:27
本文介绍了如何将会话变量引入到此Laravel 8应用程序的AuthServiceProvider中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有用户、角色和权限的Laravel 8应用程序。我使用Microsoft Azure进行用户注册和登录。我首先关注了他们网站上的this tutorial

我在routesweb.php中使用了一个自定义中间件来区分经过身份验证的用户和来宾:

Route::group(['prefix' => 'dashboard', 'middleware' => ['checkSignedIn']], function() {
    Route::get('/', [DashboardContoller::class, 'index'])->name('dashboard');
    //More routes
}); 

我从permissionsMySQL表中获得了特定于每个用户角色的逗号分隔的用户权限列表。

我将userPermissions变量存储在如下所示的会话中:

public function storeTokens($accessToken, $user, $user_role, $user_permissions) {
 session([
  'accessToken' => $accessToken->getToken(),
  'refreshToken' => $accessToken->getRefreshToken(),
  'tokenExpires' => $accessToken->getExpires(),
  'userName' => $user->getDisplayName(),
  'firstName' => $user->getGivenName(),
  'lastName' => $user->getSurname(),
  'userRole' => $user_role,
  'userPermissions' => $user_permissions,
  'userEmail' => null !== $user->getMail() ? $user->getMail() : $user->getUserPrincipalName(),
  'userTimeZone' => $user->getMailboxSettings()->getTimeZone()
 ]);
}

这使我可以在视图(navbar.blade.php部分)中直接从会话输出当前(已登录)用户的权限列表,如下所示:

@if(session('userPermissions'))
  <ul>
    @foreach (session('userPermissions') as $up)
      <li>{{ $up }}</li>
    @endforeach
  </ul>
@endif 

目标

我的意图(创建session('userPermissions')的目的)是使用该用户在盖茨中的权限。为此,在appProvidersAuthServiceProvider.php中,我有:

// More code
use IlluminateSupportFacadesGate;
// More code

/* View Users */
Gate::define('view-users', function() {
  return in_array('view-users', session('userPermissions'));
});

在基本控制器(appHttpControllersController.php)中,我使用use IlluminateSupportFacadesGate导入了Gate Facade,然后在users.blade.php

@can('view-users') 
    <h2>Users</h2>
    <table class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Role</th>
            </tr>
        </thead>
        @if ($users)
        <tbody>
            @foreach ($users as $user)
            <tr>
                <td>{{ $user->first_name }} {{ $user->last_name }}</td>
                <td>{{ $user->email }}</td>
                <td>{{ $user->role }}</td>
            </tr>
            @endforeach
        </tbody>
        @endif
    </table>
@endcan 

问题

evan如果当前用户确实具有view-user权限(在导航栏中可见),则USERS表不存在,在AuthServiceProvider.php中执行dd(session('userPermissions'))将返回null

我的错误在哪里?

推荐答案

假定您已在AppProvidersAuthServiceProvider中注册了Policy&;Gate

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        User::class => UserPolicy::class,
    ];
 
    /**
     * Register any application authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        // this merely maps the name of the gate
        Gate::define('view-users', [UserPolicy::class, 'index']);
    }
}

CLI命令:

php artisan make:policy UserPolicy --model=User

然后在class UserPolicy中,您必须实现方法index()


用户甚至可以在呈现任何刀片模板之前签入控制器。

对于您的用例...请参阅以下示例:Methods Without ModelsGuest Users

Gate知道User $user时,依赖session()是没有意义的

这篇关于如何将会话变量引入到此Laravel 8应用程序的AuthServiceProvider中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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