Skip to content

Commit 6e405be

Browse files
committed
added ability to limit duplicate votes based on IP
1 parent 8b0f042 commit 6e405be

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

client/app/scripts/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ angular
3737
})
3838
.constant('myConfig', {
3939
'backend': 'http://45.55.31.147:9090/api/poll/',
40-
'version': 0.2
40+
'version': 0.3
4141
});

client/app/scripts/controllers/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ angular.module('materialPollApp')
2222
}
2323
if(added !== false){
2424
$('.submitButton').hide();
25-
$http.post(myConfig.backend,{answers: $scope.options, name: $scope.title, multiple: $scope.multiple, votes: votes})
25+
$http.post(myConfig.backend,{answers: $scope.options, name: $scope.title, multiple: $scope.multiple, singleIP: $scope.singleIP, votes: votes})
2626
.success(function(data){
2727
$scope.linkto = data[1].poll.id;
2828
$scope.location = window.location.protocol + window.location.host + '/#/' + $scope.linkto;

client/app/views/main.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
</div>
1515
<div class="card-action" style="margin-left:25px;margin-right:25px;">
1616
<p>
17-
<input type="checkbox" ng-model="multiple" id="test5" />
18-
<label for="test5" class='red-text text-lighten-5'>Allow multiple poll choices?</label>
17+
<input type="checkbox" ng-model="multiple" id="multiple" />
18+
<label for="multiple" class='red-text text-lighten-5'>Allow multiple poll choices?</label>
19+
</p>
20+
<p>
21+
<input type="checkbox" ng-model="singleIP" id="singleip" />
22+
<label for="singleip" class='red-text text-lighten-5'>Restrict Votes By IP Address?</label>
1923
</p>
2024
<a ng-click="submit()" ng-if="!linkto" class="submitButton waves-effect waves-light btn red lighten-2 red-text text-lighten-5">Create Poll</a>
2125
<a ng-href="#/{{linkto}}" ng-if="linkto" class="waves-effect waves-light btn red lighten-2 red-text text-lighten-5">View Poll</a>

server/app/models/poll.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ var PollSchema = new Schema({
66
name: String,
77
answers: Schema.Types.Mixed,
88
votes: Schema.Types.Mixed,
9-
multiple: Boolean
9+
multiple: Boolean,
10+
singleIP: Boolean,
11+
IpAddresses: Schema.Types.Mixed,
1012
});
1113

1214
module.exports = mongoose.model('Poll', PollSchema);

server/server.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ router.route('/poll')
7272
poll.id = makeid();
7373
poll.name = req.body.name; // set the poll name (comes from the request)
7474
poll.multiple = req.body.multiple;
75+
poll.singleIP = req.body.singleIP;
7576
poll.answers = req.body.answers;
77+
poll.IpAddresses = [];
7678
poll.votes = req.body.votes;
7779
poll.save(function(err, poll) {
7880
if (err)
@@ -113,14 +115,33 @@ router.route('/poll/:poll_id')
113115
if (err){
114116
res.send(err);
115117
}
116-
117-
for(x = 0; x < req.body.votes.length; x++){
118-
if(req.body.votes[x] != null){
119-
poll.votes[x] += req.body.votes[x];
118+
if(poll.singleIP == true){
119+
var ip = req.headers['x-forwarded-for'] ||
120+
req.connection.remoteAddress ||
121+
req.socket.remoteAddress ||
122+
req.connection.socket.remoteAddress;
123+
124+
if (poll.IpAddresses.indexOf(ip) > -1) {
125+
//In the array!
126+
} else {
127+
poll.IpAddresses.push(ip);
128+
for(x = 0; x < req.body.votes.length; x++){
129+
if(req.body.votes[x] != null){
130+
poll.votes[x] += req.body.votes[x];
131+
}
132+
}
133+
poll.markModified('IpAddresses');
134+
poll.markModified('votes');
135+
}
136+
}else{
137+
for(x = 0; x < req.body.votes.length; x++){
138+
if(req.body.votes[x] != null){
139+
poll.votes[x] += req.body.votes[x];
140+
}
120141
}
142+
poll.markModified('votes');
121143
}
122-
123-
poll.markModified('votes');
144+
124145
poll.save(function(err) {
125146
io.emit('chat' + req.params.poll_id, poll);
126147
if (err){

0 commit comments

Comments
 (0)