User.rb
class User < ActiveRecord::Base
belongs_to :company
belongs_to :branch
belongs_to :shop
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:authentication_keys => [:company_code, :user_id]
attr_accessor :company_code
# enable authentication by user_id
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
logger.debug "*************** conditions class ******************"
logger.debug conditions.to_yaml
company_code = conditions.delete(:company_code)
company = Company.where(code: company_code).first
user_id = conditions.delete(:user_id)
logger.debug "company_code: " + company_code
logger.debug "company: " + company.to_yaml
logger.debug "user_id: " + user_id
# devise認証を、複数項目(企業ID、及びユーザID)に対応させる
if company && user_id
where(conditions).where(["company_id = :company_id AND lower(user_id) = :user_id",
{ company_id: company.id, user_id: user_id }]).first
else
where(conditions).first
end
end