如何在laravel 7 Gate中允许多个用户访问同一级别

wooyq4lh  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(125)

我想创建一个功能,允许多个用户在AuthServiceProvider.php相同的访问级别,我已经尝试使用&请为我提供这个问题的解决方案,
这是我的AuthServiceProvider.php

<?php

namespace App\Providers;

use Laravel\Passport\Passport;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        // 'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        // * Laravel ACL
        // ! Admin
        Gate::define('isAdmin', function ($user) {
            return $user->type === 'admin';
        });
        // ! Admin and author
        Gate::define('isAdminOrAuthor', function ($user) {
            return $user->type === 'admin' && 'author';
        });

        // ! Author
        Gate::define('isAuthor', function ($user) {
            return $user->type === 'author';
        });

        // ! User
        Gate::define('isUser', function ($user) {
            return $user->type === 'user';
        });

        Passport::routes();

        //
    }
}

字符串
这就是我如何尝试给予权限的管理员和作者

// ! Admin and author
    Gate::define('isAdminOrAuthor', function ($user) {
        return $user->type === 'admin' && 'author';
    });


这不起作用,我只是想知道我如何才能允许相同的用户级别下的isAdminOrAuthor名称的两个用户。
这里的地方,我需要允许管理员和作者查看

@can('isAdmin')
    <li class="nav-item">
     <router-link to="users" class="nav-link ">
       <i class="fas fa-users nav-icon text-blue"></i>
           <p>Users</p>
           </router-link>
      </li>
 @endcan


我想将单词isAdmin更改为isAdminOrAuthor,以便用户都可以访问它

uubf1zoe

uubf1zoe1#

大门

Gate::define('isAdminOrAuthor', function ($user) {
    return $user->type === 'admin' || $user->type === 'author';
});

字符串

查看

@can('isAdminOrAuthor')
    <!-- Content visible to users with the 'isAdminOrAuthor' permission -->
@endcan

相关问题