fix security issue
[selectricity] / old_api_code / selectricity_service.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 require 'action_controller/integration'
9
10 class SelectricityService < ActionWebService::Base
11   web_service_api SelectricityAPI
12
13   ## Casts a quickvote.
14   ## Expects a quickvote name, a voter ID, and a list of candidate ID's respectively.
15   ## Returns a string containing any potential errors that occurred in the process.
16   def cast_quickvote(election_name, voter_id, vote_list)
17     election = QuickVote.ident_to_quickvote election_name
18     if election
19       candidates=election.candidates.collect { |c| c.id }
20       vote_list[0].each do |vote|
21         raise ArgumentError.new("Invalid Candidate ID #{vote}") unless candidates.index(vote)
22       end
23       raise ArgumentError.new("You must rank all candidates") unless candidates.length <= vote_list[0].length
24       raise ArgumentError.new("Please rank each candidate only once") if vote_list[0].uniq!
25       voter = QuickVoter.new
26       voter.election = election
27       voter.ipaddress = "XMLRPC Request"
28       voter.session_id = "XMLRPC:#{voter_id}"
29       voter.vote=Vote.new
30       voter.vote.votes=vote_list[0]
31       voter.vote.time = Time.now
32       voter.save!
33       voter.vote.confirm!
34       voter.save!
35     else
36       raise ArgumentError.new("Cannot find election #{election_name}")
37     end
38   end
39
40   ## Converts QuickVote candidate ID's to names
41   ## Takes in a QuickVote name and a list of candidate ID's, and returns the names of
42   ## each candidate. Useful for doing just a few lookups; it's more efficient to use
43   ## get_quickvote_candidate_map for presenting info about an entire election.
44   def quickvote_candidate_ids_to_names(shortname, id_list)
45     qv=QuickVote.ident_to_quickvote(shortname)
46     candidates={}
47     raise ArgumentError.new("Quickvote by name #{shortname} doesn't exist") unless qv
48     qv.results
49     qv.candidates.each {|c| candidates[c.id] = c}
50     results=[]
51     id_list.each { |id|
52       name=candidates[id]
53       if name
54         results << name
55       else
56         results << ""
57       end
58     }
59     results
60   end
61
62   ## Return the results of a QuickVote.
63   ## Takes in the name of a quickvote, and returns a structure as described by
64   ## QuickVoteResultStruct
65   def get_quickvote_results(shortname)
66     #TODO: Validate shortname
67     qv=QuickVote.ident_to_quickvote(shortname)
68     result=QuickVoteResultStruct.new
69     unless qv
70       raise ArgumentError.new("No quickvote with name #{shortname} found!")
71     end
72     qv.results
73     result.plurality_winners=qv.plurality_result.winners
74     result.approval_winners=qv.approval_result.winners
75     result.condorcet_winners=qv.condorcet_result.winners
76     result.ssd_winners=qv.ssd_result.winners
77     result.borda_winners=qv.borda_result.winners
78     result
79   end
80
81   ## Returns information regarding all the candidates in a QuickVote
82   ## Takes in a QuickVote name, and returns the list of names and ID's of candidates
83   ## This can be useful for presenting the user with a list of readable names, while
84   ## the software sends results to us in the numeric ID's we require. The two lists are in
85   ## respective order.
86   def get_quickvote_candidate_map(shortname)
87     qv=QuickVote.ident_to_quickvote(shortname)
88     result=QuickVoteCandidateMap.new
89     unless qv
90       raise ArgumentError.new("No quickvote with name #{shortname} found!")
91     end
92     candidates={}
93     qv.candidates.each {|c| candidates[c.id] = c.name}
94     result.candidate_ids=candidates.keys
95     result.candidate_names=candidates.values
96     result
97   end
98
99   ## Get information on all the votes cast in a QuickVote
100   ## Takes in the name of a QuickVote, returns an array of QuickVoterInfo structures.
101   def get_quickvote_votes(shortname)
102     qv = QuickVote.ident_to_quickvote(shortname)
103
104     unless qv
105       raise ArgumentError.new("Cannot find QuickVote #{shortname}")
106     end
107
108     qv.votes.collect do |vote|
109        QuickVoterInfo.new(:voter_id => vote.voter.id,
110                     :voter_ipaddress => vote.voter.ipaddress,
111                     :vote_time => vote.time.to_i,
112                     :vote => vote.votes,
113                     :voter_session_id => vote.voter.session_id)
114     end
115   end
116
117   ## Gets a list of all QuickVotes in the system.
118   def list_quickvotes()
119     QuickVote.find(:all).collect do |election|
120       get_quickvote(election.name)
121     end
122   end
123
124   ## Gets information on a particular QuickVote
125   ## Takes in a QuickVote name
126   def get_quickvote(shortname)
127     unless election = QuickVote.ident_to_quickvote(shortname)
128       raise ArgumentError.new("Cannot find QuickVote named #{shortname}")
129     end
130
131     QuickVoteStruct.new(
132       :id => election.id,
133       :name => election.name,
134       :description => election.description,
135       :candidate_ids => election.candidates.collect {|c| c.id },
136       :candidate_names => election.candidates.collect {|c| c.name } )
137   end
138
139   ## Create a QuickVote
140   ## Pass in a QuickVoteStruct populated with all the fields but the candidate ID's
141   ## Any candidate ID's you fill in will be ignored anyway.
142   def create_quickvote(election)
143     qv = QuickVote.new(:name => election.name,
144                        :description => election.description)
145     qv.candidate_names = election.candidate_names
146
147     if qv.save
148       return ""
149     else
150       raise ArgumentError.new("Saving quickvote FAILED:"+qv.errors.inspect)
151     end
152   end
153
154 end

Benjamin Mako Hill || Want to submit a patch?