代码之家  ›  专栏  ›  技术社区  ›  Razvan Zamfir

在这个Laravel 8项目中,如何将会话变量引入AuthServiceProvider?

  •  0
  • Razvan Zamfir  · 技术社区  · 4 年前

    我正在做一件事 拉维尔8号 具有用户、角色和权限的应用程序。我用 微软Azure 用于用户注册和登录。我从下面开始 this tutorial 在他们的网站上。

    我在中使用自定义中间件 routes\web.php 要区分经过身份验证的用户和来宾:

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

    我有一张 逗号分隔的用户权限 特定于每个用户角色,从 permissions MySQL表。

    我存储 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') )是使用用户在Gates中的权限。为此,在 app\Providers\AuthServiceProvider.php 我有:

    // More code
    use Illuminate\Support\Facades\Gate;
    // More code
    
    /* View Users */
    Gate::define('view-users', function() {
      return in_array('view-users', session('userPermissions'));
    });
    

    在基本控制器中( app\Http\Controllers\Controller.php )我已经把大门的正面用 use Illuminate\Support\Facades\Gate 然后,在 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 
    

    问题

    如果当前用户没有 查看用户 权限(在导航栏中可见),用户表不存在并且正在执行 dd(session('userPermissions')) 在里面 AuthServiceProvider.php 返回 null .

    我的错在哪里?

    0 回复  |  直到 4 年前
        1
  •  1
  •   Martin Zeitler    4 年前

    假设你有 Policy & Gate 注册于 App\Providers\AuthServiceProvider .

    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 Models Guest Users .

    依靠别人是没有意义的 session() ,当 大门 知道 User $user ...

    推荐文章