Hello,
I have a admin dashboard and there are three roles,
user
admin
manager
User will have access to his dashboard and admin will have complete access to admin dashboard to mange the website.
i have a tables like below,
roles
id, name
role_permissions
id,role_id,can_access_admin,can_access_journal
in users table,
id, default_role_id,name,email
User.php
public function role()
{
return $this->belongsTo('App\Models\Role', 'role_id', 'id');
}
Role.php
public function permission()
{
return $this->hasOne('App\Models\RolePermission');
}
RolePermission.php
public function role()
{
return $this->belongsTo('App\Models\Role', 'role_id', 'id');
}
So now in controller to check if user logged in or not i have added below middleware,
public function __construct()
{
$this->middleware('auth');
}
So now i have created a new middleware to check user can access admin panel like below,
public function handle(Request $request, Closure $next)
{
if (Auth::user()->role->permission->can_access_admin_panel)
return $next($request);
abort(401, 'This action is unauthorized.');
}
and added this middleware in controller like below,
public function __construct()
{
$this->middleware('auth');
$this->middleware('canAccessAdminPanel');
}
this is working fine for user and admin.
Now i am trying to create a new role in admin end for manager role. and manger role will have access to only one module in admin end,
i have another column in role_permission table like, "can_access_journal" this coulmn can be used to check this user has access to that module or not.
But i want to redirect to same admin dashboard for the manager role too. but i want to restrict all other pages access and it should return unauthorized.
So now manger should login to admin end and can access Journal management module and restrict other module access by returning unauthorized access.
Any suggestions?