I normally use authorization to control who can see and do things, but groups also work. Spatie uses laravel's authorization anyway, so just study how it's done there and implement yourself.
Can't use Spatie Laravel-Permission, is my implementation OK for my basic needs?
I can't use Spatie's package because of business requirements
but what I need to do is to protect routes in web.php against certain roles.
Currently the design seem to be wrong:
There is only 1 table called permission_groups, which is simply a relationship between a user_id and a permission_name:
user_id | permission_name
Then the following code is used to create the role middleware:
<?php
namespace App\Http\Middleware;
use Closure;
class EnsureUserHasRole
{
public function handle($request, Closure $next, $role)
{
if (! $request->user()->hasRole($role)) {
// Redirect...
}
return $next($request);
}
}
( Source: https://laravel.com/docs/9.x/middleware#middleware-parameters )
So with the current design, what's happening is that the name of the permission turns to also be the name of the role (So every user that is assigned this permission also has this "role")
I was thinking to add another table - roles table, and rename the above table to permissions.
Then, I will make a relationship between the user_id and the role_name instead of the permission.
And in the permissions table I will change the relationship to be between the permission_name and role:
//table 1:
permission_id | permission_name
//table 2:
role | permission_id
//table3:
user_id | role
Then in the web.php I will protect routes like so:
Route::middleware(['role:managers,users'])->group(function () {
});
Route::middleware(['role:admin'])->group(function () {
});
Would that be better than the current design? Or any other suggestions?
Thanks
Please or to participate in this conversation.