本文整理汇总了PHP中Illuminate\Support\Facades\Gate::before方法的典型用法代码示例。如果您正苦于以下问题:PHP Gate::before方法的具体用法?PHP Gate::before怎么用?PHP Gate::before使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Support\Facades\Gate
的用法示例。
在下文中一共展示了Gate::before方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: boot
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
// admins are gods
Gate::before(function ($user, $ability) {
// if no Laratrust role is configured, nobody is admin
if (!is_string(config('laraboard.user.admin_role'))) {
return false;
}
// ignore for these abilities
if (!in_array($ability, ['laraboard::thread-subscribe', 'laraboard::thread-unsubscribe'])) {
if (!is_null($user) && $user->hasRole(config('laraboard.user.admin_role'))) {
return true;
}
}
});
// reply edit
Gate::define('laraboard::reply-edit', function ($user, $post) {
if ($post->status != 'Open') {
return false;
}
return $user->id === $post->user_id;
});
// reply delete
Gate::define('laraboard::post-delete', function ($user, $post) {
if ($post->status != 'Open') {
return false;
}
return $user->id === $post->user_id;
});
// thread-reply
Gate::define('laraboard::thread-reply', function ($user, $post) {
if (!$post->is_open) {
return false;
}
return \Auth::check();
});
// thread-subscribe
Gate::define('laraboard::thread-subscribe', function ($user, $thread) {
if (\Auth::check()) {
// only if they aren't already subscribed
if (!$user->forumSubscriptions->contains('post_id', $thread->id)) {
return true;
}
}
});
// thread-unsubscribe
Gate::define('laraboard::thread-unsubscribe', function ($user, $thread) {
if (\Auth::check()) {
// only if they aren't already subscribed
if ($user->forumSubscriptions->contains('post_id', $thread->id)) {
return true;
}
}
});
// thread-create
Gate::define('laraboard::thread-create', function ($user, $board) {
if ($board->status != 'Open') {
return false;
}
return \Auth::check();
});
// category-create
Gate::define('laraboard::category-manage', function ($user) {
// only admins
return false;
});
// board-create
Gate::define('laraboard::board-create', function ($user, $board) {
if ($board->status != 'Open') {
return false;
}
// return \Auth::check();
});
// board-edit
Gate::define('laraboard::board-edit', function ($user, $board) {
return false;
});
// forum-create
Gate::define('laraboard::forum-create', function ($user) {
return false;
});
// forum-edit
Gate::define('laraboard::forum-edit', function ($user, $category) {
return false;
});
Gate::define('laraboard::post-edit', function ($user, $post) {
if (!in_array($post->type, ['Post', 'Thread'])) {
return false;
}
if ($user->id == $post->user_id) {
return true;
}
});
}