switch_user
switch_user copied to clipboard
Switch_back as a link
Is there a simple way to detect that I have switched to a user and display a switch_back link? I tried to check for original_user
in the view, but it doesn't work this way.
Internally, I use session[:original_user] to store the original user. Checking to see if that exists will tell you if you've switched from another user.
FYI This is definitely going to change because I definitely shouldn't be storing the whole user object in the session.
HTH
Hi, you mean you're storing the whole user object? I think it would be ok to store just the ID.
Just checked it, session[:original_user]
is always nil
Examine the entire session variable if you can. It may be under session[:original_user_scope_identifier] at this point in time.
Added session[:original_user_scope_identifier].inspect
to application layout. It's always nil
as well.
Hmmmm are you using the switch_back feature ? You can read about it in the read me.
Hmm I think yes, here is an excerpt from my initializers/switch_user.rb
:
config.switch_back = true
config.controller_guard = ->(current_user, request, original_user) do
current_user && current_user.can_supervise? || original_user && original_user.can_supervise?
end
try this in your switch_user.rb
SwitchUser.setup do |config|
config.switch_back = true
config.controller_guard = lambda { |current_user, request, original_user|
current_user && (current_user.has_role? :admin) || original_user && (original_user.has_role? :admin)
}
end
and then something like this in your view
<%if current_user.present? %>
<% if (current_user.has_role? :admin) or (session[:original_user_scope_identifier].present?) %>
<%= switch_user_select %>
<% end %>
<% end %>