fix security issue
[selectricity] / app / controllers / quickvote_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 QuickvoteController < ApplicationController
9   helper :sparklines
10   layout 'main'
11   require_dependency "quick_voter"
12   require_dependency "quick_vote"
13   require_dependency "vote"
14   require_dependency "election"
15   
16   #############################################################
17   # the following methods pertain to creating quickvotes
18   #############################################################
19
20   def create
21     if params[:quickvote]
22       @quickvote = QuickVote.new(params[:quickvote])
23
24       # check to see if any of the advanced options have been changed
25       new_qv = QuickVote.new
26       if @quickvote.election_method != new_qv.election_method \
27         or @quickvote.enddate.day != new_qv.enddate.day \
28         or @quickvote.viewable != new_qv.viewable \
29         or @quickvote.notices != new_qv.notices
30         show_advanced = true
31       end
32     end
33
34     show_advanced ||= false
35
36     if params[:quickvote]
37
38       # store the candidate grabbed through ajax and stored in flash
39       @quickvote.candidate_names = flash[:candidate_names]
40       @quickvote.description=@quickvote.description
41
42       #record who created the quickvote so that person can monitor it easily
43       @quickvote.quickuser = session.session_id
44
45       #Give registered users additional QuickVote functionality 
46       @quickvote.user_id = session[:user][:id] if session[:user]
47       @quickvote.create_candidates
48
49       # try to save, if it fails, show the page again (the flash should
50       # still be intact
51       if @quickvote.save
52         @quickvote = @quickvote.reload
53         # blank sidebar and show the success page
54         @sidebar_content = ''
55         render :action => 'success'
56       else
57         # render the sidebar
58         @sidebar_content = render_to_string(:partial => 'create_sidebar',
59           :locals => {:show_advanced => show_advanced})
60         flash.keep(:candidate_names)
61       end 
62
63     else
64       # if we don't have a quickvote param, it means that the person
65       # here has not been hitting this page and we can clear any
66       # candidate_names list in the flash
67       flash.delete(:candidate_names) if flash.has_key?(:candidate_names)
68       @quickvote = QuickVote.new
69       @sidebar_content = render_to_string(:partial => 'create_sidebar',
70         :locals => {:show_advanced => show_advanced})
71     end
72
73   end
74
75   def add_candidate
76     candidate_name = params[:ajax][:newcandidate]
77     unless candidate_name.strip.empty?
78       if flash.has_key?(:candidate_names) \
79         and flash[:candidate_names].instance_of?(Array) 
80         unless flash[:candidate_names].index(candidate_name)
81           flash[:candidate_names] << candidate_name
82         end
83      else
84        flash[:candidate_names] = [ candidate_name ]
85       end
86     end
87     flash.keep(:candidate_names)
88     render :partial => 'candidate_list'
89   end
90  
91   #############################################################
92   # the following methods pertain to *voting* in the quickvotes
93   #############################################################
94
95   def index
96     @election = QuickVote.ident_to_quickvote(params[:ident])
97     # if the person has specified an election, we show them the voting
98     # page. otherwise, we redirect back to main the page
99     if @election
100
101       # if the election is over, redirect to the the results page
102       unless @election.active?
103         redirect_to quickaction_url(:ident => params[:ident],
104                                     :action => 'results')
105       end
106
107       # look to see that the voter has been created and has voted in
108       # this election, and has confirmed their vote
109       @voter = QuickVoter.find(:all,
110         :conditions => ["session_id = ? and election_id = ?",
111                         session.session_id, @election.id])[0]
112
113       # if the voter has not voted we destroy them
114       if @voter and not @voter.voted?
115         @voter.destroy
116         @voter = nil
117       end
118
119       # if the voter does not exist or has has been destroyed, lets
120       # create a new one
121       unless @voter
122         # create a new voter and populate it
123         @voter = QuickVoter.new
124         @voter.election = @election
125         @voter.session_id = session.session_id
126               
127         # create new vote and make it the defaulted sorted list
128         @voter.vote = Vote.new
129               @voter.save
130               @voter.vote.set_defaults!
131               @voter.reload
132       end
133     else
134       redirect_to :controller => 'front'
135     end
136   end
137
138   def confirm
139     
140     # we need the election to verify that we have the right voter
141     election = QuickVote.ident_to_quickvote(params[:ident])
142
143     # find out who the voter is for this election
144     @voter = QuickVoter.find(:all,
145       :conditions => ["session_id = ? and election_id = ?", 
146                       session.session_id, election.id])[0]
147    
148     if not @voter
149       # we have not seen this  voter before. something is wrong, try
150       # again
151       redirect_to quickvote_url( :ident => params[:ident] ) 
152       
153     elsif @voter.voted? 
154       # this person has already voted, we try again
155       flash[:notice] = "You have already voted!"
156       redirect_to quickvote_url( :ident => params[:ident] )
157       
158     else
159       
160       # record the ip address for posterity
161       @voter.ipaddress = request.env["HTTP_X_FORWARDED_FOR"]
162       @voter.save
163       
164       # toggle the confirmation bit      
165       if @voter.vote.confirm!
166         @voter.reload
167         render :action => 'thanks'
168       else
169         redirect_to :action => 'index'
170       end
171     end
172   end
173  
174   def change
175     voter = QuickVoter.find(:all, :conditions => ["session_id = ?",
176                                                   session.session_id])[0]
177     voter.destroy
178     redirect_to quickvote_url( :ident => params[:ident] )
179   end
180                 
181   def list_voters
182     @map = GMap.new("map_div_id") 
183     @map.control_init(:large_map => true, :map_type => true) 
184     center = nil
185     @election=QuickVote.ident_to_quickvote(params[:id])
186     @election.voters.each do |voter|
187       next unless voter.ipaddress
188
189       location=nil
190       if Cache and location=Cache.get("GEO:#{voter.ipaddress}")
191       elsif Cache
192         location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
193         Cache.set "GEO:#{voter.ipaddress}", location
194       else
195         location = GeoKit::Geocoders::IpGeocoder.geocode(voter.ipaddress)
196       end
197       next unless location.lng and location.lat
198
199       unless center
200         center = [location.lat, location.lng]
201         @map.center_zoom_init(center, 4)
202       end
203
204       marker = GMarker.new([location.lat,location.lng],
205                            :title => "Voter",
206                            :info_window => (voter.ipaddress or "unknown"))
207       @map.overlay_init(marker)
208     end
209   end
210
211   ###############################################################
212   # the following method pertains to displaying the results of a
213   # quickvote
214   ###############################################################
215
216   def results
217     unless @election = QuickVote.ident_to_quickvote(params[:ident])
218       flash[:notice] = "Cannot find quickvote #{params[:ident]}."
219       redirect_to :controller => 'front'
220       return
221     end
222     if @election.viewable == 0 && @election.active == 1
223       render :action => 'not_viewable' and return
224     end
225     @results = @election.results
226     @candidates = {}
227     @election.candidates.each {|c| @candidates[c.id] = c}
228     @names = @election.names_by_id
229     @sidebar_content = render_to_string :partial => 'results_sidebar'
230   end
231   
232   def my_quickvotes
233     @myqvs = QuickVote.find(:all, :conditions => ["quickuser = ?",
234                                 session.session_id])
235   end
236   
237 end
238

Benjamin Mako Hill || Want to submit a patch?