switch_user icon indicating copy to clipboard operation
switch_user copied to clipboard

Switch_back as a link

Open firedev opened this issue 11 years ago • 8 comments

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.

firedev avatar Oct 18 '13 13:10 firedev

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

lcowell avatar Oct 22 '13 04:10 lcowell

Hi, you mean you're storing the whole user object? I think it would be ok to store just the ID.

firedev avatar Oct 23 '13 03:10 firedev

Just checked it, session[:original_user] is always nil

firedev avatar Oct 23 '13 04:10 firedev

Examine the entire session variable if you can. It may be under session[:original_user_scope_identifier] at this point in time.

lcowell avatar Jan 08 '14 19:01 lcowell

Added session[:original_user_scope_identifier].inspect to application layout. It's always nil as well.

firedev avatar Jan 09 '14 02:01 firedev

Hmmmm are you using the switch_back feature ? You can read about it in the read me.

lcowell avatar Jan 09 '14 14:01 lcowell

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

firedev avatar Jan 10 '14 04:01 firedev

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 %>

osuthorpe avatar Jul 03 '14 03:07 osuthorpe