Steve Grunwell@stevegrunwell
Play along at home: stevegrunwell.github.io/laravel-authentication-made-easy
Easiest to do on a fresh installation of Laravel (the User model's usually pretty important)
Full instructions on GitHub:https://github.com/Zizaco/confide
"require": { "laravel/framework": "4.2.*", + "zizaco/confide": "~4.0@dev" },
Then, of course, run:
$ composer update
In app/config/app.php:
'providers' => array( ... 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', + 'Zizaco\Confide\ServiceProvider', ), 'aliases' => array( ... 'Validator' => 'Illuminate\Support\Facades\Validator', 'View' => 'Illuminate\Support\Facades\View', + 'Confide' => 'Zizaco\Confide\Facade', ),
Generate a default users table by running:
# Let Confide generate a migration that creates the users table $ php artisan confide:migration # Run any available migrations $ php artisan migrate
use Zizaco\Confide\ConfideUser; use Zizaco\Confide\ConfideUserInterface; class User extends Eloquent implements ConfideUserInterface { use ConfideUser; }
# Generates UsersController $ php artisan confide:controller # Appends default routes to app/routes.php $ php artisan confide:routes
If you've installed a new package or changed config and things aren't updating, this is usually a good first step:
$ composer dump-autoload
Visit /users/create to see the (very generic) registration page!
Great, so users can register on your app, but what can they do?
If you're familiar with WordPress Roles and Capabilities, this should be familiar!
# app/models/Role.php <?php use Zizaco\Entrust\EntrustRole; class Role extends EntrustRole { }
# app/models/Permission.php <?php use Zizaco\Entrust\EntrustPermission; class Permission extends EntrustPermission { }
<?php use Zizaco\Confide\ConfideUser; use Zizaco\Confide\ConfideUserInterface; class User extends Eloquent implements ConfideUserInterface { - use ConfideUser; + use ConfideUser, HasRole; }
$assemble = new Permission; $assemble->name = 'assemble_blocks'; $assemble->display_name = 'Assemble blocks'; $assemble->save();
$dream = new Permission; $dream->name = 'dream_big'; $dream->display_name = 'Dream BIG!'; $dream->save();
$construction_worker = new Role; $construction_worker->name = 'Average construction worker'; $construction_worker->save(); $construction_worker->perms()->sync([$assemble->id]);
$master_builder = new Role; $master_builder->name = 'Master Builder'; $master_builder->save(); $master_builder->perms()->sync([$assemble->id, $dream->id]);
$user = User::where('username', '=', 'emmet')->first(); // Attach a specific ID $user->roles()->attach($master_builder->id); // OR... // attachRole() can take a Role object, array, or ID $user->attachRole($master_builder);
Remember: a user can have many roles!
$ php artisan db:seed --class=PermissionSeeder
class PermissionSeeder extends Seeder { // Track all the permissions you use in your app... $permissions = [ 'assemble_blocks' => 'Assemble blocks', 'dream_big' => 'Dream BIG!' ]; // And the permissions $roles = [ 'Average construction worker' => ['assemble_blocks'], 'Master Builder' => ['assemble_blocks', 'dream_big'] ]; ... }
class PermissionSeeder extends Seeder { ... // Create any new permissions and store their IDs for later $permission_ids = Permission::lists('id', 'slug'); foreach ($permissions as $slug => $name) { if (! in_array($slug, $permission_ids)) { $perm = new Permission; $permission->name = $slug; $permission->display_name = $name; $permission->save(); $permission_ids[$slug] = $permission->id; } } ... }
class PermissionSeeder extends Seeder { ... // Attach permissions to roles and create roles that don't exist yet foreach ($roles as $name => $perms) { $role_permissions = array_intersect_key($perms, $permission_ids); $role = Role::where('name', '=', $name)->first(); if (! $role) { $role = new Role $role->name = $name; $role->save(); } $role->perms()->sync($role_permissions); } }
// Check Roles... $user->hasRole('Average contruction worker'); // false $user->hasRole('Master Builder'); // true // ...or Permissions $user->can('assemble_blocks'); // true $user->can('build_spaceship'); // false
// Checks if the user has *any* of these $user->ability( ['Master Builder', 'Micro-manager'], // Roles ['assemble_blocks','dream_big'] // Permissions );
// Use the third argument to check for *all* // the specified roles + permissions $user->ability( ['Master Builder', 'Micro-manager'], ['assemble_blocks','dream_big'], ['validate_all' => true] );
// Restrict the Master Builders section Entrust::routeNeedsRole('master-builders/*', 'Master Builder'); // Restrict a route to users with a given permission Entrust::routeNeedsPermission('assembly-line', 'assemble_blocks'); // Only let users who can assemble_blocks AND be_evil in Entrust::routeNeedsPermission( 'president-business-tower/*', ['assemble_blocks', 'be_evil'] );
Instead of just throwing a 403, you can redirect the users:
// Only let users who can assemble_blocks AND be_evil in Entrust::routeNeedsPermission( 'president-business-tower/*', ['assemble_blocks', 'be_evil'], Redirect::to('real-world') // any response object works here );
$ php artisan config:publish zizaco/confide $ php artisan config:publish zizaco/entrust
Steve Grunwellstevegrunwell.com@stevegrunwell
Packages:github.com/Zizaco/confidegithub.com/Zizaco/entrust
Slides:stevegrunwell.github.io/laravel-authentication-made-easy
Ohio Laravelwww.meetup.com/Ohio-Laravel/
Columbus PHPwww.meetup.com/phpphp/