Class: Caboose::LoginController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/login_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_delete, #admin_edit, #admin_index, #admin_json, #admin_json_single, #admin_update, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#forgot_password_formObject


48
49
50
51
52
53
54
# File 'app/controllers/caboose/login_controller.rb', line 48

def forgot_password_form
  @return_url = params[:return_url].nil? ? "/" : params[:return_url]
  @modal = params[:modal].nil? ? false : params[:modal]
  redirect_to @return_url and return if logged_in?
  @page.title = "Forgot Password" if @page
  render :layout => "caboose/application"
end

#indexObject


6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/caboose/login_controller.rb', line 6

def index
  if params[:logout]
    logout_user
    elo = User.find(User::LOGGED_OUT_USER_ID)        
    (elo)
  end      
  @return_url = params[:return_url].nil? ? "/" : params[:return_url]
  @modal = params[:modal].nil? ? false : params[:modal]
  @page.title = "Login" if @page
  redirect_to @return_url and return if logged_in?
  render :layout => "caboose/application"
end

#loginObject


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
# File 'app/controllers/caboose/login_controller.rb', line 20

def       
  resp = StdClass.new('error' => '', 'redirect' => '')
  return_url = params[:return_url].nil? ? "/" : params[:return_url]
  if logged_in?
    resp.redirect = return_url
  elsif params[:username].blank?
    resp.error = "Please provide a username."
  elsif params[:password].blank?
    resp.error = "Please provide a password."
  else
    username = params[:username].downcase
    password = params[:password]
    bouncer_class = Caboose::authenticator_class.constantize
    bouncer = bouncer_class.new
     = bouncer.authenticate(username, password, @site, request)
    if .error
      resp.error = .error
    else
      remember = params[:remember] && (params[:remember] == 1 || params[:remember] == "1" || params[:remember] == 'on')
      (.user, remember)
      resp.redirect = Caboose.plugin_hook('login_success', return_url, .user)
      resp.modal = false                        
    end
  end
  render :json => resp      
end

#reset_passwordObject


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/caboose/login_controller.rb', line 102

def reset_password
   @return_url = params[:return_url].nil? ? "/" : params[:return_url]
   redirect_to @return_url if logged_in?
   
   resp = Caboose::StdClass.new
	
  reset_id = params[:id]
  pass1    = params[:pass1]
  pass2    = params[:pass2]
  
  if reset_id.blank?
    resp.error = "This password reset link is invalid."
  else
    user = Caboose::User.user_for_reset_id(reset_id)
  	if user.nil?
  	  resp.error = "This password reset link is invalid."
  	elsif pass1.length < 8
  	  resp.error = "Passwords must be at least 8 characters."
     elsif pass1 != pass2
       resp.error = "Your passwords don't match."
  	else          
  	  user.password = Digest::SHA1.hexdigest(Caboose::salt + pass1)
  	  user.password_reset_id = ''
  	  user.password_reset_sent = ''
  	  user.save
  		resp.redirect = '/login'
  	end
  	
  end
  render :json => resp
end

#reset_password_formObject


89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/caboose/login_controller.rb', line 89

def reset_password_form
@return_url = params[:return_url].nil? ? "/" : params[:return_url]
if logged_in?
  redirect_to @return_url 
  return
end
  @reset_id = params[:reset_id]      
  @user = Caboose::User.user_for_reset_id(@reset_id)
  @page.title = "Reset Password" if @page
  render :layout => "caboose/application"
end

#send_reset_emailObject


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
# File 'app/controllers/caboose/login_controller.rb', line 57

def send_reset_email
    @return_url = params[:return_url].nil? ? "/" : params[:return_url]
    redirect_to @return_url if logged_in?
    resp = Caboose::StdClass.new		      		
    username = params[:username]
  if username.blank?
  	resp.error = "You must enter a username or email address."
      render :json => resp		  	
  	return
  end
  
  bob = Caboose::User.where(:site_id => @site.id, :username => username.strip.downcase).first
  bob = Caboose::User.where(:site_id => @site.id, :email    => username.strip.downcase).first if bob.nil?		  
  
  if bob.nil?		
	  resp.error = "The given username or email address does not exist."
	  render :json => resp
	  return
	end
	
  rand = Array.new(20){rand(36).to_s(36)}.join		  
  bob.password_reset_id = rand
  bob.password_reset_sent = DateTime.now
  bob.save
  		  		  
  LoginMailer.configure_for_site(@site.id).forgot_password_email(bob).deliver		  
  		  
  resp.success = "Please check your email for a link to reset your password. This link is good for 3 days."
  render :json => resp
end