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
130
Other Decks in Programming
See All in Programming
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.3k
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
17
4.3k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
300
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
550
Outline View in SwiftUI
1024jp
1
300
現場で役立つモデリング 超入門
masuda220
PRO
15
3.1k
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
320
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.2k
Jakarta EE meets AI
ivargrimstad
0
200
Dev ContainersとGitHub Codespacesの素敵な関係
ymd65536
1
140
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
250
C++でシェーダを書く
fadis
6
4k
Featured
See All Featured
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
400
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
505
140k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Visualization
eitanlees
145
15k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
42
2.2k
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]