Skip to content

Instantly share code, notes, and snippets.

@fabianmu
Created November 27, 2015 12:05
Show Gist options
  • Save fabianmu/f76bf430ddeb066b1a88 to your computer and use it in GitHub Desktop.
Save fabianmu/f76bf430ddeb066b1a88 to your computer and use it in GitHub Desktop.
laravel role based permission acl sample
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\App;
use App\Role;
use App\Permission;
use App\User;
class RoleAndPermissionSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// create roles
$manager = factory(Role::class)->create([
'name' => 'manager',
'label' => 'Manager'
]);
// create permissions
$listEditors = factory(Permission::class)->create([
'name' => 'listEditors',
'label' => 'View all Editor'
]);
$createEditor = factory(Permission::class)->create([
'name' => 'createEditor',
'label' => 'Create an Editor'
]);
// assign permissions to roles
$manager->givePermissionTo($listEditors);
$managerUser = factory(User::class)->create([
'first_name' => 'Tinker',
'last_name' => 'Bell',
'email' => '[email protected]',
'password' => bcrypt('password')
]);
$managerUser->assignRole('manager');
}
}
@can('listEditors')
<h4>Editors:</h4>
<ul>
<li {{ Request::is('users') ? 'class="active"' : '' }}>
<a href="{{ action('CMS\UserController@index', ['roleName' => 'editor']) }}">All Editors</a>
</li>
@can('createEditor')
<li {{ Request::is('users/create') ? 'class="active"' : '' }}>
<a href="{{ action('CMS\UserController@create', ['roleName' => 'editor']) }}">Add an editor</a>
</li>
@endcan
</ul>
@endcan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment