As I mentioned in several posts, I was going to write about how to add a role system in our project, the first post I’ll write about this will be this one. I don’t know how many parts it will have, but it will have several.
Today we will add the necessary package to be able to manage the roles and permissions of our project with Laravel.
Package installation
We are going to install the Spatie’s laravel-permission. This installation guide is based on the package documentation.
First, download it:
1 |
composer require spatie/laravel-permission |
When it finishes, we have to add to the providers array of config/app.php this line:
1 2 3 4 |
'providers' => [ ... Spatie\Permission\PermissionServiceProvider::class, ]; |
Publish the migration and execute it:
1 2 3 |
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="migrations" php artisan migrate |
Publish the configuration file:
1 |
php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" --tag="config" |
And it would already be installed, we simply need to add these lines to the User.php model so we can start using it:
1 2 3 4 5 6 7 |
... use Spatie\Permission\Traits\HasRoles; class User extends Authenticatable { use HasRoles; ... |
Modification of seeders
Currently in our seeders aren’t considered roles and permissions, that the reason why we are going to generate some roles and permissions and assign to the users that are generated.
Let’s create seeders for roles and permissions:
1 2 |
php artisan make:seeder PermissionsTableSeedeer php artisan make:seeder RolesTableSeedeer |
Open the PermissionsTableSeeder.php and add this code, this is an example, you can add the permissions we want:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php use Illuminate\Database\Seeder; use Spatie\Permission\Models\Permission; class PermissionsTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { Permission::create(['name' => 'post_create']); Permission::create(['name' => 'post_edit']); Permission::create(['name' => 'post_delete']); Permission::create(['name' => 'comment_create']); } } |
Open the RolesTableSeeder.php and add this code, this is an example, we can add the roles we want and assign the permissions we want:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php use Illuminate\Database\Seeder; use Spatie\Permission\Models\Role; class RolesTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { $role = Role::create(['name' => 'administrator']); $role->givePermissionTo('post_create','post_edit', 'post_delete', 'comment_create'); $role = Role::create(['name' => 'user']); $role->givePermissionTo('comment_create'); } } |
And modify the seeder of the users:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php use Illuminate\Database\Seeder; use App\User; use App\Post; class UsersTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { factory(User::class, 5)->create()->each(function($u) { if ($u->id == 1) { $u->assignRole('administrator'); } else { $u->assignRole('user'); } }); factory(Post::class, 8)->create(['user_id'=>1]); } } |
We are generating 5 users. For the first user generated, we assign the role of administrator and the others the user role. Then, we generate 8 posts that are associated with the user with id number 1 (the administrator).
Modify DatabaseSeeder.php :
1 2 3 |
$this->call(PermissionsTableSeeder::class); $this->call(RolesTableSeeder::class); $this->call(UsersTableSeeder::class); |
Refresh the database (delete all content) and execute the seeders with this command:
1 |
php artisan migrate:refresh --seed |
Now we are going to assign new users the user role. Open RegisterController.php and modify the create method:
1 2 3 4 5 6 7 8 9 10 11 12 |
protected function create(array $data) { $user = User::create([ 'name' => $data['name'], 'email' => $data['email'], 'country' => $data['country'], 'password' => bcrypt($data['password']), ]); $user->assignRole('user'); return $user; } |