Reputation: 35
I just wanna redirect different type of users in different views.
I have seen so much tutorials but it doesn't seem so applicable to what I have done so far. So I have is, a table which has Username, password and type( enum type which are admin and superAdmin), another table for each entity for their uniques attributes . I have done this kind of problem but without a laravel framework. I'm using Auth of laravel. How could I done this with a simple way. Thank You so MUCH!!
Upvotes: 3
Views: 657
Reputation: 31
Just this will work:
protected function redirectTo(){
$user = Auth::user();
if($user->type == "SUPERADMIN"){ // Use your user type
return '/superadminroute';
} elseif($user->type == "ADMIN"){ // Use your user type
return '/adminroute';
} else{
return '/home';
}
}
Put this is Auth\LoginController
. Make sure to remove protected $redirectTo = '/home';
;
Upvotes: 2
Reputation: 902
the easiest way to achieve this is to do it this way. With this users sees what you assigned to them as a super admin. Have this tables:
userstable with ID, fullname, username, password and role attributes
roletable with ID and roleName attributes
moduletable with ID, moduleName and route attributes
assignModulestable with ID, roleID and moduleID attributes
asignRole table with ID, roleID and userID attributes
ROLE table:
ID roleName
--------------
1 admin
2 sub admin
MODULE table:
ID moduleName route
------------------------------
8 | View Members | /members
7 | Members Withdrawal | /withdrawals
6 | View Payment | /payment
assignModules table:
ID roleID moduleID
--------------------
5 | 1 | 8
6 | 1 | 7
21 | 1 | 6
22 | 2 | 8
23 | 1 | 7
asignRole table:
ID roleID userID
-----------------
1 | 1 | 39
2 | 2 | 43
5 | 2 | 44
4 | 2 | 46
users table:
id name email password role
--------------------------------------------------
39 Luke [email protected] 1234 1
43 John [email protected] 1234 2
44 mark [email protected] 1234 2
46 Peter [email protected] 1234 2
the above will be the table structure and the data therein. try to normalised the table with primary and foreign keys.
create your corresponding forms for data insertion. As you can see above, we have two roles; admin and subadmin with ids 1 and 2 respectively
define your route and link name in module table as shown in module table
in your assign module table assign a role to a module using the ids
in the assign role table define the user and roles as shown in assignRole table above.
then you have your users table with role attribute defined
finally, in your layout.blade.php add this:
<li>
<a href="#"><i class="fa fa-user-o fa-fw"></i> Management<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
</li>
@php
$userModule = DB::table('assignModules')
->join('modules', 'modules.moduleID', '=', 'assignModules.moduleID')
->where('assignModules.roleID', '=', Auth::user()->role)
->whereRaw('modules.moduleID = assignModules.moduleID')
->distinct()
->select('modules.moduleName', 'modules.moduleID','modules.route')
->orderBy('modules.moduleID', 'ASC')
->get();
@endphp
@if($userModule)
@foreach($userModule as $module)
<li>
<a href="{!! url($module->route) !!}"><i class="fa fa-user"></i> {{ $module->moduleName }}</a>
</li>
@endforeach
@endif
</ul>
<!-- /.nav-second-level -->
</li>
@if( Auth::user()->role==1 )
<li>
<a href="#"><i class="fa fa-sitemap fa-fw"></i> Technical Settings<span class="fa arrow"></span></a>
<ul class="nav nav-second-level">
<li>
<a href="{{ url('/assign-user') }}"><i class="fa fa-user"></i> Assign User</a>
</li>
<li>
<a href="{{ url('/module') }}"><i class="fa fa-user"></i> Create Modules</a>
</li>
<li>
<a href="{{ url('/assign-module') }}"><i class="fa fa-user"></i> Assign Module</a>
</li>
</ul>
<!-- /.nav-second-level -->
</li>
@else
@endif
Upvotes: 0
Reputation: 4992
If You have run command php artisan make:auth
Then you have LoginController.php
in You app\Http\Controllers\Auth
Folder.
You Can Put Method redirectTo
in that class Where You Can put your redirection Logic
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected function redirectTo(){
$user = Auth::user();
if($user->hasRole('SUPER_ADMIN')){
return '/superadminroute';
} elseif($user->hasRole('ADMIN')){
return '/adminroute';
}
// Of Course You Can Use route('routename') Helper fuction to generate Route
return '/';
}
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
This Function Runs when user log in successfully and it is time to redirect user to specified route.
Hope This Helps You.
Upvotes: 1
Reputation: 856
You can do it simple as you can using Laravel Middleware. Maybe some code below can help :
First, make a new middleware php artian make:middleware UserVerify
.
Second, write some logic code to redirect user by User role
:
public function handle($request, Closure $next)
{
if(\Auth::user()->type == 'superAdmin')
{
return $next($request);
}
return redirect()->route('route_name');
}
Third, register this middleware to your project App\Http\Kernel.php
Finally, apply this middleware to your route you want.
Upvotes: 0