92 lines
2.8 KiB
PHP
Executable File
92 lines
2.8 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Privilege;
|
|
use App\Models\User;
|
|
use App\Providers\RouteServiceProvider;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class LoginController extends Controller
|
|
{
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| Login Controller
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
| This controller handles authenticating users for the application and
|
|
| redirecting them to your home screen. The controller uses a trait
|
|
| to conveniently provide its functionality to your applications.
|
|
|
|
|
*/
|
|
|
|
use AuthenticatesUsers;
|
|
|
|
/**
|
|
* Where to redirect users after login.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $redirectTo = RouteServiceProvider::HOME;
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('guest')->except('logout');
|
|
}
|
|
|
|
/**
|
|
* The user has been authenticated.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param mixed $user
|
|
* @return mixed
|
|
*/
|
|
protected function authenticated(Request $request, $user)
|
|
{
|
|
// Get user's role_id
|
|
$roleId = Auth::user()->role_id;
|
|
|
|
if (!$roleId) {
|
|
// User has no role, redirect to default
|
|
return redirect(RouteServiceProvider::HOME);
|
|
}
|
|
|
|
// Check if user has access to adminarea menu
|
|
if (!User::roleCanAccessMenu($roleId, 'adminarea')) {
|
|
// User doesn't have admin area access, redirect to default home
|
|
return redirect(RouteServiceProvider::HOME);
|
|
}
|
|
|
|
// User has admin area access, get first accessible menu (excluding adminarea and mechanicarea)
|
|
$firstMenu = Privilege::join('menus', 'privileges.menu_id', '=', 'menus.id')
|
|
->where('privileges.role_id', $roleId)
|
|
->where('privileges.view', 1)
|
|
->whereNotIn('menus.link', ['adminarea', 'mechanicarea'])
|
|
->select('menus.*', 'privileges.view', 'privileges.create', 'privileges.update', 'privileges.delete')
|
|
->orderBy('menus.id')
|
|
->first();
|
|
|
|
if (!$firstMenu) {
|
|
// User has no accessible menus (excluding adminarea/mechanicarea), redirect to default
|
|
return redirect(RouteServiceProvider::HOME);
|
|
}
|
|
|
|
try {
|
|
// Try to redirect to the first accessible menu
|
|
return redirect()->route($firstMenu->link);
|
|
} catch (\Exception $e) {
|
|
// Route doesn't exist, fallback to default home
|
|
return redirect(RouteServiceProvider::HOME);
|
|
}
|
|
}
|
|
|
|
}
|