10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Deviseにおいて、複数の認証キー(企業コード、ユーザID、パスワードの組み合わせ)で認証する

10
Posted at
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
10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?