Giter Club home page Giter Club logo

Comments (4)

flyerhzm avatar flyerhzm commented on September 28, 2024

do you mean it works fine in development environment, but not production?
it's wierd, did you put the gem "switch_user" in development group in
Gemfile?

On Mon, Apr 11, 2011 at 8:20 AM, aunderwo <
[email protected]>wrote:

I have changed my config in an initializer to be

config.controller_guard == lambda { |current_user, request|
Rails.env.development? || (Rails.env.production? && current_user &&
current_user.admin?) }

However in production switch_user always fails. Even if I change the lambda
to always return true it still fails. It seems that the config is not being
read. However I know the switch_user initializer is working since changing
config.provider to :authlogic causes an error since I am using devise.

Reply to this email directly or view it on GitHub:
#7

Best regards,
Richard Huang
[email protected]

http://www.huangzhimin.com
http://www.huangzhimin.comhttp://rails-bestpractices.com
http://www.rubyslide.com
http://github.com/flyerhzm
@flyerhzm

from switch_user.

antunderwood avatar antunderwood commented on September 28, 2024

No the gem is available in all environments and I know the gem is active in production since the switch_user URL works but gives permission denied whether I'm admin or not. This is still the case even if the controller_guard lambda is just 'true'.

Can't quite figure how to debug this. I'm using Ruby 1.9.2 in case this makes a difference.

from switch_user.

antunderwood avatar antunderwood commented on September 28, 2024

OK tracked down the problem. The initializer config does get picked up.
The problem was copying the default config from the ReadMe has this line

config.view_guard == lambda { |current_user, request| Rails.env.development? }

note the double =. I copied and pasted this and caused errors using this. My switch_user.rb initializer is as below and works fine. Please note for the redirect to match the default config it should be

 config.redirect_path = lambda { |request, params| request.env["HTTP_REFERER"] ? :back : root_path } 

not I think

config.redirect_path = lambda { |request, params| '/' }

As can be found in the readme

SwitchUser.setup do |config|
  # provider may be :devise or :authlogic
  config.provider = :devise

  # available_users is a hash, 
  # key is the model name of user (:user, :admin, or any name you use), 
  # value is a block that return the users that can be switched.
  config.available_users = { :user => lambda { User.all } }

  # available_users_identifiers is a hash,
  # keys in this hash should match a key in the available_users hash
  # value is the name of the identifying column to find by,
  # defaults to id
  # this hash is to allow you to specify a different column to
  # expose for instance a username on a User model instead of id
  config.available_users_identifiers = { :user => :id }

  # available_users_names is a hash,
  # keys in this hash should match a key in the available_users hash
  # value is the column name which will be displayed in select box
  config.available_users_names = { :user => :email }

  # controller_guard is a block, 
  # if it returns true, the request will continue, 
  # else the request will be refused and returns "Permission Denied"
  # if you switch from "admin" to user, the current_user param is "admin"
  config.controller_guard  = lambda { |current_user, request| Rails.env.development? || ( Rails.env.production? && current_user && current_user.admin?) }

  # view_guard is a block, 
  # if it returns true, the switch user select box will be shown, 
  # else the select box will not be shown
  # if you switch from admin to "user", the current_user param is "user"
  config.view_guard =  lambda { |current_user, request| Rails.env.development? || ( Rails.env.production? && current_user && current_user.admin?) }

  # redirect_path is a block, it returns which page will be redirected 
  # after switching a user.
  config.redirect_path = lambda { |request, params| request.env["HTTP_REFERER"] ? :back : root_path }  
end

from switch_user.

flyerhzm avatar flyerhzm commented on September 28, 2024

fixed, thanks for pointing it out.

from switch_user.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.