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
260
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
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
210
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
120
Why Laravel apps break—Mastering the fundamentals to keep them maintainable
kentaroutakeda
1
350
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
110
AI時代の仕事技芸論 — ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ
kuranuki
2
660
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
160
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
220
TAKTでAI駆動開発の品質を設計する
j5ik2o
6
1.2k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
570
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
190
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
320
A2UI という光を覗いてみる
satohjohn
1
130
Featured
See All Featured
WCS-LA-2024
lcolladotor
0
630
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
22k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
Leo the Paperboy
mayatellez
7
1.8k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
160
StorybookのUI Testing Handbookを読んだ
zakiyama
31
6.8k
Thoughts on Productivity
jonyablonski
76
5.2k
Raft: Consensus for Rubyists
vanstee
141
7.5k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.9k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.5k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
530
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]