you can use Laravel Spatie. This package allows you to manage user permissions and roles in a database. it's simple and very fast ti implement. it's also allow you to manage permission on routes, controllers and blade.
One controller for different roles and query
Hello to all,
I am new in laravel and I still have many doubts. I present you my problem.
I'm building an app for the management of condominiums, the peculiarity and that each user will have different queries and if necessary different permissions (policy).
I'll give you a small example for what I mean by query + different views for each user:
the 'condominium' can read the table / model 'Bill' where it displays 'Data' and 'Cost' to be paid. the 'chief condominium' ( Always on the same table ) can have a broader vision, reading other data, perhaps more technical and if necessary make a JOIN with related tables.
In this case, what changes is the query that generates the results of the view.
How can I manage different queries based on different users roles ?
I tried some solutions but ...
-- FIRST SOLUTION: it was to create a single reference to the resource:
site.com/bill
where thanks to the management of permissions and roles defined in the database I could display, modify, delete etc ....
( Click here for SCHEMA DB ER: https://drive.google.com/file/d/1wWwLxc4qW2Cy97TaDN9IosKaHG9-Hs0z/view?usp=sharing )
but I can filter your user roles only through an IF. This does not make the code clean if for example I will add 10 roles :-(
-- SECOND SOLUTION:
To solve the problem of query + dedicated views for each user I have defined different routes for different controllers, like this:
site.com/condominiums/bill
site.com/chief-condominium/bill
site.com/admin/bill
site.com/.../bill
Now I can generate different queries + view but code maintenance is laborious.
What do you recommend?
Thank you so much for the support! Andrea
@clusteCode If you have a “CondominiumController” that fetches a condominium and displays different data depending on the role of the user logged in, then I think you want to have a way of switching the view rather than performing different queries.
You could use Laravel’s “Responsable” interface and add it to a view model that picks a view depending on the authenticated user’s role:
namespace App\Http\Controllers;
use App\Condominium;
use App\Http\Views\CondominiumView;
class CondominiumController extends Controller
{
public function show(Condominium $condominium)
{
return new CondominiumView($condominium);
}
}
namespace App\Http\Views;
use Illuminate\Contracts\Support\Responsable;
use Exception;
class CondominiumView implements Responsable
{
private $condominium;
public function __construct(Condominium $condominium)
{
$this->condominium = $condominium;
}
public function toResponse($request)
{
// If your User model has a property called role…
switch ($request->user()->role) {
case 'normal_user':
$path = 'condominium.show.normal_user';
break;
case 'admin':
$path = 'condominium.show.admin';
break;
default:
throw new Exception("No view for role [{$role}].");
}
return view($path, [
'condominium' => $this->condominium,
]);
}
}
So a user can view a single condominium, but the view they get to see depends on their role.
Please or to participate in this conversation.