module Devise
def self.activerecord51? # :nodoc:
def self.activerecord51? # :nodoc: deprecator.warn <<-DEPRECATION.strip_heredoc [Devise] `Devise.activerecord51?` is deprecated and will be removed in the next major version. It is a non-public method that's no longer used internally, but that other libraries have been relying on. DEPRECATION defined?(ActiveRecord) && ActiveRecord.gem_version >= Gem::Version.new("5.1.x") end
def self.add_mapping(resource, options)
def self.add_mapping(resource, options) mapping = Devise::Mapping.new(resource, options) @@mappings[mapping.name] = mapping @@default_scope ||= mapping.name @@helpers.each { |h| h.define_helpers(mapping) } mapping end
def self.add_module(module_name, options = {})
Devise.add_module(:party_module, insert_at: 0)
Devise.add_module(:party_module, model: 'party_module/model')
Devise.add_module(:party_module, strategy: true, controller: :sessions)
Devise.add_module(:party_module)
== Examples:
name.
All values, except :model, accept also a boolean and will have the same name as the given module
+insert_at+ - Integer representing the order in which this module's model will be included
+strategy+ - Symbol representing if this module got a custom *strategy*.
+route+ - Symbol representing the named *route* helper for this module.
+controller+ - Symbol representing the name of an existing or custom *controller* for this module.
+model+ - String representing the load path to a custom *model* for this module (to autoload.)
== Options:
in the model class definition.
process. That requires that the module be listed in the arguments passed to the 'devise' method
Note that adding a module using this method does not cause it to be used in the authentication
called from lib/devise/modules.rb. Third-party modules need to be added explicitly using this method.
Register available devise modules. For the standard modules that Devise provides, this method is
def self.add_module(module_name, options = {}) options.assert_valid_keys(:strategy, :model, :controller, :route, :no_input, :insert_at) ALL.insert (options[:insert_at] || -1), module_name if strategy = options[:strategy] strategy = (strategy == true ? module_name : strategy) STRATEGIES[module_name] = strategy end if controller = options[:controller] controller = (controller == true ? module_name : controller) CONTROLLERS[module_name] = controller end NO_INPUT << strategy if options[:no_input] if route = options[:route] case route when TrueClass key, value = module_name, [] when Symbol key, value = route, [] when Hash key, value = route.keys.first, route.values.flatten else raise ArgumentError, ":route should be true, a Symbol or a Hash" end URL_HELPERS[key] ||= [] URL_HELPERS[key].concat(value) URL_HELPERS[key].uniq! ROUTES[module_name] = key end if options[:model] path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model]) camelized = ActiveSupport::Inflector.camelize(module_name.to_s) Devise::Models.send(:autoload, camelized.to_sym, path) end Devise::Mapping.add_module module_name end
def self.available_router_name
def self.available_router_name router_name || :main_app end
def self.configure_warden! #:nodoc:
See lib/devise/rails/routes.rb - ActionDispatch::Routing::RouteSet#finalize_with_devise!
A method used internally to complete the setup of warden manager after routes are loaded.
def self.configure_warden! #:nodoc: @@warden_configured ||= begin warden_config.failure_app = Devise::Delegator.new warden_config.default_scope = Devise.default_scope warden_config.intercept_401 = false Devise.mappings.each_value do |mapping| warden_config.scope_defaults mapping.name, strategies: mapping.strategies warden_config.serialize_into_session(mapping.name) do |record| mapping.to.serialize_into_session(record) end warden_config.serialize_from_session(mapping.name) do |args| mapping.to.serialize_from_session(*args) end end @@warden_config_blocks.map { |block| block.call Devise.warden_config } true end end
def self.deprecator
def self.deprecator @deprecator ||= ActiveSupport::Deprecation.new("5.0", "Devise") end
def self.friendly_token(length = 20)
Generate a friendly string randomly to be used as token.
def self.friendly_token(length = 20) # To calculate real characters, we must perform this operation. # See SecureRandom.urlsafe_base64 rlength = (length * 3) / 4 SecureRandom.urlsafe_base64(rlength).tr('lIO0', 'sxyz') end
def self.include_helpers(scope)
def self.include_helpers(scope) ActiveSupport.on_load(:action_controller) do include scope::Helpers if defined?(scope::Helpers) include scope::UrlHelpers end ActiveSupport.on_load(:action_view) do include scope::UrlHelpers end end
def self.mailer
def self.mailer @@mailer_ref.get end
def self.mailer=(class_name)
def self.mailer=(class_name) @@mailer_ref = ref(class_name) end
def self.omniauth(provider, *args)
config.omniauth :github, APP_ID, APP_SECRET
Specify an OmniAuth provider.
def self.omniauth(provider, *args) config = Devise::OmniAuth::Config.new(provider, args) @@omniauth_configs[config.strategy_name.to_sym] = config end
def self.omniauth_providers
def self.omniauth_providers omniauth_configs.keys end
def self.ref(arg)
def self.ref(arg) # TODO: Remove AS::Dependencies usage when dropping support to Rails < 7. if ActiveSupport::Dependencies.respond_to?(:reference) ActiveSupport::Dependencies.reference(arg) end Getter.new(arg) end
def self.regenerate_helpers!
def self.regenerate_helpers! Devise::Controllers::UrlHelpers.remove_helpers! Devise::Controllers::UrlHelpers.generate_helpers! end
def self.secure_compare(a, b)
def self.secure_compare(a, b) return false if a.blank? || b.blank? || a.bytesize != b.bytesize l = a.unpack "C#{a.bytesize}" res = 0 b.each_byte { |byte| res |= byte ^ l.shift } res == 0 end
def self.setup
Default way to set up Devise. Run rails generate devise_install to create
def self.setup yield self end
def self.warden(&block)
end
manager.oauth(:twitter)
# Configure warden to use other strategies, like oauth.
config.warden do |manager|
config.allow_unconfirmed_access_for = 2.days
Devise.setup do |config|
initialization.
Sets warden configuration using a block that will be invoked on warden
def self.warden(&block) @@warden_config_blocks << block end