當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Gate::before方法代碼示例

本文整理匯總了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;
         }
     });
 }
開發者ID:christhompsontldr,項目名稱:laraboard,代碼行數:100,代碼來源:AuthServiceProvider.php


注:本文中的Illuminate\Support\Facades\Gate::before方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。