Reputation: 23
I want to make sure this two is connecting with a variable. Which the variable determine during login: ROLE. The user will only can view information who work under them only.
LoginController.php
protected function authenticated($request, $user)
{
if($user->role == 'workers') {
$this->redirectTo = ('/home');
} else if($user->role == 'manager') {
$this->redirectTo = ('/welcome');
} else {//($user->role = 'admin')
$this->redirectTo = ('/dashboard');
}
return redirect()->intended($this->redirectPath());
}
AttendanceController.php
class AttendanceViewController extends Controller
{
protected function index()
{
$attendance1 = DB::select('select * from attendance where line_num = "$role"');
return view('attendanceview',['attendance1'=>$attendance1]);
}
}
I have tried the suggestion from our friends: Session & Authorization it seems to work but it does not appear the information that I want in the AttendanceController.
Upvotes: 0
Views: 100
Reputation: 23
After trying the suggestion by Chris and Josh, I have updated the code:
LoginController.php
protected function authenticated($request, $user)
{
$user = auth()->user();
$role=$user->role;
Session::put('role', $role);
if($user->role == 'workers') {
$this->redirectTo = ('/home');
} else if($user->role == 'manager') {
$this->redirectTo = ('/welcome');
} else { //($user->role = 'admin')
$this->redirectTo = ('/dashboard');
}
return redirect()->intended($this->redirectPath());
}
AttendanceController.php
public function index() {
$user = auth()->user();
$position=$user->position;
}
Upvotes: 0
Reputation: 1366
nia
I would save the information that you need to the session as this will update the information each time they login
This can be done like this:
Session::put('yourUniqueSessionName', 'valuesYouWishToStore');
You can access the session data like this:
if (Session::has('yourUniqueSessionName') ) {
$data = Session::get('yourUniqueSessionName');
} else {
$data = (get the data that you want again);
}
This will then allow you to query the Session data and see what you want to allow them to access.
For more info:
https://laravel.com/docs/5.7/session
Hope this helps
Upvotes: 0
Reputation: 58312
This requirement wouldn't request "global" variables.
In any controller, you will have access to the user via the Auth facade:
public function index()
{
if (Auth::check()) {
// The user is logged in...
if(Auth::user()->role === 'something') {
// Do something when x role
}
}
}
Note, this is a very primitive example - most likely you want to read through the authorization documentation (https://laravel.com/docs/5.7/authorization) which provides more robust approach to authorization (not authentication) than simple if checks.
The point of the above snippet is more about showing that you don't need to have globals to do what you need to do.
If you absolutely need to use globals (please don't - you really don't need to), you still have access to all normal PHP approaches, such as:
$GLOBALS['variable'] = 'foo';
Upvotes: 1