config.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # encoding: utf-8
  2. module Warden
  3. # This is a class which is yielded on use Warden::Manager. If you have a plugin
  4. # and wants to add more configuration to warden, you just need to extend this
  5. # class.
  6. class Config < Hash
  7. # Creates an accessor that simply sets and reads a key in the hash:
  8. #
  9. # class Config < Hash
  10. # hash_accessor :failure_app
  11. # end
  12. #
  13. # config = Config.new
  14. # config.failure_app = Foo
  15. # config[:failure_app] #=> Foo
  16. #
  17. # config[:failure_app] = Bar
  18. # config.failure_app #=> Bar
  19. #
  20. def self.hash_accessor(*names) #:nodoc:
  21. names.each do |name|
  22. class_eval <<-METHOD, __FILE__, __LINE__ + 1
  23. def #{name}
  24. self[:#{name}]
  25. end
  26. def #{name}=(value)
  27. self[:#{name}] = value
  28. end
  29. METHOD
  30. end
  31. end
  32. hash_accessor :failure_app, :default_scope, :intercept_401
  33. def initialize(other={})
  34. merge!(other)
  35. self[:default_scope] ||= :default
  36. self[:scope_defaults] ||= {}
  37. self[:default_strategies] ||= {}
  38. self[:intercept_401] = true unless key?(:intercept_401)
  39. end
  40. def initialize_copy(other)
  41. super
  42. deep_dup(:scope_defaults, other)
  43. deep_dup(:default_strategies, other)
  44. end
  45. # Do not raise an error if a missing strategy is given.
  46. # :api: plugin
  47. def silence_missing_strategies!
  48. self[:silence_missing_strategies] = true
  49. end
  50. def silence_missing_strategies? #:nodoc:
  51. !!self[:silence_missing_strategies]
  52. end
  53. # Set the default strategies to use.
  54. # :api: public
  55. def default_strategies(*strategies)
  56. opts = Hash === strategies.last ? strategies.pop : {}
  57. hash = self[:default_strategies]
  58. scope = opts[:scope] || :_all
  59. hash[scope] = strategies.flatten unless strategies.empty?
  60. hash[scope] || hash[:_all] || []
  61. end
  62. # A short hand way to set up a particular scope
  63. # :api: public
  64. def scope_defaults(scope, opts = {})
  65. if strategies = opts.delete(:strategies)
  66. default_strategies(strategies, :scope => scope)
  67. end
  68. if opts.empty?
  69. self[:scope_defaults][scope] || {}
  70. else
  71. self[:scope_defaults][scope] ||= {}
  72. self[:scope_defaults][scope].merge!(opts)
  73. end
  74. end
  75. # Quick accessor to strategies from manager
  76. # :api: public
  77. def strategies
  78. Warden::Strategies
  79. end
  80. # Hook from configuration to serialize_into_session.
  81. # :api: public
  82. def serialize_into_session(*args, &block)
  83. Warden::Manager.serialize_into_session(*args, &block)
  84. end
  85. # Hook from configuration to serialize_from_session.
  86. # :api: public
  87. def serialize_from_session(*args, &block)
  88. Warden::Manager.serialize_from_session(*args, &block)
  89. end
  90. protected
  91. def deep_dup(key, other)
  92. self[key] = hash = other[key].dup
  93. hash.each { |k, v| hash[k] = v.dup }
  94. end
  95. end
  96. end