Module: Sorcery::Model

Defined in:
lib/sorcery/model.rb,
lib/sorcery.rb,
lib/sorcery/model/temporary_token.rb,
lib/sorcery/model/adapters/mongoid.rb,
lib/sorcery/model/submodules/external.rb,
lib/sorcery/model/adapters/mongo_mapper.rb,
lib/sorcery/model/adapters/active_record.rb,
lib/sorcery/model/submodules/remember_me.rb,
lib/sorcery/model/submodules/reset_password.rb,
lib/sorcery/model/submodules/user_activation.rb,
lib/sorcery/model/submodules/activity_logging.rb,
lib/sorcery/model/submodules/brute_force_protection.rb

Overview

This module handles all plugin operations which are related to the Model layer in the MVC pattern. It should be included into the ORM base class. In the case of Rails this is usually ActiveRecord (actually, in that case, the plugin does this automatically).

When included it defines a single method: 'authenticates_with_sorcery!' which when called adds the other capabilities to the class. This method is also the place to configure the plugin in the Model layer.

Defined Under Namespace

Modules: Adapters, ClassMethods, InstanceMethods, Submodules, TemporaryToken Classes: Config

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sorcery/model.rb', line 10

def self.included(klass)
  klass.class_eval do
    class << self
      def authenticates_with_sorcery!
        @sorcery_config = Config.new
        self.class_eval do
          extend ClassMethods # included here, before submodules, so they can be overriden by them.
          include InstanceMethods
          include TemporaryToken
        end
        
        include_required_submodules!

        # This runs the options block set in the initializer on the model class.
        ::Sorcery::Controller::Config.user_config.tap{|blk| blk.call(@sorcery_config) if blk}
        
        init_mongoid_support! if defined?(Mongoid) and self.ancestors.include?(Mongoid::Document)
        init_mongo_mapper_support! if defined?(MongoMapper) and self.ancestors.include?(MongoMapper::Document)

        init_orm_hooks!
        
        @sorcery_config.after_config << :add_config_inheritance if @sorcery_config.subclasses_inherit_config
        @sorcery_config.after_config.each { |c| send(c) }
      end
      
      protected
      
      # includes required submodules into the model class, 
      # which usually is called User.
      def include_required_submodules!
        self.class_eval do
          @sorcery_config.submodules = ::Sorcery::Controller::Config.submodules
          @sorcery_config.submodules.each do |mod|
            begin
              include Submodules.const_get(mod.to_s.split("_").map {|p| p.capitalize}.join("")) 
            rescue NameError
              # don't stop on a missing submodule. Needed because some submodules are only defined 
              # in the controller side.
            end
          end
        end
      end
      
      # defines mongoid fields on the model class,
      # using 1.8.x hash syntax to perserve compatibility.
      def init_mongoid_support!
        self.class_eval do
          sorcery_config.username_attribute_names.each do |username|
            field username,         :type => String
          end
          field sorcery_config.email_attribute_name,            :type => String unless sorcery_config.username_attribute_names.include?(sorcery_config.email_attribute_name)
          field sorcery_config.crypted_password_attribute_name, :type => String
          field sorcery_config.salt_attribute_name,             :type => String
        end
      end
      
      # defines mongo_mapper fields on the model class,
      def init_mongo_mapper_support!
        self.class_eval do
          sorcery_config.username_attribute_names.each do |username|
            key username, String
          end
          key sorcery_config.email_attribute_name, String unless sorcery_config.username_attribute_names.include?(sorcery_config.email_attribute_name)
          key sorcery_config.crypted_password_attribute_name, String
          key sorcery_config.salt_attribute_name, String
        end
      end

      # add virtual password accessor and ORM callbacks.
      def init_orm_hooks!
        self.class_eval do
          attr_accessor @sorcery_config.password_attribute_name
          attr_protected @sorcery_config.crypted_password_attribute_name, @sorcery_config.salt_attribute_name
          before_save :encrypt_password, :if => Proc.new { |record|
            record.send(sorcery_config.password_attribute_name).present?
          }
          after_save :clear_virtual_password, :if => Proc.new { |record|
            record.send(sorcery_config.password_attribute_name).present?
          }
        end
      end
    end
  end
end