Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Override Rails具有"安全"密码';s认证方法_Ruby On Rails_Ruby_Authentication - Fatal编程技术网

Ruby on rails Override Rails具有"安全"密码';s认证方法

Ruby on rails Override Rails具有"安全"密码';s认证方法,ruby-on-rails,ruby,authentication,Ruby On Rails,Ruby,Authentication,我正在使用一个自定义用户数据库与具有\u secure\u password和active directory的组合进行身份验证。如果用户被标记为Active Directory用户,则我将针对AD进行身份验证,否则针对数据库进行身份验证 我想通过的身份验证方法来反映这一点 我知道我可以在控制器中处理它,或者通过调用身份验证的新方法来处理它,但我更希望使用适当的覆盖 有没有办法做到这一点: class User < ActiveRecord::Base # Here is how I

我正在使用一个自定义用户数据库与
具有\u secure\u password
和active directory的组合进行身份验证。如果用户被标记为Active Directory用户,则我将针对AD进行身份验证,否则针对数据库进行身份验证

我想通过
身份验证
方法来反映这一点

我知道我可以在控制器中处理它,或者通过调用
身份验证的新方法来处理它,但我更希望使用适当的覆盖

有没有办法做到这一点:

class User < ActiveRecord::Base

  # Here is how I make the validation work out, if that matters:
  has_secure_password :validations => false
  validates :active_directory_user, presence: true, unless: :password
  validates :password, presence: true, length: { minimum: 6 }, unless: :active_directory_user
  validates :password_digest, presence: true, unless: :active_directory_user

  # Here is what I want to do
  def authenticate(pass)
    if self.active_directory_user
      active_directory_auth(pass)
    else
      # this doesn't work, I need to somehow reference the non-overriden function
      self.authenticate(pass)
    end
  end
end
class用户false
验证:active\u目录\u用户,状态:true,除非::password
验证:password,presence:true,length:{minimum:6},除非::active\u directory\u user
验证:password\u digest,presence:true,除非::active\u directory\u user
#这是我想做的
def身份验证(通过)
如果self.active\u目录\u用户
活动目录验证(通过)
其他的
#这不起作用,我需要以某种方式引用非重写函数
自我验证(pass)
结束
结束
结束

好吧,我想出来了。我需要使用一些猴子补丁作为详细说明。我使用方法包装了部分的建议

最终结果是:

class User < ActiveRecord::Base
  # Must have either password or be a active directory user
  has_secure_password :validations => false
  validates :active_directory_user, presence: true, unless: :password
  validates :password, presence: true, length: { minimum: 6 }, unless: :active_directory_user
  validates :password_digest, presence: true, unless: :active_directory_user

  # Mokey patch on authenticate to use active directory
  old_autheticate = instance_method(:authenticate)
  define_method(:authenticate) do |pass|
    if self.active_directory_user
      active_directory_auth(pass)
    else
      old_autheticate.bind(self).(pass)
    end
  end

  private
    def active_directory_auth (password)
      domain = "domain"
      ldap = Net::LDAP.new
      ldap.host = '<ad ip>'
      ldap.port = 389
      ldap.auth self.username + "@" + domain, password
      if ldap.bind
        self
      else
        false
      end
    end
end
class用户false
验证:active\u目录\u用户,状态:true,除非::password
验证:password,presence:true,length:{minimum:6},除非::active\u directory\u user
验证:password\u digest,presence:true,除非::active\u directory\u user
#使用active directory进行身份验证时的Mokey修补程序
旧的\u authenticate=实例\u方法(:authenticate)
define|u方法(:authenticate)do | pass|
如果self.active\u目录\u用户
活动目录验证(通过)
其他的
旧授权。绑定(自身)。(通过)
结束
结束
私有的
def活动目录认证(密码)
domain=“domain”
ldap=Net::ldap.new
ldap.host=“”
ldap.port=389
ldap.auth self.username+“@”+域,密码
如果ldap.bind
自己
其他的
假的
结束
结束
结束