在NovaServiceProvider
有:
protected function gate()
{
Gate::define('viewNova', function ($user) {
return in_array($user->email, [
'[email protected]',
]);
});
}
但我想做的只是允许我在config/auth
设置的管理员守卫的人访问Nova。来自网络守卫的所有用户在访问任何Nova URL时理想情况下都应获得404。
用于望远镜的This question似乎相似,但我似乎无法弄清楚我应该在哪里定义它,以及如何为网络守卫生成404。
一个可能相关的问题:viewNova
方法中的gate
究竟是什么意思?
config/auth
中为特定警卫定义具体行动吗? (我想我已经在某个地方见过这个但似乎无法找到它)?结帐vendor/laravel/nova/src/NovaApplicationServiceProvider.php
。它有一个名为authorization
的方法:
/**
* Configure the Nova authorization services.
*
* @return void
*/
protected function authorization()
{
$this->gate();
Nova::auth(function ($request) {
return app()->environment('local') ||
Gate::check('viewNova', [$request->user()]);
});
}
如果环境是本地的,它允许每个人访问面板,但如果环境是其他的,它会检查viewNova
方法的定义,并将$request->user()
传递给它。
在同一个文件中,有gate()
方法定义了viewNova
:
/**
* Register the Nova gate.
*
* This gate determines who can access Nova in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewNova', function ($user) {
return in_array($user->email, [
//
]);
});
}
基本上,这种方法什么都不做。您可以在app/Providers/NovaServiceProvider.php
中实现它(这是您在文件中看到的默认实现,并且您已经提到过)。在您的情况下,您可以这样实现它:
/**
* Register the Nova gate.
*
* This gate determines who can access Nova in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewNova', function ($user) {
Auth::guard('admin')->check();
});
}
如果当前经过身份验证的用户位于true
后卫,则返回admin
。希望我能回答你所有的问题。