Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Forked from kdwinter/authenticable.rb
Created September 10, 2010 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimsynz/574490 to your computer and use it in GitHub Desktop.
Save jimsynz/574490 to your computer and use it in GitHub Desktop.
module AuthlogicModel
def self.included(model)
model.class_eval do
extend ClassMethods
include InstanceMethods
include ProtectedAttributes
field :username
field :email
field :crypted_password
field :password_salt
field :persistence_token
field :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
index :username
index :email
index :persistence_token
index :last_login_at
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
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
'users'
end
def primary_key
# FIXME: Is this check good enough?
if caller.first.to_s[/(persist|session)/]
:"_id"
else
super
end
end
def default_timezone
:utc
end
def find_by__id(*args)
find *args
end
# Change this to your preferred login field
def find_by_username(username)
where(:username => username).first
end
def with_scope(query)
query = where(query) if query.is_a?(Hash)
yield query
end
end
module InstanceMethods
def readonly?
false
end
end
end
module ProtectedAttributes
def self.included(base)
base.class_eval do
include InstanceMethods
extend ClassMethods
end
end
module ClassMethods
def attr_protected(*attributes)
@@protected_attributes = attributes.collect(&:to_s)
end
def protected_attributes
@@protected_attributes ||= []
end
end
module InstanceMethods
def write_attributes(attrs = nil)
attrs = attrs.reject {|k,v| self.class.protected_attributes.include?(k.to_s)}
process(attrs || {})
identify if id.blank?
notify
end
end
end
class User
include Mongoid::Document
include Mongoid::Timestamps
include AuthlogicModel
acts_as_authentic do |config|
# Change this to your preferred login field
config.login_field = 'username'
config.instance_eval do
validates_uniqueness_of_login_field_options :scope => '_id', :case_sensitive => true
end
end
end
class UserSession < Authlogic::Session::Base
def to_key
new_record? ? nil : [self.send(self.class.primary_key)]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment