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
苦しいTiDBへの移行を乗り越えて快適な運用を目指す
leveragestech
0
340
Java Webフレームワークの現状 / java web framework at burikaigi
kishida
9
2.2k
『GO』アプリ バックエンドサーバのコスト削減
mot_techtalk
0
140
CNCF Project の作者が考えている OSS の運営
utam0k
6
710
Software Architecture
hschwentner
6
2.1k
Flutter × Firebase Genkit で加速する生成 AI アプリ開発
coborinai
0
150
How mixi2 Uses TiDB for SNS Scalability and Performance
kanmo
35
14k
ASP. NET CoreにおけるWebAPIの最新情報
tomokusaba
0
360
Unity Android XR入門
sakutama_11
0
150
プログラミング言語学習のススメ / why-do-i-learn-programming-language
yashi8484
0
130
富山発の個人開発サービスで日本中の学校の業務を改善した話
krpk1900
4
380
GAEログのコスト削減
mot_techtalk
0
120
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.4k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.7k
Facilitating Awesome Meetings
lara
51
6.2k
A designer walks into a library…
pauljervisheath
205
24k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
366
25k
VelocityConf: Rendering Performance Case Studies
addyosmani
328
24k
Bash Introduction
62gerente
610
210k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
320
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]