Module: Accounts::Helpers

Defined in:
lib/accounts/helpers.rb

Instance Method Summary collapse

Instance Method Details

#authenticate!(email, password) ⇒ Object



75
76
77
78
79
80
# File 'lib/accounts/helpers.rb', line 75

def authenticate!(email, password)
   = ::Accounts::Account.first(:email => email) \
    or return false
  .confirm_password(password) or return false
  session[:account_id] = .id
end

#on_email_confirmed(account) ⇒ Object



41
42
43
44
45
46
# File 'lib/accounts/helpers.rb', line 41

def on_email_confirmed()
  .status << :email_confirmed
  .taint! :status  # taint!() defined in model.rb
  .save
  Accounts.[.email]
end

#register_new_account(email) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/accounts/helpers.rb', line 82

def (email)
   = ::Accounts::Account.create ({ :email => email })
  .saved? or return "We are unable to register you at this time.  Please try again later."
  tok = ::Accounts::ActionToken.create({ :account => , :action => 'reset password' })
  link = "#{site}/response-token/#{tok.id}"
  Accounts.deliver_registration_confirmation[.email, link]
end

#respond_to_token(id) ⇒ Object



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
# File 'lib/accounts/helpers.rb', line 48

def respond_to_token(id)
  token = ::Accounts::ActionToken.get(id)

  raise ::Accounts::AccountsError.new 404, %Q{Page not found.  Go to <a href="/">home page</a>.} \
    unless token

  begin
    on_email_confirmed token. \
      unless token..status.include? :email_confirmed

    case token.action
    when 'change email' then
      token..email = token.params[:new_email]
      token..save or return "We are unable to change your e-mail right now.  Try again later."
      session[:account_id] = token..id # this visitor is authenticated
      redirect to("/logon?email=#{token.params[:new_email]}")
    when 'reset password' then
      session[:account_id] = token..id # this visitor is authenticated
      redirect '/change-password'
    else
      nil
    end
  ensure
    token.destroy or raise "Failed to destroy token #{token.id}"
  end
end

#send_change_email_confirmation(account, new_email) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/accounts/helpers.rb', line 31

def send_change_email_confirmation(, new_email)
  tok = ::Accounts::ActionToken.create({ 
    :account => ,
    :action => 'change email',
    :params => {:new_email => new_email}
  })
  link = "#{site}/response-token/#{tok.id}"
  Accounts.deliver_change_email_confirmation[.email, new_email, link]
end


25
26
27
28
29
# File 'lib/accounts/helpers.rb', line 25

def send_change_password_link()
  tok = ::Accounts::ActionToken.create({ :account => , :action => 'reset password' })
  link = "#{site}/response-token/#{tok.id}"
  Accounts.deliver_change_password_link[.email, link]
end

#siteObject



19
20
21
22
23
# File 'lib/accounts/helpers.rb', line 19

def site
  scheme = request.env['rack.url_scheme']
  host = request.env['HTTP_HOST'] # includes port number
  "#{scheme}://#{host}"
end