当前位置: 首页>>代码示例>>PHP>>正文


PHP Gate::define方法代码示例

本文整理汇总了PHP中Illuminate\Support\Facades\Gate::define方法的典型用法代码示例。如果您正苦于以下问题:PHP Gate::define方法的具体用法?PHP Gate::define怎么用?PHP Gate::define使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Illuminate\Support\Facades\Gate的用法示例。


在下文中一共展示了Gate::define方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: boot

 /**
  * Register any authentication / authorization services.
  *
  * @return void
  */
 public function boot()
 {
     $this->registerPolicies();
     Gate::define('administer', function (User $user) {
         return $user->roles->contains('name', 'admin');
     });
 }
开发者ID:hughgrigg,项目名称:ching-shop,代码行数:12,代码来源:AuthServiceProvider.php

示例2: boot

 /**
  * Register any authentication / authorization services.
  *
  * @return void
  */
 public function boot()
 {
     $this->registerPolicies();
     foreach ($this->getPermissions() as $permission) {
         Gate::define($permission->name, function ($user) use($permission) {
             return $user->hasRole($permission->roles);
         });
     }
 }
开发者ID:nayed,项目名称:lab,代码行数:14,代码来源:AuthServiceProvider.php

示例3: isAdmin

 /**
  * Define abilities that checks if the current user is admin.
  *
  * @param  array  $arguments
  * @return boolean
  */
 private function isAdmin($arguments)
 {
     foreach ($arguments as $resource => $actions) {
         foreach ($actions as $action) {
             Gate::define($this->ability($action, $resource), function ($user) {
                 return $user->is_admin;
             });
         }
     }
 }
开发者ID:omarelgabry,项目名称:lumen-api-oauth,代码行数:16,代码来源:AuthServiceProvider.php

示例4: boot

 /**
  * Register any authentication / authorization services.
  *
  * @return void
  */
 public function boot()
 {
     $this->registerPolicies();
     Gate::define('create', function ($user) {
         if ($user->id) {
             return true;
         } else {
             return false;
         }
     });
 }
开发者ID:mazedlx,项目名称:minifig,代码行数:16,代码来源:AuthServiceProvider.php

示例5: boot

 /**
  * Boot the authentication services for the application.
  *
  * @return void
  */
 public function boot()
 {
     // Here you may define how you wish users to be authenticated for your Lumen
     // application. The callback which receives the incoming request instance
     // should return either a User instance or null. You're free to obtain
     // the User instance via an API token or any other method necessary.
     Auth::viaRequest('api', function (Request $request) {
         $authorization_header = explode(' ', $request->header('Authorization'));
         if (count($authorization_header) != 2 || strpos($authorization_header[0], 'Bearer')) {
             throw new Exception('Authorization header not set or invalid.');
         }
         $user = User::where('api_token', $authorization_header[1])->first();
         if (is_null($user)) {
             throw new Exception('Invalid access token.');
         }
         return $user;
     });
     // Event Authorization
     Gate::define('create-event', function (User $user) {
         return $user->hasPermission('create-event');
     });
     Gate::define('update-event', function (User $user, Event $event) {
         return $user->hasPermission('update-event') && $user->id === $event->user_id;
     });
     Gate::define('delete-event', function (User $user, Event $event) {
         return $user->hasPermission('delete-event') && $user->id === $event->user_id;
     });
     Gate::define('view-event', function (User $user, Event $event) {
         return $user->hasPermission('view-event');
     });
     Gate::define('list-event', function (User $user) {
         return $user->hasPermission('list-event');
     });
     // User Authorization
     Gate::define('list-user', function (User $user) {
         return $user->hasPermission('list-user');
     });
     Gate::define('view-user', function (User $user, User $user_check) {
         return $user->hasPermission('view-user');
     });
     // User Location Authorization
     Gate::define('list-user-location', function (User $user) {
         return $user->hasPermission('list-user-location');
     });
     Gate::define('update-user-location', function (User $user, User $user_check) {
         return $user->hasPermission('update-user-location') && $user->id === $user_check->id;
     });
 }
开发者ID:teladan48,项目名称:restful-server,代码行数:53,代码来源:AuthServiceProvider.php

示例6: boot

 /**
  * Boot the authentication services for the application.
  *
  * @return void
  */
 public function boot()
 {
     // Here you may define how you wish users to be authenticated for your Lumen
     // application. The callback which receives the incoming request instance
     // should return either a User instance or null. You're free to obtain
     // the User instance via an API token or any other method necessary.
     $user = null;
     $this->app['auth']->viaRequest('api', function ($request) {
         if ($request->header("AuthToken")) {
             $tk = Token::where('api_token', $request->header("AuthToken"))->first();
             return User::where('id', $tk->user_id)->first();
         }
     });
     // Authorises the current user for particular requests
     Gate::define('getUser', function ($user, $userid) {
         // TODO allow user to get users matched with them
         return $user->id == $userid;
     });
     Gate::define('deleteUser', function ($user, $userid) {
         // TODO allow user to get users matched with them
         return $user->id == $userid;
     });
 }
开发者ID:GoAwayPeter,项目名称:MatchiServer,代码行数:28,代码来源:AuthServiceProvider.php

示例7: test_closure_permission_fails

 public function test_closure_permission_fails()
 {
     $user = $this->createUser(['name' => 'John Doe']);
     $create = new Permission();
     $create->name = 'create-post';
     $create->label = 'Create Post';
     $create->closure = function ($user, $id, $otherParameter) {
         return $user->id == $id;
     };
     $create->save();
     // Stub the service provider defined ability.
     Gate::define($create->name, $create->closure);
     $this->assertTrue($user->can('create-post', [1, 'other-parameter']));
     $this->setExpectedException(\ErrorException::class);
     // Missing argument three.
     $user->can('create-post', [1]);
 }
开发者ID:larapacks,项目名称:authorization,代码行数:17,代码来源:AuthorizationTest.php

示例8: 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::define方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。