User summary method is now more secure. Email change method prtects account SQL injec...
[selectricity-live] / app / controllers / account_controller.rb
1 # Selectricity: Voting Machinery for the Masses
2 # Copyright (C) 2007, 2008 Benjamin Mako Hill <mako@atdot.cc>
3 # Copyright (C) 2007 Massachusetts Institute of Technology
4 #
5 # This program is free software. Please see the COPYING file for
6 # details.
7
8 class AccountController < ApplicationController
9   layout 'main'
10   
11   # Be sure to include AuthenticationSystem in Application Controller instead
12   include AuthenticatedSystem
13   # If you want "remember me" functionality, add this before_filter to Application Controller
14   before_filter :login_from_cookie
15
16   # say something nice, you goof!  something sweet.
17   def index
18     redirect_to(:action => 'signup') unless logged_in? || User.count > 0
19   end
20   
21   #these methods provide basic functionality for the user login system
22   #===================================================================
23   def login
24     
25     return unless request.post?
26     self.current_user = User.authenticate(params[:login], params[:password])
27     if logged_in?
28       if params[:remember_me] == "1"
29         self.current_user.remember_me
30         cookies[:auth_token] = { :value => self.current_user.remember_token , :expires => self.current_user.remember_token_expires_at }
31       end
32       redirect_back_or_default :controller => 'front'
33       flash[:notice] = "Logged in successfully"
34     end
35   end
36   
37   def forgot_password
38     raise "Not Implemented!"
39   end
40
41   def signup
42     @user = User.new(params[:user])
43     return unless request.post?
44     @user.save!
45     self.current_user = @user
46     redirect_back_or_default :controller => 'front'
47     flash[:notice] = "Thanks for signing up!"
48   rescue ActiveRecord::RecordInvalid
49     render :action => 'signup'
50   end
51   
52   def logout
53     self.current_user.forget_me if logged_in?
54     cookies.delete :auth_token
55     reset_session
56     flash[:notice] = "You have been logged out."
57     redirect_back_or_default :controller => 'front'
58   end
59   #======================================================================
60   
61   #The following methods are for selectricity specific uses
62   def summary
63     #@user = User.find(params[:id])
64     
65     #constrain the find command such that it only returns the user if it's the currently
66     #logged in user, otherwise, redirect to the front page
67     id = params[:id]
68     user_id = session[:user][:id]
69     @user = User.find(id, :conditions => ["id = ?", user_id])
70     
71     rescue
72       redirect_to :controller =>'front'
73     
74   end
75   
76   def change_contact
77     @user = User.find(params[:id])
78     return unless request.post?
79     @user.email=params[:email]
80     @user.save!
81     flash[:notice] = "Email successfully updated"
82     render :action => 'summary'
83   end
84   
85   
86 end
87
88

Benjamin Mako Hill || Want to submit a patch?