Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Access Control in Laravel

Access Control in Laravel

Introduction to Access Control in Laravel

Fareez Ahamed

February 27, 2016
Tweet

More Decks by Fareez Ahamed

Other Decks in Programming

Transcript

  1. public function index() { //check access if(Gate::denies('view-post-list')) { abort(403); }

    $posts = Post::all(); return response()->json($posts); } denies allows check
  2. class AuthServiceProvider extends ServiceProvider { ... public function boot(GateContract $gate)

    { $this->registerPolicies($gate); //Definition of access control $gate->define('view-post-list', function ($user) { return $user->isModerator(); }); } }
  3. public function edit(Request $req, $id) { $post = Post::findOrFail($id); //check

    access if(Gate::denies('edit-post',$post)) { abort(403); } return response()->json($post); }
  4. class User extends Authenticatable { ... protected $casts = [

    'roles' => 'collection' ]; } Cast roles to Collection