Added the RoR Login-Engine and activated it on the site.
[selectricity-live] / vendor / plugins / login_engine / app / controllers / user_controller.rb
1 class UserController < ApplicationController
2   model   :user
3
4   # Override this function in your own application to define a custom home action.
5   def home
6     if user?
7       @fullname = "#{current_user.firstname} #{current_user.lastname}"
8     else
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.
13   end
14
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".
18   def login
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
23       session[:user].save
24       flash[:notice] = 'Login successful'
25       redirect_to_stored_or_default :action => 'home'
26     else
27       @login = params[:user][:login]
28       flash.now[:warning] = 'Login unsuccessful'
29     end
30   end
31
32   # Register as a new user. Upon successful registration, the user will be sent to
33   # "/user/login" to enter their details.
34   def signup
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])
39     begin
40       User.transaction(@user) do
41         @user.new_password = true
42         unless LoginEngine.config(:use_email_notification) and LoginEngine.config(:confirm_account)
43           @user.verified = 1
44         end
45         if @user.save
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.'
52           else
53             flash[:notice] << ' Please log in.'
54           end
55           redirect_to :action => 'login'
56         end
57       end
58     rescue Exception => e
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:"
62       logger.error e
63     end
64   end
65
66   def logout
67     session[:user] = nil
68     redirect_to :action => 'login'
69   end
70
71   def change_password
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'
77     end
78   end
79
80   protected
81     def do_change_password_for(user)
82       begin
83         User.transaction(user) do
84           user.change_password(params[:user][:password], params[:user][:password_confirmation])
85           if user.save
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}"
89             else
90               flash[:notice] = "Password updated."
91             end
92             return true
93           else
94             flash[:warning] = 'There was a problem saving the password. Please retry.'
95             return false
96           end
97         end
98       rescue
99         flash[:warning] = 'Password could not be changed at this time. Please retry.'
100       end
101     end
102     
103   public
104
105
106   def forgot_password
107     # Always redirect if logged in
108     if user?
109       flash[:message] = 'You are currently logged in. You may change your password now.'
110       redirect_to :action => 'change_password'
111       return
112     end
113
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'
118       return
119     end
120
121     # Render on :get and render
122     return if generate_blank
123
124     # Handle the :post
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]}"
129     else
130       begin
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]}"
136         end  
137         unless user?
138           redirect_to :action => 'login'
139           return
140         end
141         redirect_back_or_default :action => 'home'
142       rescue
143         flash.now[:warning] = "Your password could not be emailed to #{params[:user][:email]}"
144       end
145     end
146   end
147
148   def edit
149     return if generate_filled_in
150     do_edit_user(@user)
151   end
152   
153   protected
154     def do_edit_user(user)
155       begin
156         User.transaction(user) do
157           user.attributes = params[:user].delete_if { |k,v| not LoginEngine.config(:changeable_fields).include?(k) }
158           if user.save
159             flash[:notice] = "User details updated"
160           else
161             flash[:warning] = "Details could not be updated! Please retry."
162           end
163         end
164       rescue
165         flash.now[:warning] = "Error updating user details. Please try again later."
166       end
167     end
168   
169   public
170
171   def delete
172     get_user_to_act_on
173     if do_delete_user(@user)
174       logout
175     else
176       redirect_back_or_default :action => 'home'
177     end    
178   end
179   
180   protected
181     def do_delete_user(user)
182       begin
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)
189             end
190           end
191         else
192           destroy(@user)
193         end
194         return true
195       rescue
196         if LoginEngine.config(:use_email_notification)
197           flash.now[:warning] = 'The delete instructions were not sent. Please try again later.'
198         else
199           flash.now[:notice] = 'The account has been scheduled for deletion. It will be removed in #{LoginEngine.config(:delayed_delete_days)} days.'
200         end
201         return false
202       end
203     end
204     
205   public
206
207   def restore_deleted
208     get_user_to_act_on
209     @user.deleted = 0
210     if not @user.save
211       flash.now[:warning] = "The account for #{@user['login']} was not restored. Please try the link again."
212       redirect_to :action => 'login'
213     else
214       redirect_to :action => 'home'
215     end
216   end
217
218   protected
219
220   def destroy(user)
221     UserNotify.deliver_delete(user) if LoginEngine.config(:use_email_notification)
222     flash[:notice] = "The account for #{user['login']} was successfully deleted."
223     user.destroy()
224   end
225
226   def protect?(action)
227     if ['login', 'signup', 'forgot_password'].include?(action)
228       return false
229     else
230       return true
231     end
232   end
233
234   # Generate a template user for certain actions on get
235   def generate_blank
236     case request.method
237     when :get
238       @user = User.new
239       render
240       return true
241     end
242     return false
243   end
244
245   # Generate a template user for certain actions on get
246   def generate_filled_in
247     get_user_to_act_on
248     case request.method
249     when :get
250       render
251       return true
252     end
253     return false
254   end
255   
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]
260   end
261 end

Benjamin Mako Hill || Want to submit a patch?