1 class UserController < ApplicationController
4 # Override this function in your own application to define a custom home action.
7 @fullname = "#{current_user.firstname} #{current_user.lastname}"
9 @fullname = "Not logged in..."
10 end # this is a bit of a hack since the home action is used to verify user
11 # keys, where noone is logged in. We should probably create a unique
12 # 'validate_key' action instead.
15 # The action used to log a user in. If the user was redirected to the login page
16 # by the login_required method, they should be sent back to the page they were
17 # trying to access. If not, they will be sent to "/user/home".
19 return if generate_blank
20 @user = User.new(params[:user])
21 if session[:user] = User.authenticate(params[:user][:login], params[:user][:password])
22 session[:user].logged_in_at = Time.now
24 flash[:notice] = 'Login successful'
25 redirect_to_stored_or_default :action => 'home'
27 @login = params[:user][:login]
28 flash.now[:warning] = 'Login unsuccessful'
32 # Register as a new user. Upon successful registration, the user will be sent to
33 # "/user/login" to enter their details.
35 return if generate_blank
36 params[:user].delete('form')
37 params[:user].delete('verified') # you CANNOT pass this as part of the request
38 @user = User.new(params[:user])
40 User.transaction(@user) do
41 @user.new_password = true
42 unless LoginEngine.config(:use_email_notification) and LoginEngine.config(:confirm_account)
46 key = @user.generate_security_token
47 url = url_for(:action => 'home', :user_id => @user.id, :key => key)
48 flash[:notice] = 'Signup successful!'
49 if LoginEngine.config(:use_email_notification) and LoginEngine.config(:confirm_account)
50 UserNotify.deliver_signup(@user, params[:user][:password], url)
51 flash[:notice] << ' Please check your registered email account to verify your account registration and continue with the login.'
53 flash[:notice] << ' Please log in.'
55 redirect_to :action => 'login'
59 flash.now[:notice] = nil
60 flash.now[:warning] = 'Error creating account: confirmation email not sent'
61 logger.error "Unable to send confirmation E-Mail:"
68 redirect_to :action => 'login'
72 return if generate_filled_in
73 if do_change_password_for(@user)
74 # since sometimes we're changing the password from within another action/template...
75 #redirect_to :action => params[:back_to] if params[:back_to]
76 redirect_back_or_default :action => 'change_password'
81 def do_change_password_for(user)
83 User.transaction(user) do
84 user.change_password(params[:user][:password], params[:user][:password_confirmation])
86 if LoginEngine.config(:use_email_notification)
87 UserNotify.deliver_change_password(user, params[:user][:password])
88 flash[:notice] = "Updated password emailed to #{@user.email}"
90 flash[:notice] = "Password updated."
94 flash[:warning] = 'There was a problem saving the password. Please retry.'
99 flash[:warning] = 'Password could not be changed at this time. Please retry.'
107 # Always redirect if logged in
109 flash[:message] = 'You are currently logged in. You may change your password now.'
110 redirect_to :action => 'change_password'
114 # Email disabled... we are unable to provide the password
115 if !LoginEngine.config(:use_email_notification)
116 flash[:message] = "Please contact the system admin at #{LoginEngine.config(:admin_email)} to reset your password."
117 redirect_back_or_default :action => 'login'
121 # Render on :get and render
122 return if generate_blank
125 if params[:user][:email].empty?
126 flash.now[:warning] = 'Please enter a valid email address.'
127 elsif (user = User.find_by_email(params[:user][:email])).nil?
128 flash.now[:warning] = "We could not find a user with the email address #{params[:user][:email]}"
131 User.transaction(user) do
132 key = user.generate_security_token
133 url = url_for(:action => 'change_password', :user_id => user.id, :key => key)
134 UserNotify.deliver_forgot_password(user, url)
135 flash[:notice] = "Instructions on resetting your password have been emailed to #{params[:user][:email]}"
138 redirect_to :action => 'login'
141 redirect_back_or_default :action => 'home'
143 flash.now[:warning] = "Your password could not be emailed to #{params[:user][:email]}"
149 return if generate_filled_in
154 def do_edit_user(user)
156 User.transaction(user) do
157 user.attributes = params[:user].delete_if { |k,v| not LoginEngine.config(:changeable_fields).include?(k) }
159 flash[:notice] = "User details updated"
161 flash[:warning] = "Details could not be updated! Please retry."
165 flash.now[:warning] = "Error updating user details. Please try again later."
173 if do_delete_user(@user)
176 redirect_back_or_default :action => 'home'
181 def do_delete_user(user)
183 if LoginEngine.config(:delayed_delete)
184 User.transaction(user) do
185 key = user.set_delete_after
186 if LoginEngine.config(:use_email_notification)
187 url = url_for(:action => 'restore_deleted', :user_id => user.id, :key => key)
188 UserNotify.deliver_pending_delete(user, url)
196 if LoginEngine.config(:use_email_notification)
197 flash.now[:warning] = 'The delete instructions were not sent. Please try again later.'
199 flash.now[:notice] = 'The account has been scheduled for deletion. It will be removed in #{LoginEngine.config(:delayed_delete_days)} days.'
211 flash.now[:warning] = "The account for #{@user['login']} was not restored. Please try the link again."
212 redirect_to :action => 'login'
214 redirect_to :action => 'home'
221 UserNotify.deliver_delete(user) if LoginEngine.config(:use_email_notification)
222 flash[:notice] = "The account for #{user['login']} was successfully deleted."
227 if ['login', 'signup', 'forgot_password'].include?(action)
234 # Generate a template user for certain actions on get
245 # Generate a template user for certain actions on get
246 def generate_filled_in
256 # returns the user object this method should act upon; only really
257 # exists for other engines operating on top of this one to redefine...
258 def get_user_to_act_on
259 @user = session[:user]