我需要做的是检查身份验证模型的关联内容。以下是我想要使用的方法。
# make sure that the user has access to the subdomain that they are
# attempting to login to, subdomains are company names
def self.find_by_email_and_company(email, company)
user = User.find_by_email(email)
companies = []
user.brands.each do |b|
companies << b.company.id
end
user && companies.include?(company)
end
但是这种方法失败了,因为只有一个参数被发送到find_by_email_and_company方法。
公司实际上是子域名,所以为了在这里获取它,我只能将其放置在表单中的隐藏字段中(这是我能想到的唯一方法来将其传递给模型)。
有没有一种方法可以覆盖某些方法..?
使用下面的答案,我得出了以下解决方案:
用户模型(User.rb)
def self.find_by_email_within_company(email)
# find the user
user = self.find_by_email(email)
# no need to continue if the email address is invalid
return false if user.nil?
# collect the subdomains the provided user has access to
company_subdomains = user.brands.map(&:company).map(&:subdomain)
# verify that the user has access to the current subdomain
company_subdomains.include?(Thread.current[:current_subdomain]) && user
end
应用程序控制器
before_filter :set_subdomain
private
def set_subdomain
# helper that retreives the current subdomain
get_company
Thread.current[:current_subdomain] = @company.subdomain
end
User Session Model (UserSession.rb)
find_by_login_method :find_by_email_within_company
我读了一些关于使用Thread.current和名称空间冲突的东西。这是一个对我有用的好方法,但在赏金过期之前,我很想听听其他建议,否则,+100给Jens Fahnenbruck :)