Hello Laravelers 👋,
I need feedback from you to check what I've done to manage access to a platform I'm developing based on roles.
I've got the impression I've got something a bit messy (maybe not?) but I'm not sure how to simplify it.
So in my case, I have 4 roles:
webmaster, admin, pro, private.
When you go to the basic URL, you log in and depending on your role, you're redirected to the right page. Note that if the role is equal to 3, you go through an API to check that the account is active before connecting the person.
So, here's what I did in my HomeController:
public function index(GetClientInfoAction $getClientInfoAction)
{
// Check if the user is authenticated
if (!auth()->check()) {
// If not authenticated, redirect to the login page
return view('auth.login');
}
// Fetch the role of the authenticated user
$userRole = auth()->user()->roles()->first();
// Switch based on the user role ID
switch ($userRole['id']) {
// Webmaster role (Page for accessing logs, clearing the cache, putting the site into maintenance, etc.)
case 1:
// Check if the application is down for maintenance
if (app()->isDownForMaintenance()) {
$token = Maintenance::first()->token;
return view('webmaster.home', ['token' => $token]);
}
return view('webmaster.home');
// Admin role (Page for managing users, roles, etc.)
case 2:
// Fetch latest 5 users having roles with ID 3 or 4
$users = User::latest()->whereHas('roles', function ($q): void {
$q->where('id', 3)->orWhere('id', 4);
})->take(5)->get();
// Count total users and total excel orders
$countUsers = (new \App\Models\User())->countUsers();
$countOrder = count(ExcelFile::all());
return view('admin.index', ['users' => $users, 'cntUsers' => $countUsers, 'cntOrder' => $countOrder]);
// Pro role (Pro customer page)
case 3:
$excel = ExcelFile::all();
$ApiID = auth()->user()->api->identifier;
// Check if ApiID is set
if (isset($ApiID)) {
// Execute the GetClientInfoAction for the given traplusID
$response = $getClientInfoAction->execute($ApiID);
// Handle the possible response scenarios
if (!$response) {
$error = __("alerts.error.Your account does not exist or has been deactivated");
Auth::guard('web')->logout();
return redirect()->route('login')->withErrors($error);
} elseif (true !== $response) {
$error = __("alerts.error.Communication problem with our API, please try again later");
Auth::guard('web')->logout();
return redirect()->route('login')->withErrors($error);
}
return view('pro.home', ['excel' => $excel, 'fileFormat' => null]);
}
break;
// Individual role (Individual customer page)
case 4:
return view('individual.home');
}
}
Does this seem like a good approach to you?
I also have another small question. I often get a [production] error when testing the platform in the browser (even though my .env is local) telling me that the encryption key doesn't exist when I log in/out and switch accounts. This forces me to clear my cache each time, but I can't quite work out where it's coming from. I haven't touched the Breeze authentication system.
Thanks for your help and feedback 😇