将自定义角色分配给控制器Laravel Voyager

发布于 2025-02-13 20:39:22 字数 1084 浏览 0 评论 0原文

我在Laravel Voyager仪表板上创建了一个自定义页面,但是在限制其访问权限时遇到了困难。 使用Voyager文档:

我在权限表中创建密钥“ browse_sendusermails”,并链接到管理员角色

路线

Route::get('/sendusermails', [App\Http\Controllers\SendUserMailsController::class, 'index'])->middleware('admin.user');

控制器

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;

class SendUserMailsController extends Controller
{
  protected function guard()
  { return Auth::guard(app('VoyagerGuard')); }
    public function index()
    {
        $this->authorize('browse_sendusermails');
        return view('logs.logs');
    }
}

但是当我单击 http://127.0.0.1:8000/admin/sendusermails 它总是显示403此操作未经授权。

我还能做什么来为控制器分配自定义角色?

$ this->授权('sendusermails');也不起作用。

当然,当我删除$ this->授权('browse_sendusermails')页面加载而没有问题

I created a custom page in the Laravel Voyager dashboard but I'm having trouble restricting its access rights.
Using the voyager documentation:

I create key "browse_sendusermails" in permissions table and linked to the admin role

Route

Route::get('/sendusermails', [App\Http\Controllers\SendUserMailsController::class, 'index'])->middleware('admin.user');

Controller

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;

class SendUserMailsController extends Controller
{
  protected function guard()
  { return Auth::guard(app('VoyagerGuard')); }
    public function index()
    {
        $this->authorize('browse_sendusermails');
        return view('logs.logs');
    }
}

But when i click http://127.0.0.1:8000/admin/sendusermails
It always show 403 This action is unauthorized.

What else can I do to assign custom roles to the controller?

$this->authorize('sendusermails'); also doesn't work.

Of course when I delete $this->authorize('browse_sendusermails') the page loads without a problem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

居里长安 2025-02-20 20:39:22

您只需要在AppServiceProvider文件中添加门

use Illuminate\Support\Facades\Gate;

class AppServiceProvider {
    public function
    boot()
    {
        Gate::define('browse_sendusermails', function ($user) {
           return $user->hasPermission('browse_sendusermails');
        });
    }
}

You just need to add the gate in AppServiceProvider file

use Illuminate\Support\Facades\Gate;

class AppServiceProvider {
    public function
    boot()
    {
        Gate::define('browse_sendusermails', function ($user) {
           return $user->hasPermission('browse_sendusermails');
        });
    }
}
自控 2025-02-20 20:39:22

对于$ this->授权('browse_sendusermails');作品您需要第二个参数作为模型的实例:

$this->authorize('browse', new SendUserMails());

但是,如果您有一个自定义门,

Gate::define('browse_create_bill', function ($user) {
   return $user->hasPermission(`browse_create_bill`);
});

则不需要以下方式将模型作为第二个参数传递。

如果您在基本控制器中工作(作为您的情况),此外还将有效:

$canView = Auth::user()->can('read', new SendUserMails());

for $this->authorize('browse_sendusermails'); works you need a second param as a instance of model like:

$this->authorize('browse', new SendUserMails());

But if you have a custom Gate like:

Gate::define('browse_create_bill', function ($user) {
   return $user->hasPermission(`browse_create_bill`);
});

Is not need pass a model as second argument.

Additional if you working in a basic controller (as your case), with this will work:

$canView = Auth::user()->can('read', new SendUserMails());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文