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
270
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Access Control in Laravel
Introduction to Access Control in Laravel
Fareez Ahamed
February 27, 2016
More Decks by Fareez Ahamed
See All by Fareez Ahamed
Laravel Integration Testing
fareez
3
140
Other Decks in Programming
See All in Programming
ソフトウェアラスタライザ
fadis
1
470
今さら聞けない .NET CLI
htkym
0
210
使いながら育てる Claude Code — 開発フローの1コマンド化 × 繰り返し指摘の自動仕組み化
shiki_kakaku
0
1.9k
VibeCodingからAgenticWorkflowへ
starfish719
0
760
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
200
私のClaude Code活用法 (個人開発編) - PHPerKaigi mini #4(2026/08/24)
panda_program
1
100
Oxlintはいいぞ(続)
yug1224
1
390
メールのエイリアス機能を履き違えない
isshinfunada
0
250
React本体のコードリーディング
high_g_engineer
1
150
Loosening the Reins: Go Generics Get More Flexible
kuro_kurorrr
0
250
freeeにおけるEvalsの実践例の紹介
freee
PRO
0
140
tsc.rip を支える技術 / Kyoto.なんか #8
susisu
0
120
Featured
See All Featured
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
550
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
220
Music & Morning Musume
bryan
47
7.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.4k
How to build a perfect <img>
jonoalderson
1
5.9k
Context Engineering - Making Every Token Count
addyosmani
9
1.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
300
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
280
Exploring anti-patterns in Rails
aemeredith
3
470
How to Think Like a Performance Engineer
csswizardry
28
2.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]