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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Fareez Ahamed
February 27, 2016
Programming
260
4
Share
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
感情を設計する
ichimichi
5
1.4k
10 Tips of AWS ~Gen AI on AWS~
licux
5
320
TiDBのアーキテクチャから学ぶ分散システム入門 〜MySQL互換のNewSQLは何を解決するのか〜 / tidb-architecture-study
dznbk
1
160
煩雑なSkills管理をSoC(関心の分離)により解決する――関心を分離し、プロンプトを部品として育てるためのOSSを作った話 / Solving Complex Skills Management Through SoC (Separation of Concerns)
nrslib
4
860
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
180
KagglerがMixSeekを触ってみた
morim
0
370
SkillがSkillを生む:QA観点出しを自動化した
sontixyou
6
3.3k
Swift Concurrency Type System
inamiy
0
470
Claude Codeをカスタムして自分だけのClaude Codeを作ろう
terisuke
0
110
How Swift's Type System Guides AI Agents
koher
0
220
Don't Prompt Harder, Structure Better
kitasuke
0
690
今からFlash開発できるわけないじゃん、ムリムリ! (※ムリじゃなかった!?)
arkw
0
190
Featured
See All Featured
A Soul's Torment
seathinner
6
2.6k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.8k
Designing Experiences People Love
moore
143
24k
Mobile First: as difficult as doing things right
swwweet
225
10k
What does AI have to do with Human Rights?
axbom
PRO
1
2.1k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Raft: Consensus for Rubyists
vanstee
141
7.4k
Making Projects Easy
brettharned
120
6.6k
How to Ace a Technical Interview
jacobian
281
24k
RailsConf 2023
tenderlove
30
1.4k
Being A Developer After 40
akosma
91
590k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
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]