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/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: ‘activate_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



9
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
# File 'lib/sorcery/model.rb', line 9

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
          
          # set the user_class for the controller methods which call it
          ::Sorcery::Controller::Config.user_class = self
          @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

        # 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}

        self.class_eval do
          field sorcery_config.username_attribute_name, type: String
          field sorcery_config.password_attribute_name, type: String
          field sorcery_config.email_attribute_name, type: String unless sorcery_config.username_attribute_name == sorcery_config.email_attribute_name
          field sorcery_config.crypted_password_attribute_name, type: String
          field sorcery_config.salt_attribute_name, type: String
        end if defined?(Mongoid) and self.ancestors.include?(Mongoid::Document)

        # add virtual password accessor and ORM callbacks
        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
        
        @sorcery_config.after_config << :add_config_inheritance if @sorcery_config.subclasses_inherit_config
        @sorcery_config.after_config.each { |c| send(c) }
      end
    end
  end
end