Dropdown to view a page as different user roles while admin

135 Views Asked by At

I currently have a view simplified below...

<% if can? :manage, @task %>
  Admin 
<% elsif can? :update, @task %>
  Moderator
<% elsif current_user %>
  User
<% else %>
  Not logged in
<% end %>

This view has a large number of fields that are wrapped in similar conditionals for each user and I currently have to log in and out of test accounts to check formatting.

I want to be logged in as an admin but have a dropdown to select whether the page is rendered as Admin or Moderator or User or Not logged in

I have some rough ideas of solutioning, but don't know which to follow...

  • Bake into cancan with an extra column on user that I can set from a navbar dropdown form
  • Create user specific methods
  • Allow a url parameter to request session view and render accordingly

Is there a best practice around this?

1

There are 1 best solutions below

1
Gary Wright On

How about if you implemented something like the real/effective user dichotomy of Unix?

You're existing infrastructure is the 'real' user based on whatever authentication has occurred but you also associate an 'effective user' with each session.

The effective user starts out the same as the real user but can be changed to something else. Changing the effective user should be conditioned on the real user having admin rights.

Condition all your layout on the effective user and not on the real user. Condition your 'change effective user' drop down on the real user.