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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
CSC307 Lecture 09
javiergs
PRO
1
840
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.3k
高速開発のためのコード整理術
sutetotanuki
1
400
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
270
プロダクトオーナーから見たSOC2 _SOC2ゆるミートアップ#2
kekekenta
0
220
Oxlintはいいぞ
yug1224
5
1.3k
AtCoder Conference 2025
shindannin
0
1.1k
Amazon Bedrockを活用したRAGの品質管理パイプライン構築
tosuri13
5
750
AIによるイベントストーミング図からのコード生成 / AI-powered code generation from Event Storming diagrams
nrslib
2
1.9k
360° Signals in Angular: Signal Forms with SignalStore & Resources @ngLondon 01/2026
manfredsteyer
PRO
0
130
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
190
AI Schema Enrichment for your Oracle AI Database
thatjeffsmith
0
310
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.3k
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
320
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
68
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
60
42k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.6k
WENDY [Excerpt]
tessaabrams
9
36k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
370
Darren the Foodie - Storyboard
khoart
PRO
2
2.4k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
430
Utilizing Notion as your number one productivity tool
mfonobong
3
220
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
180
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.7k
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]