Home > OS >  How to make user login as role features?
How to make user login as role features?

Time:11-20

So currently I'm developing web apps that will have users management and role management (roles using spatie/laravel-permission). it have a lot of roles for example:

  • Root (super admin)
  • Admin Daerah(based on the village)
  • admin unit (based on his working place/unit)
  • Kepala Badan
  • Kepala seksi
  • Staff
  • Citizen
  • Etc.

Every user can (and will) have more than one roles for example user with roles ['admin daerah', 'staff', and 'citizen'].

I want to make a feature where every time a user login and has more than one role, he will be redirected to a page that shows his role lists and he will select one of the roles. after he selects the role, it will be redirected to the dashboard and only have permission based on the selected role (for example he select 'Staff' role, so he only can do 'things' that be allowed as staff).

I already find on StackOverflow and asking on another forum, but I still can't find my solutions, and I'm still new on laravel too. I hope someone can give some solution and the logic of how to do it. thank you

Edit 1: because I'm using spatie/laravel-permission, its already have model_has_roles table as the pivot.. so it will contain role_id, role_name, guard_name, model_id as user_id, foreign key unit_id, foreign key institution_id, is_active

CodePudding user response:

Check this way:

  1. Create role_ids(text) row in users table. Save user roles in this row via implode() method. (example: 1,4,3,6)
  2. When user login, check this role_ids, if there is more than 1 role ask for choosing one of them.
  3. After choosing, update user role in spatie.

But don't forget, when you will update user role via Administration Panel you should update it not in spatie roles table, but in role_ids in users table. otherwise user can get access to other roles if user already logged in.

CodePudding user response:

You can try an approach like this:

Once user has logged in, include the query below before redirecting the user to the intended view.

Ex.

$userRoles = ModelHasRole::where('user_id', '=', Auth::User()->id)->get();

return view ('select_role_page', compact('userRoles'))

Then in your select_role_page blade file

@foreach ($userRoles as $indiv_userRole)

    @if ($indiv_userRole->role_name == 'admin')
      <a href="role/admin">Link to Admin Dashboard</a>
    @endif

    @if ($indiv_userRole->role_name == 'staff')
      <a href="role/staff">Link to staff Dashboard</a>
    @endif

    @if ($indiv_userRole->role_name == 'citizen')
      <a href="role/citizen">Link to Citizen Dashboard</a>
    @endif

@endforeach

If the user has any of the roles indentified above, the appropriate link will be shown.

EDIT: Make sure you have a route for the links above like the one below.

Route::get('/role/{id}', 'RoleController@showDashboard');

And in your controller.

public function showDashboard ($id) {

   if ($id == 'admin') {
      return view ('admindashboard');
   }

   if ($id == 'staff') {
      return view ('staffdashboard');
   }

   if ($id == 'citizen') {
      return view ('citizendashboard');
   }

}
  • Related