简体   繁体   中英

How do I override a class variable in this gem's module?

I'm using the devise_saml_authenticatable gem for saml authentication with devise and would like to override the @@saml_default_resource_locator variable on line 105 here . I just want to add an extra where clause so that it's only looking at users of a certain type. I've overriden gem methods in the past (but never class variables) do doing putting something like this in an initializer:

RedCloth::Formatters::HTML.send(:include, GemExtensions::RedCloth::Formatters::HTML::Notextile)

But I'm not sure where to start in this instance. Any help would be appreciated, thanks!

While @chumakoff's answer will do the trick, I think that you do not have to and shouldn't patch the DeviseSamlAuthenticatable gem this way. The gem mainly calls the block configurable under the Devise.saml_resource_locator accessor when authenticating. And if this block is not set, it calls the Devise.saml_default_resource_locator block by default.

So, I guess you should simply set the saml_resource_locator :

Devise.saml_resource_locator = Proc.new do |model, saml_response, auth_value|
  model.where(Devise.saml_default_user_key => auth_value).where(...).first
end

See also the specs for some examples .

It is very easy to change a class variable. There is the class_variable_set method that you can use. Or you can just open the module and define the variable again, or use instance_eval .

Put this code to initializers:

Devise.class_variable_set(
  :@@saml_default_resource_locator,
  Proc.new do |model, saml_response, auth_value|
    # whatever you want
  end
)

Or open the module and define the variable again:

module Devise
  @@saml_default_resource_locator = Proc.new do |model, saml_response, auth_value|
    # whatever you want
  end
end

Using instance_eval :

Devise.instance_eval do
  @@saml_default_resource_locator = Proc.new do |model, saml_response, auth_value|
    # whatever you want
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM