Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Access Control in Laravel
Search
Fareez Ahamed
February 27, 2016
Programming
4
260
Access Control in Laravel
Introduction to Access Control in Laravel
Fareez Ahamed
February 27, 2016
Tweet
Share
More Decks by Fareez Ahamed
See All by Fareez Ahamed
Laravel Integration Testing
fareez
3
140
Other Decks in Programming
See All in Programming
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
190
今から始めるClaude Code超入門
448jp
8
8.9k
Raku Raku Notion 20260128
hareyakayuruyaka
0
330
dchart: charts from deck markup
ajstarks
3
990
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
310
izumin5210のプロポーザルのネタ探し #tskaigi_msup
izumin5210
1
130
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
650
AI巻き込み型コードレビューのススメ
nealle
2
410
AIと一緒にレガシーに向き合ってみた
nyafunta9858
0
240
FOSDEM 2026: STUNMESH-go: Building P2P WireGuard Mesh Without Self-Hosted Infrastructure
tjjh89017
0
170
ぼくの開発環境2026
yuzneri
0
240
AI & Enginnering
codelynx
0
110
Featured
See All Featured
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
How GitHub (no longer) Works
holman
316
140k
Documentation Writing (for coders)
carmenintech
77
5.3k
Being A Developer After 40
akosma
91
590k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
62
Between Models and Reality
mayunak
1
190
Testing 201, or: Great Expectations
jmmastey
46
8k
We Have a Design System, Now What?
morganepeng
54
8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.2k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
94
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.4k
Transcript
Access Control in Laravel
Access Control in Laravel
What is Access Control?
ACL 403 200
How to do that in Laravel?
public function index() { //check access if(Gate::denies('view-post-list')) { abort(403); }
$posts = Post::all(); return response()->json($posts); } denies allows check
But, where do I define the 'ability'?
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(); }); } }
But I wan't to give access selectively
public function edit(Request $req, $id) { $post = Post::findOrFail($id); //check
access if(Gate::denies('edit-post',$post)) { abort(403); } return response()->json($post); }
Now, How to define this?!
$gate->define('edit-post', function ($user, $post){ return $user->id === $post->user_id; });
What if I'm a Super Admin?
$gate->before(function ($user, $ability){ if ($user->isSuperAdmin()) { return true; } });
How to log failed Gate checks?
$gate->after( function ($user, $ability, $result, $arguments){ if (!$result) { //Log
here } });
Cleaner way to define abilities
$gate->define('update-post', 'Class@method'); Defining in classes
php artisan make:policy PostPolicy Defining Policies
protected $policies = [ Post::class => PostPolicy::class, ]; Advantages Cleaner
Code Implicitly identifies Policy to use
Blade 'can'!
@can('edit-post', $post) <a href='{{ url('post.edit',$post->id) }}'>Edit Post</a> @else <a class='disabled'
href='{{ url('post.edit',$post->id) }}'> Edit Post </a> @endcan
Simple Implementation
Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('email')->unique(); $table->string('password'); $table->string('roles');
$table->rememberToken(); $table->timestamps(); }); Add roles to user
class User extends Authenticatable { ... protected $casts = [
'roles' => 'collection' ]; } Cast roles to Collection
$gate->define('create-post', function($user){ return $user->roles->contains('author'); }); Define the abilities
Now 'Gate!!!' public function create(Request $req) { //check access if(Gate::denies('create-post'))
{ abort(403); } return view('post.create'); }
Thank you! www.fareez.info
[email protected]