Module: MerbAuth::Adapter::Common::InstanceMethods

Defined in:
lib/merb-auth/adapters/common.rb

Instance Method Summary collapse

Instance Method Details

#activateObject



32
33
34
35
36
37
38
39
# File 'lib/merb-auth/adapters/common.rb', line 32

def activate
  self.reload unless new_record? # Make sure the model is up to speed before we try to save it
  set_activated_data!
  self.save

  # send mail for activation
  send_activation_notification  if MA[:use_activation] && !MA[:no_activation_email]
end

#activated?Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/merb-auth/adapters/common.rb', line 46

def activated?
 return false if self.new_record?
 !! activation_code.nil?
end

#active?Boolean

Alias for activated?

Returns:

  • (Boolean)


52
# File 'lib/merb-auth/adapters/common.rb', line 52

def active?; activated?; end

#authenticated?(password) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/merb-auth/adapters/common.rb', line 24

def authenticated?(password)
  crypted_password == encrypt(password)
end

#deliver_email(action, params) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/merb-auth/adapters/common.rb', line 124

def deliver_email(action, params)
  return if defined?(Merb) && Merb.testing?
  from = MA[:from_email]

  params[:subject] = unix2dos(params[:subject])

  MA::UserMailer.dispatch_and_deliver(action, params.merge(
    :from => from, 
    :to => self.email,
    "Mime-Version" => "1.0",
    "Content-Type" => %Q{text/plain; charset="UTF-8"},
    "Content-Transfer-Encoding" => 'base64'
  ), MA[:single_resource] => self)
end

#encrypt(password) ⇒ Object

Encrypts the password with the user salt



14
15
16
# File 'lib/merb-auth/adapters/common.rb', line 14

def encrypt(password)
  self.class.encrypt(password, salt)
end

#encrypt_passwordObject



18
19
20
21
22
# File 'lib/merb-auth/adapters/common.rb', line 18

def encrypt_password
  return if password.blank?
  self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{MA[:login_field]}--") if new_record?
  self.crypted_password = encrypt(password)
end

#forget_meObject



102
103
104
105
106
# File 'lib/merb-auth/adapters/common.rb', line 102

def forget_me
  self.remember_token_expires_at = nil
  self.remember_token            = nil
  self.save
end

#make_activation_codeObject



72
73
74
75
76
77
78
# File 'lib/merb-auth/adapters/common.rb', line 72

def make_activation_code
  if MA[:use_activation]
    self.activation_code = self.class.make_key
  else
    set_activated_data!
  end
end

#password_required?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/merb-auth/adapters/common.rb', line 28

def password_required?
  crypted_password.blank? || !password.blank?
end

#recently_activated?Boolean

Returns true if the user has just been activated.

Returns:

  • (Boolean)


42
43
44
# File 'lib/merb-auth/adapters/common.rb', line 42

def recently_activated?
  @activated
end

#remember_meObject

These create and unset the fields required for remembering users between browser closes Default of 2 weeks



98
99
100
# File 'lib/merb-auth/adapters/common.rb', line 98

def remember_me
  remember_me_for (Merb::Const::WEEK * 2)
end

#remember_me_for(time) ⇒ Object

Useses seconds for the time



91
92
93
94
# File 'lib/merb-auth/adapters/common.rb', line 91

def remember_me_for(time)
  time = time / Merb::Const::DAY
  remember_me_until (DateTime.now + time)
end

#remember_me_until(time) ⇒ Object



84
85
86
87
88
# File 'lib/merb-auth/adapters/common.rb', line 84

def remember_me_until(time)
  self.remember_token_expires_at = time
  self.remember_token            = encrypt("#{email}--#{remember_token_expires_at}")
  save
end

#remember_token?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/merb-auth/adapters/common.rb', line 80

def remember_token?
  remember_token_expires_at && DateTime.now < DateTime.parse(remember_token_expires_at.to_s)
end

#send_activation_notificationObject



108
109
110
111
112
# File 'lib/merb-auth/adapters/common.rb', line 108

def send_activation_notification
  if MA[:use_activation]
    deliver_email(:activation, :subject => (MA[:activation_subject] || "Welcome" ))
  end
end

#send_forgot_passwordObject



120
121
122
# File 'lib/merb-auth/adapters/common.rb', line 120

def send_forgot_password
  deliver_email(:forgot_password, :subject => (MA[:password_request_subject] || "Request to change your password"))
end

#send_signup_notificationObject



114
115
116
117
118
# File 'lib/merb-auth/adapters/common.rb', line 114

def 
  if MA[:use_activation]
    deliver_email(:signup, :subject => (MA[:welcome_subject] || "Please Activate Your Account") )
  end
end

#set_loginObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/merb-auth/adapters/common.rb', line 54

def 
  return nil unless self..nil?
  return nil if self.email.nil?
  logn = self.email.split("@").first
  # Check that that login is not taken
  taken_logins = self.class.("#{logn}%").map{|u| u.}
  if taken_logins.empty?
    self. = logn
  else
    taken_logins.first =~ /(\d*)$/
    if $1.blank?
      self. = "#{logn}000"
    else
      self. ="#{logn}#{$1.succ}"
    end
  end
end