Authlogic에는 활성 모델에서 사용할 수없고 Mongoid에서는 사용할 수없는 많은 Active Record 기능이 필요합니다. Authlogic을 Mongoid와 함께 사용하려면 어떻게해야합니까? Authlogic Authlogic 패치가 원숭이에서 Mongoid 함께 사용하는Mongoid와 Authlogic을 어떻게 사용할 수 있습니까?
2
A
답변
5
것이 가능하다. YMMV.
먼저 Authlogic과 Mongoid를 모두 Gemfile에 추가하십시오.
gem 'mongoid', github: 'mongoid/mongoid' # Currently required for Rails 4
gem 'authlogic', '~> 3.3.0'
다음 원숭이 패치 Authlogic.
: 먼저config/application.rb
# config/application.rb
module YourApp
class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/lib) # ADD THIS
end
end
이 config/initializers/authlogic.rb
를 작성하여 모듈을 필요로 한 줄을 추가 레일을 알려 편집하여 새 모듈을로드 할 파일 lib/modules/login.rb
# lib/modules/login.rb
module Authlogic
module ActsAsAuthentic
module Login
module Config
def find_by_smart_case_login_field(login)
# Case insensitivity for my project means downcase.
login = login.downcase unless validates_uniqueness_of_login_field_options[:case_sensitive]
if login_field
where({ login_field.to_sym => login }).first
else
where({ email_field.to_sym => login }).first
end
end
end
end
end
end
에게 레일을 디렉토리 lib/modules
을 만들
# config/initializers/authlogic.rb
require 'modules/login.rb'
다음 단계는 인증 가능한 것에 대한 우려를 만드는 것입니다. 우려 당신은 기본적으로 즉 레일 3.
# app/models/concerns/authenticable.rb
module Authenticable
extend ActiveSupport::Concern
included do
include Authlogic::ActsAsAuthentic::Base
include Authlogic::ActsAsAuthentic::Email
include Authlogic::ActsAsAuthentic::LoggedInStatus
include Authlogic::ActsAsAuthentic::Login
include Authlogic::ActsAsAuthentic::MagicColumns
include Authlogic::ActsAsAuthentic::Password
include Authlogic::ActsAsAuthentic::PerishableToken
include Authlogic::ActsAsAuthentic::PersistenceToken
include Authlogic::ActsAsAuthentic::RestfulAuthentication
include Authlogic::ActsAsAuthentic::SessionMaintenance
include Authlogic::ActsAsAuthentic::SingleAccessToken
include Authlogic::ActsAsAuthentic::ValidationsScope
end
def readonly?
false
end
module ClassMethods
def <(klass)
return true if klass == ::ActiveRecord::Base
super(klass)
end
def column_names
fields.map &:first
end
def quoted_table_name
self.name.underscore.pluralize
end
def default_timezone
:utc
end
def primary_key
if caller.first.to_s =~ /(persist|session)/
:_id
else
@@primary_key
end
end
def find_by__id(*args)
find *args
end
def find_by_persistence_token(token)
where(persistence_token: token).first
end
# Replace this with a finder to your login field
def find_by_email(email)
where(email: email).first
end
def with_scope(query)
query = where(query) if query.is_a?(Hash)
yield query
end
end
end
에 인증 가능 클래스에 직접 추가해야합니다 레일 (4)에 기본적으로 만 사용할 수 있습니다. 다음은 샘플 User
입니다.
# app/models/user.rb
class User
include Mongoid::Document
include Mongoid::Timestamps
include Authenticable
field :name
field :email
field :crypted_password
field :password_salt
field :persistence_token
field :single_access_token
field :perishable_token
field :login_count, type: Integer, default: 0
field :failed_login_count, type: Integer, default: 0
field :last_request_at, type: DateTime
field :last_login_at, type: DateTime
field :current_login_at, type: DateTime
field :last_login_ip
field :current_login_ip
field :role, type: Symbol, default: :user
index({ name: 1 })
index({ email: 1 }, { unique: true })
index({ persistence_token: 1}, { unique: true })
index({ last_login_at: 1})
validates :name, presence: true
validates :email, presence: true, uniqueness: true
validates :crypted_password, presence: true
validates :password_salt, presence: true
acts_as_authentic do |config|
config.login_field = 'email' # Remember to add a finder for this
end
end
UserSession
는 다음과 같을 것이다 :
# app/models/user_session.rb
class UserSession < Authlogic::Session::Base
def to_key
new_record? ? nil : [self.send(self.class.primary_key)]
end
def to_partial_path
self.class.name.underscore
end
end
응용 프로그램 컨트롤러 헬퍼는 항상 동일합니다.
# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
helper_method :current_user
private
def current_user_session
@current_user_session = UserSession.find unless
defined?(@current_user_session)
@current_user_session
end
def current_user
@current_user = current_user_session &&
current_user_session.user unless defined?(@current_user)
@current_user
end
end
그게 전부입니다. 다른 모든 것은 이전과 똑같이 작동해야합니다.