Finally, in this course, we will take care of permission. In this lesson, we will take care of the back-end part. We will create Role and Permission models. Then we will add two roles: admin and editor. The admin role will be able to do everything and the editor will not be able to delete the posts.

First, we will create the models with migrations for roles and permissions.
php artisan make:model Role -mphp artisan make:model Permission -m
database/migrations/xxxx_create_roles_table.php:
public function up(): void{ Schema::create('roles', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); });}
app/Models/Role.php:
class Role extends Model{ protected $fillable = ['name'];}
database/migrations/xxxx_create_permissions_table.php:
public function up(): void{ Schema::create('permissions', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); });}
app/Models/Permission.php:
class permissions extends Model{ protected $fillable = ['name'];}
Next, we need to create a pivot table for...
According to this tutorial we are defining the
Gatesbut we are not using them in the controllers (or routes) to allow/prohibit the actions.My solution in
app/Http/Controllers/Api/PostController.php:You are right. Database tables permissions, permission_role and role_user are correctly populated. But without these lines all users can delete posts.
foreachClosing bracket}is missing inapp/Providers/AuthServiceProvider.phpintobootmethod.As a respected ambassador of programming principles in general and KISS/YAGNI in particular, I must say that this is too complicated for this limited functionality :)
I would limit myself to the role column in the users table. Casting it to App\Enums\Role
And the policy App\Policies\PostPolicy
But that is a completely different approach from our example.
In your code, you are simply saying that user can have only one role, while in our example - we allow multiple roles. Then we are looking at your "KISS/YAGNI" example of a policy and it's hard-coded per role. What if our user has role
Admin, but they are just learning and should not have ability to delete the user? I can't handle that case with your example.Or to shorten what I just said - there are millions of ways to build the same thing. It just boils down to the project needs and future expansions
So, it all boils down to necessity. In this case, the functionality is excessive for CRUD operations for individual roles. The pinciples and approaches to software development advocate for keeping code simple and avoiding unnecessary functionality that is redundant.
In a way, you are right. This indeed goes deeper than it should. But one thing we learned over the years of building roles/permissions systems - they change rapidly. And preparing a system like this (or just installing spatie permissions package) is a great way to have the basics covered :) It is not that complex to understand and work with, yet provides you with a good base for future changes.