Module: Sorcery::Model::Submodules::UserActivation

Defined in:
lib/sorcery/model/submodules/user_activation.rb

Overview

This submodule adds the ability to make the user activate his account via email or any other way in which he can recieve an activation code. with the activation code the user may activate his account. When using this submodule, supplying a mailer is mandatory.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.included(base) ⇒ 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
55
56
57
# File 'lib/sorcery/model/submodules/user_activation.rb', line 9

def self.included(base)
  base.sorcery_config.class_eval do
    # The attribute name to hold activation state (active/pending).
    attr_accessor :activation_state_attribute_name
    # The attribute name to hold activation code (sent by email).
    attr_accessor :activation_token_attribute_name
    # The attribute name to hold activation code expiration date.
    attr_accessor :activation_token_expires_at_attribute_name
    # How many seconds before the activation code expires. nil for never expires.
    attr_accessor :activation_token_expiration_period
    # Your mailer class. Required when activation_mailer_disabled == false.
    attr_accessor :user_activation_mailer
    # When true sorcery will not automatically email activation details and allow you to manually handle how and when email is sent
    attr_accessor :activation_mailer_disabled
    # Activation needed email method on your mailer class.
    attr_accessor :activation_needed_email_method_name
    # Activation success email method on your mailer class.
    attr_accessor :activation_success_email_method_name
    # Do you want to prevent or allow users that did not activate by email to login?
    attr_accessor :prevent_non_active_users_to_login
  end

  base.sorcery_config.instance_eval do
    @defaults.merge!(:@activation_state_attribute_name             => :activation_state,
                     :@activation_token_attribute_name             => :activation_token,
                     :@activation_token_expires_at_attribute_name  => :activation_token_expires_at,
                     :@activation_token_expiration_period          => nil,
                     :@user_activation_mailer                      => nil,
                     :@activation_mailer_disabled                  => false,
                     :@activation_needed_email_method_name         => :activation_needed_email,
                     :@activation_success_email_method_name        => :activation_success_email,
                     :@prevent_non_active_users_to_login           => true)
    reset!
  end

  base.class_eval do
    # don't setup activation if no password supplied - this user is created automatically
    sorcery_adapter.define_callback :before, :create, :setup_activation, if: proc { |user| user.send(sorcery_config.password_attribute_name).present? }
    # don't send activation needed email if no crypted password created - this user is external (OAuth etc.)
    sorcery_adapter.define_callback :after, :commit, :send_activation_needed_email!, on: :create, if: :send_activation_needed_email?
  end

  base.sorcery_config.after_config << :validate_mailer_defined
  base.sorcery_config.after_config << :define_user_activation_fields
  base.sorcery_config.before_authenticate << :prevent_non_active_login

  base.extend(ClassMethods)
  base.send(:include, InstanceMethods)
end