0

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

5
  • How do you record the multiple roles a user can have in your database? For example, do you have role_1, role_2, role_3 columns in your users table? Or do you have like a separate roles_per_user table? Commented Nov 19, 2021 at 7:25
  • because im using spatie, its already have model_has_roles table as the pivot.. so it will contain role_id, role_name, guard_name, model_id etc note: model_id = user_id Commented Nov 19, 2021 at 7:33
  • Not exactly familiar with spatie, but does the model_has_roles table have a user_id column? Commented Nov 19, 2021 at 7:41
  • Yes, model_id =user_id.. so its basically same only with different column name Commented Nov 19, 2021 at 7:46
  • Posted an answer that you might want to try. Hope it helps. Commented Nov 19, 2021 at 8:07

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I have already changed my edit so it contains all columns from model_has_role, because every row has data of unit_id and institution_id, so I think if using your ways it affects a lot of my other functions, make me change a lot of code and will take a lot of time to fix it. so maybe i will try to find another ways first before trying your advice.. thank you btw
-1

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');
   }

}

8 Comments

I've thought about this way before, but there are a few things that bother me... first, I don't know where I put the query/code before it redirected to dashboard/intended view. second, I have a menu on the sidebar, and because I'm using template for my project so it needs more configuration to handle some dynamic dashboard based on role (i will think about it later). third,
You can insert the query under the AuthenticatesUsers.php file that can be found in vendor\laravel\ui\auth-backend. Insert the query inside protected function authenticated (Request $request, $user) { // Insert it here }
let say, I'm using your way... how to limit some features in blade files and on the controller? like how to check the selected role? because i think i will implements some code based on the selected role
I mean implements some conditional based on the selected role, for example: if the selected role is staff then it will doing this, but if the selected role is citizen, it will do nothing
With the blade code, the 'citizen` link will not show up anyway if the user doesn't have a citizen role. If the user has a staff role, then he or she will be able to click the staff link and be redirected to, say, /staff. Just make sure that you create a /staff route that calls a function that contains the logic that you want.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.