Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch_back as a link #51

Open
firedev opened this issue Oct 18, 2013 · 8 comments
Open

Switch_back as a link #51

firedev opened this issue Oct 18, 2013 · 8 comments

Comments

@firedev
Copy link

firedev commented Oct 18, 2013

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.

@lcowell
Copy link
Collaborator

lcowell commented Oct 22, 2013

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

@firedev
Copy link
Author

firedev commented Oct 23, 2013

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

@firedev
Copy link
Author

firedev commented Oct 23, 2013

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

@lcowell
Copy link
Collaborator

lcowell commented Jan 8, 2014

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

@firedev
Copy link
Author

firedev commented Jan 9, 2014

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

@lcowell
Copy link
Collaborator

lcowell commented Jan 9, 2014

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

@firedev
Copy link
Author

firedev commented Jan 10, 2014

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

@osuthorpe
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants